on f3ca521 help sign in
276 lines 5.2 kB history raw
1+++
2title = "2e: Insecure Hashing"
3weight = 8
4template = "doc.html"
5aliases = ["/docs/reference/library/2e"]
6+++
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/library/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`
46FNV-1a scrambler
48Hashes `a` with the 31-bit nonzero FNV-1a non-cryptographic hash algorithm,
49producing an atom.
51#### Accepts
53`a` is a noun.
55#### Produces
57An atom.
59#### Source
61```hoon
62 ++ mug
63 ~/ %mug
64 |= a=*
65 ?^ a
66 =+ b=[p=$(a -.a) q=$(a +.a)]
67 |- ^- @
68 =+ c=(fnv (mix p.b (fnv q.b)))
69 =+ d=(mix (rsh 0 31 c) (end 0 31 c))
70 ?. =(0 d) d
71 $(q.b +(q.b))
72 =+ b=2.166.136.261
73 |- ^- @
74 =+ c=b
75 =+ [d=0 e=(met 3 a)]
76 |- ^- @
77 ?: =(d e)
78 =+ f=(mix (rsh 0 31 c) (end 0 31 c))
79 ?. =(0 f) f
80 ^$(b +(b))
81 $(c (fnv (mix c (cut 3 [d 1] a))), d +(d))
82```
84#### Examples
86```
87 > (mug 10.000)
88 178.152.889
90 > (mug 10.001)
91 714.838.017
93 > (mug 1)
94 67.918.732
96 > (mug (some 10))
97 1.872.403.737
99 > (mug [1 2 3 4 5 ~])
100 1.067.931.605
101```
104---
105### `++muk`
107Standard MurmurHash3
109Implementation of the 32-bit
110[MurmurHash3](https://en.wikipedia.org/wiki/MurmurHash#Algorithm)
111non-cryptographic hash algorithm. Takes `a` as the seed, `b` as the key length
112in [blocks](@/docs/hoon/library/2c.md) of size 3, and `c` as the key, producing an atom.
114#### Accepts
116`a` is an atom.
118`b` is an atom.
120`c` is an atom.
122#### Produces
124An atom.
126#### Source
128```hoon
129 ++ muk
130 ~% %muk ..muk ~
131 =+ ~(. fe 5)
132 |= [syd=@ len=@ key=@]
133 ?> &((lte (met 5 syd) 1) (lte (met 0 len) 31))
134 =/ pad (sub len (met 3 key))
135 =/ data (weld (rip 3 key) (reap pad 0))
136 =/ nblocks (div len 4) :: intentionally off-by-one
137 =/ h1 syd
138 =+ [c1=0xcc9e.2d51 c2=0x1b87.3593]
139 =/ blocks (rip 5 key)
140 =/ i nblocks
141 =. h1 =/ hi h1 |-
142 ?: =(0 i) hi :: negative array index
143 =/ k1 (snag (sub nblocks i) blocks)
144 =. k1 (sit (mul k1 c1))
145 =. k1 (rol 0 15 k1)
146 =. k1 (sit (mul k1 c2))
147 =. hi (mix hi k1)
148 =. hi (rol 0 13 hi)
149 =. hi (sum (sit (mul hi 5)) 0xe654.6b64)
150 $(i (dec i))
151 =/ tail (slag (mul 4 nblocks) data)
152 =/ k1 0
153 =/ tlen (dis len 3)
154 =. h1
155 ?+ tlen h1 :: fallthrough switch
156 $3 =. k1 (mix k1 (lsh 0 16 (snag 2 tail)))
157 =. k1 (mix k1 (lsh 0 8 (snag 1 tail)))
158 =. k1 (mix k1 (snag 0 tail))
159 =. k1 (sit (mul k1 c1))
160 =. k1 (rol 0 15 k1)
161 =. k1 (sit (mul k1 c2))
162 (mix h1 k1)
163 $2 =. k1 (mix k1 (lsh 0 8 (snag 1 tail)))
164 =. k1 (mix k1 (snag 0 tail))
165 =. k1 (sit (mul k1 c1))
166 =. k1 (rol 0 15 k1)
167 =. k1 (sit (mul k1 c2))
168 (mix h1 k1)
169 $1 =. k1 (mix k1 (snag 0 tail))
170 =. k1 (sit (mul k1 c1))
171 =. k1 (rol 0 15 k1)
172 =. k1 (sit (mul k1 c2))
173 (mix h1 k1)
174 ==
175 =. h1 (mix h1 len)
176 |^ (fmix32 h1)
177 ++ fmix32
178 |= h=@
179 =. h (mix h (rsh 0 16 h))
180 =. h (sit (mul h 0x85eb.ca6b))
181 =. h (mix h (rsh 0 13 h))
182 =. h (sit (mul h 0xc2b2.ae35))
183 =. h (mix h (rsh 0 16 h))
184 h
185 --
186```
188#### Examples
190```
191 > (muk 6 1 3)
192 3.427.677.118
194 > (muk 6 2 'jerry')
195 ! exit
197 > (muk 6 2 'je')
198 3.602.081.716
200 > (met 3 'jerry')
201 5
203 > (met 3 'je')
204 2
206 > (muk 6 5 'jerry')
207 1.276.447.489
209 > (muk 6 15 'jerry')
210 2.881.503.571
212 > =e (cut 3 [0 2] eny) :: `eny` is 512 bits of entropy (a random number).
213 > e
214 44.228
216 > (muk e 11 10.000)
217 246.077.549
218```
221---
222### `++mum`
22431-bit MurmurHash3 scrambler
226Hashes `a` with the 31-bit MurmurHash3 non-cryptographic hash algorithm,
227producing an atom.
229#### Accepts
231`a` is a noun.
233#### Produces
235An atom.
237#### Source
239```hoon
240 ++ mum
241 ~/ %mum
242 |= a=*
243 |^ (trim ?@(a a (mix $(a -.a) (mix 0x7fff.ffff $(a +.a)))))
244 ++ trim
245 |= key=@
246 =+ syd=0xcafe.babe
247 |- ^- @
248 =+ haz=(muk syd (met 3 key) key)
249 =+ ham=(mix (rsh 0 31 haz) (end 0 31 haz))
250 ?.(=(0 ham) ham $(syd +(syd)))
251```
253#### Examples
255```
256 > (mum 10.000)
257 795.713.195
259 > (mum 10.001)
260 420.521.697
262 > (mum 'jerry')
263 1.540.161.753
265 > (mum 1)
266 1.901.865.568
268 > (mum (some 10))
269 2.099.076.946
271 > (mum [1 2 3 4 5 ~])
272 1.719.833.228
273```
276---