Skip to content

Commit

Permalink
Add API docs for Crypto::Bcrypt.new
Browse files Browse the repository at this point in the history
  • Loading branch information
j8r committed Nov 15, 2020
1 parent 3c44f94 commit d499863
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/crypto/bcrypt.cr
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,28 @@ class Crypto::Bcrypt
0x64657253, 0x63727944, 0x6f756274,
)

# Hashes a password using Bcrypt.
#
# ```
# require "crypto/bcrypt"
#
# Crypto::Bcrypt.hash_secret "secret"
# ```
def self.hash_secret(password, cost = DEFAULT_COST) : String
# We make a clone here to we don't keep a mutable reference to the original string
passwordb = password.to_unsafe.to_slice(password.bytesize + 1).clone # include leading 0
saltb = Random::Secure.random_bytes(SALT_SIZE)
new(passwordb, saltb, cost).to_s
end

# Creates a new Bcrypt object from a string password and a salt.
#
# ```
# require "crypto/bcrypt"
#
# password = Crypto::Bcrypt.new "secret", "salt_of_16_chars"
# password.digest
# ```
def self.new(password : String, salt : String, cost = DEFAULT_COST)
# We make a clone here to we don't keep a mutable reference to the original string
passwordb = password.to_unsafe.to_slice(password.bytesize + 1).clone # include leading 0
Expand All @@ -65,6 +80,14 @@ class Crypto::Bcrypt
getter salt : Bytes
getter cost : Int32

# Creates a new Bcrypt object from a password and a salt in bytes.
#
# ```
# require "crypto/bcrypt"
#
# password = Crypto::Bcrypt.new "secret".to_slice, "salt_of_16_chars".to_slice
# password.digest
# ```
def initialize(@password : Bytes, @salt : Bytes, @cost = DEFAULT_COST)
raise Error.new("Invalid cost") unless COST_RANGE.includes?(cost)
raise Error.new("Invalid salt size") unless salt.size == SALT_SIZE
Expand Down

0 comments on commit d499863

Please sign in to comment.