Last active
March 27, 2019 17:58
-
-
Save rohjay/932111db1d1442d74ff0 to your computer and use it in GitHub Desktop.
Class to get the strongest hashing algorigthm available on any system to salt and hash passwords. Hasheesh =]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Hasheesh | |
{ | |
public static $algo = False; | |
public static function init() | |
{ | |
$algos = array(); | |
foreach ( hash_algos() as $algo ) { | |
if ( 0 === strpos($algo, 'sha') ) { | |
$algos[] = $algo; | |
} | |
} | |
self::$algo = end($algos); // sha512 ? | |
} | |
public static function generate_salt($length=56) | |
{ | |
$salt = base64_encode(openssl_random_pseudo_bytes($length)); | |
$actual_length = strlen($salt); | |
$range = $actual_length - $length; | |
return substr($salt, mt_rand(0,$range), $length); | |
} | |
public static function create_hash($pw, $pw_salt) | |
{ | |
if ( !self::$algo ) { | |
self::init(); | |
} | |
return hash_hmac(self::$algo, $pw, $pw_salt); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This has no checks if you have openssl installed on your server, so clearly, use at your own risk =]