| 1 | module main |
| 2 | |
| 3 | import rand |
| 4 | import crypto.rand as crypto_rand |
| 5 | import encoding.base64 |
| 6 | |
| 7 | pub const max_safe_unsigned_integer = u32(4_294_967_295) |
| 8 | |
| 9 | pub fn set_rand_crypto_safe_seed() { |
| 10 | first_seed := generate_crypto_safe_int_u32() |
| 11 | second_seed := generate_crypto_safe_int_u32() |
| 12 | |
| 13 | rand.seed([first_seed, second_seed]) |
| 14 | } |
| 15 | |
| 16 | pub fn generate_salt() string { |
| 17 | return rand.i64().str() |
| 18 | } |
| 19 | |
| 20 | // decode_basic_auth parses the `Authorization` header |
| 21 | // returns login and password |
| 22 | pub fn decode_basic_auth(encoded string) (string, string) { |
| 23 | decoded := base64.decode_str(encoded) |
| 24 | auth_parts := decoded.split(':') |
| 25 | |
| 26 | return auth_parts[0], auth_parts[1..].join(':') |
| 27 | } |
| 28 | |
| 29 | fn generate_crypto_safe_int_u32() u32 { |
| 30 | return u32(crypto_rand.int_u64(max_safe_unsigned_integer) or { 0 }) |
| 31 | } |
| 32 | |