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
extern crate uuid;
use uuid::Uuid;
pub struct HumanHasher {
words: Wordlist,
}
pub type Wordlist = &'static [&'static str; 256];
const DEFAULT_HUMANIZER: HumanHasher = HumanHasher { words: DEFAULT_WORDLIST };
pub const DEFAULT_WORDLIST: Wordlist = &[
"ack", "alabama", "alanine", "alaska", "alpha", "angel", "apart", "april",
"arizona", "arkansas", "artist", "asparagus", "aspen", "august", "autumn",
"avocado", "bacon", "bakerloo", "batman", "beer", "berlin", "beryllium",
"black", "blossom", "blue", "bluebird", "bravo", "bulldog", "burger",
"butter", "california", "carbon", "cardinal", "carolina", "carpet", "cat",
"ceiling", "charlie", "chicken", "coffee", "cola", "cold", "colorado",
"comet", "connecticut", "crazy", "cup", "dakota", "december", "delaware",
"delta", "diet", "don", "double", "early", "earth", "east", "echo",
"edward", "eight", "eighteen", "eleven", "emma", "enemy", "equal",
"failed", "fanta", "fifteen", "fillet", "finch", "fish", "five", "fix",
"floor", "florida", "football", "four", "fourteen", "foxtrot", "freddie",
"friend", "fruit", "gee", "georgia", "glucose", "golf", "green", "grey",
"hamper", "happy", "harry", "hawaii", "helium", "high", "hot", "hotel",
"hydrogen", "idaho", "illinois", "india", "indigo", "ink", "iowa",
"island", "item", "jersey", "jig", "johnny", "juliet", "july", "jupiter",
"kansas", "kentucky", "kilo", "king", "kitten", "lactose", "lake", "lamp",
"lemon", "leopard", "lima", "lion", "lithium", "london", "louisiana",
"low", "magazine", "magnesium", "maine", "mango", "march", "mars",
"maryland", "massachusetts", "may", "mexico", "michigan", "mike",
"minnesota", "mirror", "mississippi", "missouri", "mobile", "mockingbird",
"monkey", "montana", "moon", "mountain", "muppet", "music", "nebraska",
"neptune", "network", "nevada", "nine", "nineteen", "nitrogen", "north",
"november", "nuts", "october", "ohio", "oklahoma", "one", "orange",
"oranges", "oregon", "oscar", "oven", "oxygen", "papa", "paris", "pasta",
"pennsylvania", "pip", "pizza", "pluto", "potato", "princess", "purple",
"quebec", "queen", "quiet", "red", "river", "robert", "robin", "romeo",
"rugby", "sad", "salami", "saturn", "september", "seven", "seventeen",
"shade", "sierra", "single", "sink", "six", "sixteen", "skylark", "snake",
"social", "sodium", "solar", "south", "spaghetti", "speaker", "spring",
"stairway", "steak", "stream", "summer", "sweet", "table", "tango", "ten",
"tennessee", "tennis", "texas", "thirteen", "three", "timing", "triple",
"twelve", "twenty", "two", "uncle", "undress", "uniform", "uranus", "utah",
"vegan", "venus", "vermont", "victor", "video", "violet", "virginia",
"washington", "west", "whiskey", "white", "william", "winner", "winter",
"wisconsin", "wolfram", "wyoming", "xray", "yankee", "yellow", "zebra",
"zulu" ];
impl HumanHasher {
pub fn new(words: Wordlist) -> HumanHasher {
HumanHasher { words: words }
}
pub fn humanize(&self, uuid: &Uuid, words_out: usize) -> String {
compress(uuid.as_bytes(), words_out)
.iter()
.map(|&x| self.words[x as usize].to_string())
.collect::<Vec<String>>()
.join("-")
}
}
fn compress(bytes: &[u8], target: usize) -> Vec<u8> {
let seg_size = bytes.len() / target;
bytes.chunks(seg_size)
.map(|c| c.iter().fold(0u8, |acc, &x| acc ^ x))
.collect::<Vec<u8>>()
}
pub fn humanize(uuid: &Uuid, words_out: usize) -> String {
DEFAULT_HUMANIZER.humanize(uuid, words_out)
}
#[cfg(test)]
mod tests {
use super::uuid::Uuid;
use super::DEFAULT_WORDLIST;
use super::{humanize, HumanHasher};
const TEST_UUID: &'static str = "bc0f47f93dd046578d7eee645999b95e";
#[test]
fn it_works() {
let tuid = Uuid::parse_str(TEST_UUID).unwrap();
assert_eq!(humanize(&tuid, 4), "august-yankee-lima-coffee");
assert_eq!("pip", humanize(&tuid, 1));
assert_eq!("washington-hot", humanize(&tuid, 2));
assert_eq!("august-yankee-lima-coffee", humanize(&tuid, 4));
assert_eq!("princess-sad-victor-bakerloo-whiskey-mike-saturn-uniform",
humanize(&tuid, 8));
}
#[test]
fn class_works() {
let tuid = Uuid::parse_str(TEST_UUID).unwrap();
let hzr = HumanHasher::new(&DEFAULT_WORDLIST);
assert_eq!(humanize(&tuid, 4), hzr.humanize(&tuid, 4));
}
}