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
use sha2::Sha256;
use sha2::Digest;
use std::io::Write;
use byteorder::{LittleEndian, WriteBytesExt};
use paste::paste;
pub trait Hasher {
type Output;
fn update<D: AsRef<[u8]>>(&mut self, data: D);
fn finish(self) -> Self::Output;
fn digest<H: Hashable>(data: &H) -> Self::Output;
}
pub trait Hashable {
fn update<H: Hasher>(&self, h: &mut H);
fn digest<H: Hasher>(&self) -> <H as Hasher>::Output where Self: Sized {
H::digest(self)
}
}
impl Hasher for Sha256 {
type Output = [u8; 32];
fn update<D: AsRef<[u8]>>(&mut self, data: D) {
Digest::update(self, data);
}
fn finish(self) -> Self::Output {
let res = self.finalize();
let mut out = [0; 32];
for i in 0..res.len() {
out[i] = res[i];
}
out
}
fn digest<H: Hashable>(data: &H) -> Self::Output {
let mut sha = Sha256::new();
data.update(&mut sha);
sha.finish()
}
}
impl Hashable for u8 {
fn update<H: Hasher>(&self, h: &mut H) {
let mut buf = [0u8; std::mem::size_of::<u8>()];
let mut b = &mut buf[..];
b.write_u8(*self).unwrap();
h.update(buf.as_slice());
}
}
impl Hashable for bool {
fn update<H: Hasher>(&self, h: &mut H) {
let mut buf = [0u8; std::mem::size_of::<u8>()];
let mut b = &mut buf[..];
b.write_u8(*self as u8).unwrap();
h.update(buf.as_slice());
}
}
impl Hashable for i8 {
fn update<H: Hasher>(&self, h: &mut H) {
let mut buf = [0u8; std::mem::size_of::<u8>()];
let mut b = &mut buf[..];
b.write_i8(*self).unwrap();
h.update(buf.as_slice());
}
}
macro_rules! impl_hashable_for {
($t:ty) => {
impl crate::Hashable for $t {
fn update<H: Hasher>(&self, h: &mut H) {
let mut buf = [0u8; std::mem::size_of::<$t>()];
let mut b = &mut buf[..];
paste! {
b.[<write_ $t>]::<LittleEndian>(*self).unwrap();
}
h.update(buf.as_slice());
}
}
};
}
impl_hashable_for!(i16);
impl_hashable_for!(u16);
impl_hashable_for!(i32);
impl_hashable_for!(u32);
impl_hashable_for!(i64);
impl_hashable_for!(u64);
impl<T: Hashable> Hashable for Vec<T> {
fn update<H: Hasher>(&self, h: &mut H) {
for t in self {
t.update(h);
}
}
}
impl Hashable for String {
fn update<H: Hasher>(&self, h: &mut H) {
for t in self.as_bytes() {
t.update(h);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate as simple_hash;
#[derive(Hashable)]
struct Foo {
a: u8,
b: u16,
c: Vec<u32>,
}
#[test]
fn test_u8() {
let res = (9u8).digest::<sha2::Sha256>();
assert_eq!(res, hex_literal::hex!("2b4c342f5433ebe591a1da77e013d1b72475562d48578dca8b84bac6651c3cb9"));
}
#[test]
fn test_derive() {
let foo = Foo {
a: 8,
b: 99,
c: vec![0,1,2,3],
};
let res = foo.digest::<sha2::Sha256>();
assert_eq!(res, hex_literal::hex!("929863ce588951eae0cc88755216f96951d431e7d15adbb836d8f1960bb65a9d"));
}
}