Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
REMOVED.
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Ecko" data-source="post: 260317" data-attributes="member: 24874"><p>note u should be using php 5.3 or above</p><p>[php]</p><p>class bcrypt {</p><p></p><p> private static $algorithm = '$2y$';</p><p> private static $cost = '10';</p><p> // check if bcrypt() is usable</p><p> public static function isAvailable() {</p><p> return defined('CRYPT_BLOWFISH') && CRYPT_BLOWFISH;</p><p> }</p><p></p><p> public static function salt() {</p><p> if (function_exists('openssl_random_pseudo_bytes'))</p><p> return substr(strtr(base64_encode(openssl_random_pseudo_bytes(22)), '+', '.'), 0, 22);</p><p> else</p><p> return substr(strtr(base64_encode(mt_rand()), '+', '.'), 0, 22);</p><p> }</p><p></p><p> // let's hash</p><p> public static function hash($password) {</p><p> // bcrypt algorithm depending on PHP version</p><p> if (version_compare(PHP_VERSION, '5.3.7', '<'))</p><p> self::$algorithm = '$2a$';</p><p></p><p> return crypt($password, self::$algorithm . self::$cost . '$' . self::salt());</p><p> }</p><p></p><p> // check password against hash</p><p> public static function checkPassword($hash, $password) {</p><p> $new_hash = crypt($password, $hash);</p><p> return ($hash == $new_hash);</p><p> }</p><p>}[/php]</p><p></p><p>now check $password (based on $_POST input):</p><p>[php]</p><p>if (bcrypt::checkPassword($hashFromDatabase, $password))</p><p> echo 'Password correct';</p><p>else</p><p> echo 'Password incorrect';[/php]</p></blockquote><p></p>
[QUOTE="Ecko, post: 260317, member: 24874"] note u should be using php 5.3 or above [php] class bcrypt { private static $algorithm = '$2y$'; private static $cost = '10'; // check if bcrypt() is usable public static function isAvailable() { return defined('CRYPT_BLOWFISH') && CRYPT_BLOWFISH; } public static function salt() { if (function_exists('openssl_random_pseudo_bytes')) return substr(strtr(base64_encode(openssl_random_pseudo_bytes(22)), '+', '.'), 0, 22); else return substr(strtr(base64_encode(mt_rand()), '+', '.'), 0, 22); } // let's hash public static function hash($password) { // bcrypt algorithm depending on PHP version if (version_compare(PHP_VERSION, '5.3.7', '<')) self::$algorithm = '$2a$'; return crypt($password, self::$algorithm . self::$cost . '$' . self::salt()); } // check password against hash public static function checkPassword($hash, $password) { $new_hash = crypt($password, $hash); return ($hash == $new_hash); } }[/php] now check $password (based on $_POST input): [php] if (bcrypt::checkPassword($hashFromDatabase, $password)) echo 'Password correct'; else echo 'Password incorrect';[/php] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
REMOVED.
Top