@@ -36,6 +36,7 @@ public static function load($class) {
36
36
'phpsecOtp ' => 'phpsec.otp.php ' ,
37
37
'phpsecStore ' => 'phpsec.store.php ' ,
38
38
'phpsecToken ' => 'phpsec.token.php ' ,
39
+ 'phpsecPw ' => 'phpsec.pw.php ' ,
39
40
'phpsecStoreFilesystem ' => 'phpsec.store.filesystem.php ' ,
40
41
);
41
42
@@ -247,121 +248,4 @@ public static function genUid($length = 50) {
247
248
$ randLength = $ length -strlen ($ timeStamp );
248
249
return $ timeStamp .phpsecRand::str ($ randLength );
249
250
}
250
-
251
- /**
252
- * Create a hashed version of a password, safe for storage in a database.
253
- * This function return a json encodeed array that can be stored directly
254
- * in a database. The array has the following layout:
255
- * array(
256
- * 'hash' => The hash created from the password and a salt.
257
- * 'salt' => The salt that was used along with the password to create the hash.
258
- * 'nse?algo' => The hashing algorythm used.
259
- * 'injection' => How the salt was injected into the password.
260
- * )
261
- * The following injection methods exists:
262
- * before: The salt is placed diectly in front of the password, without using any
263
- * seperation characters.
264
- * after: The salt is placed directly after the password without any seperation
265
- * characters.
266
- *
267
- * @param string $password
268
- * The password to hash.
269
- *
270
- * @return string
271
- * Returns a json encoded array containing the password hash, salt and
272
- * some meta data.
273
- */
274
- public static function pwHash ($ password ) {
275
- $ salt = self ::genUid ();
276
- $ injected = self ::pwInject ($ password , $ salt );
277
- $ hash = hash (self ::HASH_TYPE , $ injected );
278
-
279
- $ return = array (
280
- 'hash ' => $ hash ,
281
- 'salt ' => $ salt ,
282
- 'algo ' => self ::HASH_TYPE ,
283
- );
284
- return json_encode ($ return );
285
- }
286
-
287
- /**
288
- * Validate a user-supplied password against a stored password saved
289
- * using the pwHash() method.
290
- *
291
- * @param string $password
292
- * The password supplied by the user in the login form.
293
- *
294
- * @param string $dbPassword
295
- * The json string fetched from the database, in the exact format
296
- * as created by pwHash().
297
- *
298
- * @return boolean
299
- * True on password match, false otherwise.
300
- */
301
- public static function pwCheck ($ password , $ dbPassword ) {
302
- /**
303
- * Unserialize registerd password array and validate it to ensure
304
- * we got a valid array.
305
- */
306
- $ data = json_decode ($ dbPassword , true );
307
- if (isset ($ data ['algo ' ]) && sizeof ($ data ) == 3 ) {
308
- /**
309
- * Ok, we are pretty sure this is good stuff. Now inject the salt
310
- * into the user supplied password, to see if it matches the registerd
311
- * data from $dbPassword.
312
- */
313
- $ pwInjected = self ::pwInject ($ password , $ data ['salt ' ]);
314
- /* Create a hash and see if it matches. */
315
- if (hash ($ data ['algo ' ], $ pwInjected ) == $ data ['hash ' ]) {
316
- return true ;
317
- }
318
- } else {
319
- /* Invalid array supplied. */
320
- self ::error ('Invalid data supplied. Expected serialized array as returned by pwHash() ' );
321
- }
322
- return false ;
323
- }
324
-
325
- /**
326
- * Check the age of a salted password.
327
- *
328
- * @param string $dbPassword
329
- * The json string fetched from the database, in the exact format
330
- * as created by pwHash().
331
- *
332
- * @return integer
333
- * Age of password in seconds.
334
- */
335
- public static function pwAge ($ dbPassword ) {
336
- $ data = json_decode ($ dbPassword , true );
337
- if (isset ($ data ['salt ' ])) {
338
- $ date = substr ($ data ['salt ' ], 0 , 22 );
339
- return gmdate ('U ' ) - strtotime ($ date );
340
- } else {
341
- /* Invalid array supplied. */
342
- self ::error ('Invalid data supplied. Expected serialized array as returned by pwHash() ' );
343
- }
344
- return false ;
345
- }
346
-
347
- /**
348
- * Inject a salt into a password to create the string to be hashed.
349
- *
350
- * @param string $password
351
- * Plain-text password.
352
- *
353
- * @param string $salt
354
- * Well, the salt to inject into the password.
355
- *
356
- * @return string
357
- * Returns the salted password, ready to be hashed.
358
- *
359
- */
360
- private static function pwInject ($ password , $ salt ) {
361
- $ hex = hexdec (substr (hash (self ::HASH_TYPE , $ password ), 0 , 1 ));
362
- $ len = strlen ($ password );
363
- $ pos = floor ($ hex *($ len /16 ));
364
-
365
- return substr ($ password , 0 , $ pos ).$ salt .substr ($ password , $ pos );
366
- }
367
251
}
0 commit comments