-
Notifications
You must be signed in to change notification settings - Fork 0
/
beecrypt.gleam
32 lines (25 loc) 路 868 Bytes
/
beecrypt.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import gleam/erlang/charlist.{Charlist}
import gleam/crypto
import gleam/string
pub fn hash(password: String) -> String {
let salt = generate_salt()
hash_with_salt(password, salt)
}
pub fn verify(password: String, hash: String) -> Bool {
let salt = string.slice(hash, at_index: 0, length: 29)
let hashed = hash_with_salt(password, salt)
crypto.secure_compare(<<hash:utf8>>, <<hashed:utf8>>)
}
type BcrypeErlangError
fn generate_salt() -> String {
let assert Ok(salt) = gen_salt()
charlist.to_string(salt)
}
fn hash_with_salt(password: String, salt: String) -> String {
let assert Ok(hash) = hashpw(password, salt)
charlist.to_string(hash)
}
@external(erlang, "bcrypt", "gen_salt")
fn gen_salt() -> Result(Charlist, BcrypeErlangError)
@external(erlang, "bcrypt", "hashpw")
fn hashpw(a: String, b: String) -> Result(Charlist, BcrypeErlangError)