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
#![forbid(unsafe_code)]
extern crate uuid;
use sha2::{Digest, Sha256};
use std::fmt;
use std::ops::Deref;
use uuid::Uuid;
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct Token {
key: String,
}
impl Token {
pub fn new(secret: &str) -> Token {
let mut hasher = Sha256::new();
hasher.update(secret);
hasher.update(Uuid::new_v4().to_string().deref());
Token {
key: format!("{:x}", hasher.finalize()),
}
}
pub fn new_from_str(key: &str) -> Token {
Token {
key: key.to_string(),
}
}
}
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.key)
}
}