author | alfadur |
Sat, 22 Feb 2025 19:39:31 +0300 | |
changeset 16120 | 5febd2bc5372 |
permissions | -rw-r--r-- |
16120
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
1 |
use rand::{thread_rng, RngCore}; |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
2 |
use std::fmt::{Formatter, LowerHex}; |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
3 |
|
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
4 |
#[derive(PartialEq, Debug)] |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
5 |
pub struct Sha1Digest([u8; 20]); |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
6 |
|
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
7 |
impl Sha1Digest { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
8 |
pub fn new(digest: [u8; 20]) -> Self { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
9 |
Self(digest) |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
10 |
} |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
11 |
|
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
12 |
pub fn random() -> Self { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
13 |
let mut result = Sha1Digest(Default::default()); |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
14 |
thread_rng().fill_bytes(&mut result.0); |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
15 |
result |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
16 |
} |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
17 |
} |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
18 |
|
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
19 |
impl LowerHex for Sha1Digest { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
20 |
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
21 |
for byte in &self.0 { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
22 |
write!(f, "{:02x}", byte)?; |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
23 |
} |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
24 |
Ok(()) |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
25 |
} |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
26 |
} |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
27 |
|
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
28 |
impl PartialEq<&str> for Sha1Digest { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
29 |
fn eq(&self, other: &&str) -> bool { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
30 |
if other.len() != self.0.len() * 2 { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
31 |
false |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
32 |
} else { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
33 |
#[inline] |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
34 |
fn convert(c: u8) -> u8 { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
35 |
if c > b'9' { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
36 |
c.wrapping_sub(b'a').saturating_add(10) |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
37 |
} else { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
38 |
c.wrapping_sub(b'0') |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
39 |
} |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
40 |
} |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
41 |
|
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
42 |
other |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
43 |
.as_bytes() |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
44 |
.chunks_exact(2) |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
45 |
.zip(&self.0) |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
46 |
.all(|(chars, byte)| { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
47 |
if let [hi, lo] = chars { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
48 |
convert(*lo) == byte & 0x0f && convert(*hi) == (byte & 0xf0) >> 4 |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
49 |
} else { |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
50 |
unreachable!() |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
51 |
} |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
52 |
}) |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
53 |
} |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
54 |
} |
5febd2bc5372
Add server reconnection tokens and anteroom local list of used nicks
alfadur
parents:
diff
changeset
|
55 |
} |