on c19b089 help sign in
268 lines 5.1 kB history raw
1+++
2title = "2e: Insecure Hashing"
3weight = 8
4template = "doc.html"
5+++
7### `++fnv`
9FNV scrambler
11Hashes an atom with the 32-bit FNV non-cryptographic hash algorithm. Multiplies
12`a` by the prime number 16,777,619 and then takes the [block](@/docs/hoon/reference/stdlib/2c.md) of size
135 off the product's end, producing an atom.
15#### Accepts
17`a` is an atom.
19#### Produces
21An atom.
23#### Source
25```hoon
26 ++ fnv |=(a=@ (end 5 1 (mul 16.777.619 a)))
27```
29#### Examples
31```
32 > (fnv 10.000)
33 272.465.456
35 > (fnv 10.001)
36 289.243.075
38 > (fnv 1)
39 16.777.619
40```
43---
44### `++mug` {#mug}
46murmur3 scrambler
48Hashes `a` with the 31-bit murmur3 non-cryptographic hash algorithm,
49producing an atom.
51#### Accepts
53`a` is a noun.
55#### Produces
57An atom.
59#### Source
61```hoon
62++ mug :: mug with murmur3
63 ~/ %mug
64 |= a/*
65 |^ (trim ?@(a a (mix $(a -.a) (mix 0x7fff.ffff $(a +.a)))))
66 ++ trim :: 31-bit nonzero
67 |= key/@
68 =+ syd=0xcafe.babe
69 |- ^- @
70 =+ haz=(muk syd (met 3 key) key)
71 =+ ham=(mix (rsh 0 31 haz) (end 0 31 haz))
72 ?.(=(0 ham) ham $(syd +(syd)))
73 --
74```
76#### Examples
78```
79 > (mug 10.000)
80 795.713.195
82 > (mug 10.001)
83 420.521.697
85 > (mug 1)
86 1.901.865.568
88 > (mug (some 10))
89 2.099.076.946
91 > (mug [1 2 3 4 5 ~])
92 1.719.833.228
93```
96---
97### `++muk`
99Standard MurmurHash3
101Implementation of the 32-bit
102[MurmurHash3](https://en.wikipedia.org/wiki/MurmurHash#Algorithm)
103non-cryptographic hash algorithm. Takes `a` as the seed, `b` as the key length
104in [blocks](@/docs/hoon/reference/stdlib/2c.md) of size 3, and `c` as the key, producing an atom.
106#### Accepts
108`a` is an atom.
110`b` is an atom.
112`c` is an atom.
114#### Produces
116An atom.
118#### Source
120```hoon
121 ++ muk
122 ~% %muk ..muk ~
123 =+ ~(. fe 5)
124 |= [syd=@ len=@ key=@]
125 ?> &((lte (met 5 syd) 1) (lte (met 0 len) 31))
126 =/ pad (sub len (met 3 key))
127 =/ data (weld (rip 3 key) (reap pad 0))
128 =/ nblocks (div len 4) :: intentionally off-by-one
129 =/ h1 syd
130 =+ [c1=0xcc9e.2d51 c2=0x1b87.3593]
131 =/ blocks (rip 5 key)
132 =/ i nblocks
133 =. h1 =/ hi h1 |-
134 ?: =(0 i) hi :: negative array index
135 =/ k1 (snag (sub nblocks i) blocks)
136 =. k1 (sit (mul k1 c1))
137 =. k1 (rol 0 15 k1)
138 =. k1 (sit (mul k1 c2))
139 =. hi (mix hi k1)
140 =. hi (rol 0 13 hi)
141 =. hi (sum (sit (mul hi 5)) 0xe654.6b64)
142 $(i (dec i))
143 =/ tail (slag (mul 4 nblocks) data)
144 =/ k1 0
145 =/ tlen (dis len 3)
146 =. h1
147 ?+ tlen h1 :: fallthrough switch
148 $3 =. k1 (mix k1 (lsh 0 16 (snag 2 tail)))
149 =. k1 (mix k1 (lsh 0 8 (snag 1 tail)))
150 =. k1 (mix k1 (snag 0 tail))
151 =. k1 (sit (mul k1 c1))
152 =. k1 (rol 0 15 k1)
153 =. k1 (sit (mul k1 c2))
154 (mix h1 k1)
155 $2 =. k1 (mix k1 (lsh 0 8 (snag 1 tail)))
156 =. k1 (mix k1 (snag 0 tail))
157 =. k1 (sit (mul k1 c1))
158 =. k1 (rol 0 15 k1)
159 =. k1 (sit (mul k1 c2))
160 (mix h1 k1)
161 $1 =. k1 (mix k1 (snag 0 tail))
162 =. k1 (sit (mul k1 c1))
163 =. k1 (rol 0 15 k1)
164 =. k1 (sit (mul k1 c2))
165 (mix h1 k1)
166 ==
167 =. h1 (mix h1 len)
168 |^ (fmix32 h1)
169 ++ fmix32
170 |= h=@
171 =. h (mix h (rsh 0 16 h))
172 =. h (sit (mul h 0x85eb.ca6b))
173 =. h (mix h (rsh 0 13 h))
174 =. h (sit (mul h 0xc2b2.ae35))
175 =. h (mix h (rsh 0 16 h))
176 h
177 --
178```
180#### Examples
182```
183 > (muk 6 1 3)
184 3.427.677.118
186 > (muk 6 2 'jerry')
187 ! exit
189 > (muk 6 2 'je')
190 3.602.081.716
192 > (met 3 'jerry')
193 5
195 > (met 3 'je')
196 2
198 > (muk 6 5 'jerry')
199 1.276.447.489
201 > (muk 6 15 'jerry')
202 2.881.503.571
204 > =e (cut 3 [0 2] eny) :: `eny` is 512 bits of entropy (a random number).
205 > e
206 44.228
208 > (muk e 11 10.000)
209 246.077.549
210```
213---
214### `++mum`
21631-bit MurmurHash3 scrambler
218Hashes `a` with the 31-bit MurmurHash3 non-cryptographic hash algorithm,
219producing an atom.
221#### Accepts
223`a` is a noun.
225#### Produces
227An atom.
229#### Source
231```hoon
232 ++ mum
233 ~/ %mum
234 |= a=*
235 |^ (trim ?@(a a (mix $(a -.a) (mix 0x7fff.ffff $(a +.a)))))
236 ++ trim
237 |= key=@
238 =+ syd=0xcafe.babe
239 |- ^- @
240 =+ haz=(muk syd (met 3 key) key)
241 =+ ham=(mix (rsh 0 31 haz) (end 0 31 haz))
242 ?.(=(0 ham) ham $(syd +(syd)))
243```
245#### Examples
247```
248 > (mum 10.000)
249 795.713.195
251 > (mum 10.001)
252 420.521.697
254 > (mum 'jerry')
255 1.540.161.753
257 > (mum 1)
258 1.901.865.568
260 > (mum (some 10))
261 2.099.076.946
263 > (mum [1 2 3 4 5 ~])
264 1.719.833.228
265```
268---