File: polymorphic.rs

package info (click to toggle)
rust-nettle 7.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,456 kB
  • sloc: makefile: 4
file content (188 lines) | stat: -rw-r--r-- 4,212 bytes parent folder | download | duplicates (3)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
extern crate nettle;

use nettle::aead::{Eax, Gcm};
use nettle::cipher::{Aes128, Twofish};
use nettle::hash::{Hash, Sha224, Sha256};
use nettle::mac::Hmac;
use nettle::mode::{Cbc, Cfb};
use nettle::aead::Aead;
use nettle::mac::Mac;
use nettle::mode::Mode;
use nettle::random::{Random, Yarrow};

struct Fubar {
    foo: Foo,
    bar: Bar,
    baz: Baz,
}

impl Default for Fubar {
    fn default() -> Fubar {
        Fubar { foo: Foo::default(), bar: Bar::default(), baz: Baz::default() }
    }
}

impl Fubar {
    pub fn produce_hash(&self) -> Vec<u8> {
        let mut h = self.init_hash();

        self.foo.hash(&mut h);
        self.bar.hash(&mut h);
        self.baz.hash(&mut h);

        let mut g = h.clone();
        self.foo.hash(&mut g);
        self.bar.hash(&mut g);
        self.baz.hash(&mut g);

        let mut ret = vec![0u8; h.digest_size()];
        h.digest(&mut ret);

        ret
    }

    pub fn produce_mac(&self) -> Vec<u8> {
        let mut h = self.init_mac();

        self.foo.mac(&mut h);
        self.bar.mac(&mut h);
        self.baz.mac(&mut h);

        let mut ret = vec![0u8; h.mac_size()];
        h.digest(&mut ret).unwrap();

        ret
    }

    fn init_hash(&self) -> Box<dyn Hash> {
        if random_bool() {
            Box::new(Sha224::default())
        } else {
            Box::new(Sha256::default())
        }
    }

    fn init_mac(&self) -> Box<dyn Mac> {
        if random_bool() {
            Box::new(Hmac::<Sha224>::with_key(&b"123"[..]))
        } else {
            Box::new(Hmac::<Sha256>::with_key(&b"123"[..]))
        }
    }

    fn init_cipher(&self) -> Box<dyn Mode> {
        if random_bool() {
            Box::new(Cbc::<Aes128>::with_encrypt_key(&b"123"[..]).unwrap())
        } else {
            Box::new(Cfb::<Twofish>::with_encrypt_key(&b"123"[..]).unwrap())
        }
    }

    fn init_aead(&self) -> Box<dyn Aead> {
        if random_bool() {
            Box::new(
                Gcm::<Aes128>::with_key_and_nonce(&b"123"[..], &b"123"[..])
                    .unwrap(),
            )
        } else {
            Box::new(
                Eax::<Twofish>::with_key_and_nonce(&b"123"[..], &b"123"[..])
                    .unwrap(),
            )
        }
    }
}

struct Foo {
    num_foos: usize,
}

impl Default for Foo {
    fn default() -> Foo {
        Foo { num_foos: 42 }
    }
}

impl Foo {
    pub fn hash<H: AsMut<dyn Hash>>(&self, h: &mut H) {
        h.as_mut().update(format!("{}", self.num_foos).as_bytes());
    }

    pub fn mac<H: AsMut<dyn Mac>>(&self, h: &mut H) {
        h.as_mut().update(format!("{}", self.num_foos).as_bytes());
    }
}

struct Bar {
    bar: &'static str,
}

impl Default for Bar {
    fn default() -> Bar {
        Bar { bar: "Hallo, I bims bar vong foo her" }
    }
}

impl Bar {
    pub fn hash<H: AsMut<dyn Hash>>(&self, h: &mut H) {
        h.as_mut().update(self.bar.as_bytes());
    }

    pub fn mac<H: AsMut<dyn Mac>>(&self, h: &mut H) {
        h.as_mut().update(self.bar.as_bytes());
    }
}

enum Baz {
    One,
    Two,
    Three,
}

impl Default for Baz {
    fn default() -> Baz {
        if random_bool() {
            if random_bool() {
                Baz::One
            } else {
                Baz::Two
            }
        } else {
            Baz::Three
        }
    }
}

impl Baz {
    pub fn hash<H: AsMut<dyn Hash>>(&self, h: &mut H) {
        match self {
            &Baz::One => h.as_mut().update(&b"Hello"[..]),
            &Baz::Two => h.as_mut().update(&b"Hallo"[..]),
            &Baz::Three => h.as_mut().update("こにちわ".as_bytes()),
        }
    }

    pub fn mac<H: AsMut<dyn Mac>>(&self, h: &mut H) {
        match self {
            &Baz::One => h.as_mut().update(&b"Hello"[..]),
            &Baz::Two => h.as_mut().update(&b"Hallo"[..]),
            &Baz::Three => h.as_mut().update("こにちわ".as_bytes()),
        }
    }
}

fn main() {
    let fubar = Fubar::default();

    fubar.init_aead();
    fubar.init_cipher();

    println!("Fubar hash: {:?}", fubar.produce_hash());
    println!("Fubar mac: {:?}", fubar.produce_mac());
}

fn random_bool() -> bool {
    let mut b = [0];
    Yarrow::default().random(&mut b);
    b[0] > 127
}