packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | /// Argon2id password hashing (RFC 9106), from the reference C |
| 2 | /// implementation linked as a native dependency (ADR 0005). |
| 3 | /// |
| 4 | /// import argon2 |
| 5 | /// |
| 6 | /// let stored = argon2.hash_password("hunter2", argon2.default_params()) |
| 7 | /// argon2.verify("hunter2", stored) // Ok(True) |
| 8 | /// |
| 9 | /// Hashing and verifying run on the `cpu` blocking pool: the calling actor |
| 10 | /// waits, others keep running. At most `LOUISE_BLOCKING_CPU_THREADS` hashes |
| 11 | /// run at once (each holding `m_cost_kib` of memory), and |
| 12 | /// `runtime.blocking_stats()` tells how many wait. |
| 13 | |
| 14 | import std/random.{type random} |
| 15 | import std/string |
| 16 | import std/test.{assert} |
| 17 | |
| 18 | /// Cost parameters: passes over memory, memory in KiB, lanes. |
| 19 | pub type Params { Params(t_cost: Int, m_cost_kib: Int, parallelism: Int) } |
| 20 | |
| 21 | /// RFC 9106's second recommended option: 3 passes over 64 MiB. |
| 22 | pub fn default_params :: -> Params |
| 23 | { |
| 24 | Params(t_cost: 3, m_cost_kib: 65536, parallelism: 1) |
| 25 | } |
| 26 | |
| 27 | /// Hash `password` with a fresh random 16-byte salt (from the `random` |
| 28 | /// effect); the result is the PHC string |
| 29 | /// (`$argon2id$v=19$m=...,t=...,p=...$salt$hash`) to store. |
| 30 | pub fn hash_password :: |
| 31 | password: String |
| 32 | params: Params |
| 33 | -> Result<String, String> / <ffi, random> |
| 34 | { |
| 35 | hash password (random.token 16) params |
| 36 | } |
| 37 | |
| 38 | /// Hash `password` with `salt` (at least 8 bytes). |
| 39 | pub fn hash :: |
| 40 | password: String |
| 41 | salt: String |
| 42 | params: Params |
| 43 | -> Result<String, String> / <ffi> |
| 44 | { |
| 45 | hash_raw password salt params.t_cost params.m_cost_kib params.parallelism |
| 46 | } |
| 47 | |
| 48 | @test |
| 49 | fn each_parameter_reaches_the_c_library :: -> () / <ffi> |
| 50 | { |
| 51 | // Three different values, so that two swapped arguments would show. |
| 52 | match hash "pw" "saltsalt" Params(t_cost: 2, m_cost_kib: 96, parallelism: 3) |
| 53 | { Ok(stored) -> |
| 54 | assert (string.starts_with stored "$argon2id$v=19$m=96,t=2,p=3$") stored |
| 55 | , Error(e) -> panic e |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// `Ok(True)` if `password` matches `encoded`, a string made by `hash`, |
| 60 | /// `Ok(False)` if it does not, or an `Error` if `encoded` is not an |
| 61 | /// Argon2id hash this package can check (for example, a corrupted one). |
| 62 | pub fn verify :: |
| 63 | password: String |
| 64 | encoded: String |
| 65 | -> Result<Bool, String> / <ffi> |
| 66 | { |
| 67 | verify_raw encoded password |
| 68 | } |
| 69 | |
| 70 | /// Hash `password` keyed with `secret` (RFC 9106's secret value `K`, a |
| 71 | /// pepper kept out of the database) and a fresh random 16-byte salt. The |
| 72 | /// PHC string does not record the secret: verify it with `verify_keyed` |
| 73 | /// and the same secret, never with `verify`. |
| 74 | pub fn hash_password_keyed :: |
| 75 | password: String |
| 76 | secret: String |
| 77 | params: Params |
| 78 | -> Result<String, String> / <ffi, random> |
| 79 | { |
| 80 | hash_keyed password secret (random.token 16) params |
| 81 | } |
| 82 | |
| 83 | /// Hash `password` keyed with `secret`, with `salt` (at least 8 bytes). |
| 84 | /// The secret's bytes are the key. |
| 85 | pub fn hash_keyed :: |
| 86 | password: String |
| 87 | secret: String |
| 88 | salt: String |
| 89 | params: Params |
| 90 | -> Result<String, String> / <ffi> |
| 91 | { |
| 92 | hash_keyed_raw |
| 93 | password |
| 94 | secret |
| 95 | "" |
| 96 | salt |
| 97 | params.t_cost |
| 98 | params.m_cost_kib |
| 99 | params.parallelism |
| 100 | } |
| 101 | |
| 102 | /// [`verify`] for a hash made by `hash_keyed`: `Ok(True)` only with the |
| 103 | /// same password and secret. An unkeyed hash verifies with an empty secret. |
| 104 | pub fn verify_keyed :: |
| 105 | password: String |
| 106 | secret: String |
| 107 | encoded: String |
| 108 | -> Result<Bool, String> / <ffi> |
| 109 | { |
| 110 | verify_keyed_raw encoded password secret |
| 111 | } |
| 112 | |
| 113 | // RFC 9106 §5.3, Argon2id: the secret and the associated data both count. |
| 114 | @test |
| 115 | fn keyed_hashing_matches_rfc_9106 :: -> () / <ffi> |
| 116 | { |
| 117 | match hash_keyed_raw |
| 118 | (string.repeat "\u{1}" 32) |
| 119 | (string.repeat "\u{3}" 8) |
| 120 | (string.repeat "\u{4}" 12) |
| 121 | (string.repeat "\u{2}" 16) |
| 122 | 3 |
| 123 | 32 |
| 124 | 4 |
| 125 | { Ok(stored) -> |
| 126 | assert |
| 127 | ( stored |
| 128 | == "$argon2id$v=19$m=32,t=3,p=4$AgICAgICAgICAgICAgICAg$DWQN9Y14dmwIwDejSotTydAe8EUtdbZetSUg6WsB5lk" |
| 129 | ) |
| 130 | stored |
| 131 | , Error(e) -> panic e |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @extern c "louise_argon2_hash" (blocking cpu) |
| 136 | fn hash_raw :: |
| 137 | password: String |
| 138 | salt: String |
| 139 | t_cost: Int |
| 140 | m_cost: Int |
| 141 | parallelism: Int |
| 142 | -> Result<String, String> / <ffi> |
| 143 | |
| 144 | @extern c "louise_argon2_verify" (blocking cpu) |
| 145 | fn verify_raw :: |
| 146 | encoded: String |
| 147 | password: String |
| 148 | -> Result<Bool, String> / <ffi> |
| 149 | |
| 150 | @extern c "louise_argon2_hash_keyed" (blocking cpu) |
| 151 | fn hash_keyed_raw :: |
| 152 | password: String |
| 153 | secret: String |
| 154 | ad: String |
| 155 | salt: String |
| 156 | t_cost: Int |
| 157 | m_cost: Int |
| 158 | parallelism: Int |
| 159 | -> Result<String, String> / <ffi> |
| 160 | |
| 161 | @extern c "louise_argon2_verify_keyed" (blocking cpu) |
| 162 | fn verify_keyed_raw :: |
| 163 | encoded: String |
| 164 | password: String |
| 165 | secret: String |
| 166 | -> Result<Bool, String> / <ffi> |