aboutsummaryrefslogtreecommitdiff
path: root/lockchain-core/src/users/tokens.rs
blob: e6e4854fd51cad3e0bc65ca8d516de3355f7b97d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use crypto::random;

const TOK_SIZE: usize = 64;

/// An authentication token that can be compared in constant time
/// 
/// ```
/// 
/// use lockchain_core::users::auth::Token;
/// let t1 = Token::new();
/// let t2 = Token::new();
/// 
/// // Will fail, but no expose failure length
/// assert_eq!(t1, t2);
/// ```
pub struct Token {
    tok: [u8; TOK_SIZE],
}

impl Token {
    pub fn new() -> Self {
        let v = random::bytes(TOK_SIZE);
        let mut tok = [0; TOK_SIZE];
        tok.copy_from_slice(v.as_slice());

        Self { tok }
    }
}

impl PartialEq for Token {
    fn eq(&self, other: &Self) -> bool {
        let mut ret = true;
        for i in 0..TOK_SIZE {
            if self.tok[i] != other.tok[i] {
                ret = false;
            }
        }
        ret
    }
}

impl Eq for Token {}