Search code examples
bcryptcrystal-lang

Trying to use Crystal's Bycrypt library - "invalid salt size"


I'm trying to hash + salt user passwords with the Bcrypt library that ships with Crystal.

The following code produces an "Invalid salt size" error, when run in a playground.

require "crypto/bcrypt"

user = "Jones"
pass = "password"

temp = Crypto::Bcrypt.new(pass, user)

Relevant source code


Solution

  • Use the Crypto::Bcrypt::Password API, don't directly use Crypto::Bcrypt.

    You don't use the username in the BCrypt hash generation, the API will use a random value as the salt.

    bcryptHash = Crypto::Bcrypt::Password.create("password123")
    

    See https://crystal-lang.org/api/master/Crypto/Bcrypt/Password.html