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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
extern crate base64;
extern crate byteorder;
extern crate chrono;
extern crate rand;
#[macro_use]
extern crate serde_derive;
use byteorder::{LittleEndian, WriteBytesExt};
use rand::RngCore;
use rand::rngs::OsRng;
use std::fmt;
use std::io::Cursor;
use std::ops::Deref;
#[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
pub struct TextNonce(pub String);
impl TextNonce {
pub fn new() -> TextNonce {
TextNonce::sized(32).unwrap()
}
pub fn sized(length: usize) -> Result<TextNonce, String> {
TextNonce::sized_configured(length, base64::STANDARD)
}
pub fn sized_urlsafe(length: usize) -> Result<TextNonce, String> {
TextNonce::sized_configured(length, base64::URL_SAFE)
}
pub fn sized_configured(length: usize, config: base64::Config) -> Result<TextNonce, String> {
if length < 16 {
return Err("length must be >= 16".to_owned());
}
if length % 4 != 0 {
return Err("length must be divisible by 4".to_owned());
}
let bytelength: usize = (length / 4) * 3;
let mut raw: Vec<u8> = Vec::with_capacity(bytelength);
unsafe {
raw.set_len(bytelength);
}
{
let now = chrono::Utc::now();
let secs: i64 = now.timestamp();
let nsecs: u32 = now.timestamp_subsec_nanos();
let mut cursor = Cursor::new(&mut *raw);
cursor.write_u32::<LittleEndian>(nsecs).unwrap();
cursor.write_i64::<LittleEndian>(secs).unwrap();
}
match OsRng::new() {
Ok(mut g) => g.fill_bytes(&mut raw[12..bytelength]),
Err(_) => ::rand::thread_rng().fill_bytes(&mut raw[12..bytelength]),
};
Ok(TextNonce(base64::encode_config(&raw, config)))
}
pub fn into_string(self) -> String {
let TextNonce(s) = self;
s
}
}
impl fmt::Display for TextNonce {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl Deref for TextNonce {
type Target = str;
fn deref(&self) -> &str {
&*self.0
}
}
#[cfg(test)]
mod tests {
use super::TextNonce;
use std::collections::HashSet;
extern crate bincode;
#[test]
fn new() {
let mut map = HashSet::new();
for _ in 0..100 {
let n = TextNonce::new();
let TextNonce(s) = n;
assert_eq!(s.len(), 32);
assert_eq!(
s.chars()
.filter(|x| x.is_digit(10) || x.is_alphabetic() || *x == '+' || *x == '/')
.count(),
32
);
map.insert(s);
}
assert_eq!(map.len(), 100);
}
#[test]
fn sized() {
let n = TextNonce::sized(48);
assert!(n.is_ok());
let TextNonce(s) = n.unwrap();
assert_eq!(s.len(), 48);
let n = TextNonce::sized(47);
assert!(n.is_err());
let n = TextNonce::sized(12);
assert!(n.is_err());
}
#[test]
fn serde() {
let n = TextNonce::sized(48);
let serialized = bincode::serialize(&n).unwrap();
let deserialized = bincode::deserialize(&serialized).unwrap();
assert_eq!(n, deserialized);
}
}