im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits
| 1 | //! Random tokens, codes, and the hashing and comparison rules that go with them. |
| 2 | //! |
| 3 | //! A token is 32 random bytes in base64url. Only its SHA-256 (hex) is stored, so a database leak |
| 4 | //! does not reveal live tokens. Codes typed by people use a readable alphabet with no look-alike |
| 5 | //! characters. |
| 6 | |
| 7 | use ; |
| 8 | use ; |
| 9 | use ConstantTimeEq; |
| 10 | |
| 11 | /// Alphabet for codes typed by people: lowercase letters and the digits 2 to 9, without `l`, `o`, |
| 12 | /// `0` and `1`, which are easy to confuse. It has exactly 32 symbols, so the low five bits of a |
| 13 | /// random byte pick one uniformly, with no modulo bias. |
| 14 | pub const READABLE_ALPHABET: & = b"abcdefghijkmnpqrstuvwxyz23456789"; |
| 15 | |
| 16 | /// A fresh token: 32 random bytes, base64url without padding (43 characters). |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | /// `len` random characters from [`READABLE_ALPHABET`]. |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | /// The hex SHA-256 of `token`. This is what the database stores in place of the token. |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | /// Compares two byte strings in time that does not depend on where they first differ. |
| 36 | /// |
| 37 | /// Different lengths return false at once; the length of a secret is not itself secret here, |
| 38 | /// because every token of a kind has the same length. |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | /// 32 random bytes from the thread RNG, which is seeded from the operating system. It cannot fail, |
| 44 | /// so callers need no error path. |
| 45 | pub |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 |