in
Clone
https://git.tahinli.com/Tahinli/in.git
0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits
| 1 | //! Random tokens, readable codes and constant-time comparison. Tokens are shown to the user once and |
| 2 | //! stored only as their SHA-256 hash, so a leaked database does not reveal a usable token. |
| 3 | |
| 4 | use ; |
| 5 | use ; |
| 6 | use ConstantTimeEq; |
| 7 | |
| 8 | /// Alphabet for codes that people type: lowercase letters and the digits 2 to 9, without look-alikes |
| 9 | /// such as `l`, `o`, `0` and `1`. It has exactly 32 symbols, so the low five bits of a random byte |
| 10 | /// pick one with no bias. |
| 11 | pub const READABLE_ALPHABET: & = b"abcdefghijkmnpqrstuvwxyz23456789"; |
| 12 | |
| 13 | /// A session or share token: 32 random bytes, base64url without padding (43 characters). |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | /// `len` random characters from [`READABLE_ALPHABET`]. |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | /// Lowercase hex SHA-256 of `token`. This is what the database stores. |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | /// Compares two byte strings in time that does not depend on where they first differ. Different |
| 35 | /// lengths are unequal; the length itself is not hidden. |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 |