Skip to content

Instantly share code, notes, and snippets.

@srghma
Last active March 8, 2018 17:42
Show Gist options
  • Save srghma/34b25ff44acd7ce09c8c55ae8c2d672c to your computer and use it in GitHub Desktop.
Save srghma/34b25ff44acd7ce09c8c55ae8c2d672c to your computer and use it in GitHub Desktop.
bcrypt examples
bcrypt examples in ruby and php
<?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
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