Hash passwords and encrypt strings with janet
Add to your project.janet
file
{:dependencies ["https://github.com/joy-framework/cipher"]}
Libhydrogen requires a master key for all hashing/encryption functions, so the first step is to generate it
(import cipher)
(def key (cipher/password-key))
Now we can use that key when hashing a password
(def hashed-password (cipher/hash-password key "correct horse battery staple"))
This returns a garbled string that represents the password. Then to verify a plaintext password you call this
(cipher/verify-password key hashed-password "correct horse battery staple")
This returns either true or false.
You can also encrypt strings!
(let [key (cipher/encryption-key)]
(as-> "hello" ?
(cipher/encrypt key ?)
(cipher/decrypt key ?))) => "hello"
(cipher/hash "hello") => "7fde0696c922ba4e1348dd7fa32f957531b861a93d78eef120a6c1a62fa3d2df"
Run jpm test
from the project folder.