Last active
March 8, 2018 17:42
-
-
Save srghma/34b25ff44acd7ce09c8c55ae8c2d672c to your computer and use it in GitHub Desktop.
bcrypt examples
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
bcrypt examples in ruby and php |
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 | |
$secret = 'foo'; | |
$cost = 12; | |
# random, example: $2y$12$SPvMCI9j6slqFbCJkI4xYe1VR7M0SYkv2g7TMaaJx/W94Dz/cuTJa | |
$hashedPassword = password_hash($secretToken, PASSWORD_BCRYPT, ['cost' => $cost]); | |
echo password_verify($secret, $hashedPassword); # => 1 | |
# its also possible to use ruby hashed in php, just replace 2a with 2y | |
# more - https://stackoverflow.com/questions/20980859/using-bcrypt-ruby-to-validate-hashed-passwords-using-version-2y | |
$rubyHashedPassword = '$2a$12$UliIe73CrjZ0gQvfLpxRSeMEz93iap.6VxtZNK6lw9OIAVmB7ARfm' | |
$hashedPassword = preg_replace('/$\$2a\$/', '$2y$', $rubyHashedPassword); | |
echo password_verify($secret, $hashedPassword); # => 1 |
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
require 'bcrypt' | |
secret = 'foo' | |
cost = 12 | |
# outputs random string | |
# example "$2a$12$UliIe73CrjZ0gQvfLpxRSeMEz93iap.6VxtZNK6lw9OIAVmB7ARfm" | |
hashed_password = ::BCrypt::Password.create(secret, cost: cost).to_s | |
password = ::BCrypt::Password.new(hashed_password) | |
password == secret # => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment