Skip to content

Instantly share code, notes, and snippets.

@rohjay
Last active March 27, 2019 17:58
Show Gist options
  • Save rohjay/932111db1d1442d74ff0 to your computer and use it in GitHub Desktop.
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 =]
<?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);
}
}
@rohjay
Copy link
Author

rohjay commented Mar 27, 2019

This has no checks if you have openssl installed on your server, so clearly, use at your own risk =]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment