Generate, read and verify Crypto::Bcrypt hashes.
NOTE To use Password, you must explicitly import it with require "crypto/bcrypt/password"
require "crypto/bcrypt/password"
password = Crypto::Bcrypt::Password.create("super secret", cost: 10)
# => $2a$10$rI4xRiuAN2fyiKwynO6PPuorfuoM4L2PVv6hlnVJEmNLjqcibAfHq
password.verify("wrong secret") # => false
password.verify("super secret") # => true See Crypto::Bcrypt for hints to select the cost when generating hashes.
Hashes a password.
Loads a bcrypt hash.
Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
Returns the bcrypt hash, suitable for storage and use in Crypto::Bcrypt::Password.new.
Verifies a password against the hash.
Reference
Reference
Reference
Object
Object
Object
Hashes a password.
require "crypto/bcrypt/password"
password = Crypto::Bcrypt::Password.create("super secret", cost: 10)
# => $2a$10$rI4xRiuAN2fyiKwynO6PPuorfuoM4L2PVv6hlnVJEmNLjqcibAfHq Loads a bcrypt hash.
require "crypto/bcrypt/password"
password = Crypto::Bcrypt::Password.new("$2a$10$X6rw/jDiLBuzHV./JjBNXe8/Po4wTL0fhdDNdAdjcKN/Fup8tGCya")
password.version # => "2a"
password.salt # => "X6rw/jDiLBuzHV./JjBNXe"
password.digest # => "8/Po4wTL0fhdDNdAdjcKN/Fup8tGCya" Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32> Returns the bcrypt hash, suitable for storage and use in Crypto::Bcrypt::Password.new.
require "crypto/bcrypt/password"
password = Crypto::Bcrypt::Password.create("super secret")
password.to_s # => "$2a$11$zs8yeubYXMGGJmWyIYdFtO9aOrx44g5rarvixyBfl1klr3dZPG8Ma" Verifies a password against the hash.
require "crypto/bcrypt/password"
password = Crypto::Bcrypt::Password.create("super secret")
password.verify("wrong secret") # => false
password.verify("super secret") # => true
© 2012–2026 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.19.0/Crypto/Bcrypt/Password.html