]> gitweb.factorcode.org Git - factor.git/blob - basis/checksums/md5/md5.factor
factor: trim using lists
[factor.git] / basis / checksums / md5 / md5.factor
1 ! Copyright (C) 2006, 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data byte-arrays checksums
4 checksums.common combinators grouping hints kernel
5 kernel.private literals math math.bitwise
6 math.functions sequences sequences.private specialized-arrays ;
7 SPECIALIZED-ARRAY: uint
8 IN: checksums.md5
9
10 SINGLETON: md5
11
12 INSTANCE: md5 block-checksum
13
14 TUPLE: md5-state < block-checksum-state
15 { state uint-array }
16 { old-state uint-array } ;
17
18 : <md5-state> ( -- md5 )
19     md5-state new-checksum-state
20         64 >>block-size
21         uint-array{ 0x67452301 0xefcdab89 0x98badcfe 0x10325476 }
22         [ clone >>state ] [ >>old-state ] bi ;
23
24 M: md5 initialize-checksum-state drop <md5-state> ;
25
26 <PRIVATE
27
28 : update-md5 ( md5 -- )
29     [ state>> ] [ old-state>> [ w+ ] 2map dup clone ] [ ] tri
30     [ old-state<< ] [ state<< ] bi ; inline
31
32 CONSTANT: T $[
33     80 <iota> [ sin abs 32 2^ * >integer ] uint-array{ } map-as
34 ]
35
36 :: F ( X Y Z -- FXYZ )
37     ! F(X,Y,Z) = XY v not(X) Z
38     X Y bitand X bitnot Z bitand bitor ; inline
39
40 :: G ( X Y Z -- GXYZ )
41     ! G(X,Y,Z) = XZ v Y not(Z)
42     X Z bitand Y Z bitnot bitand bitor ; inline
43
44 : H ( X Y Z -- HXYZ )
45     ! H(X,Y,Z) = X xor Y xor Z
46     bitxor bitxor ; inline
47
48 :: I ( X Y Z -- IXYZ )
49     ! I(X,Y,Z) = Y xor (X v not(Z))
50     Z bitnot X bitor Y bitxor ; inline
51
52 CONSTANT: S11 7
53 CONSTANT: S12 12
54 CONSTANT: S13 17
55 CONSTANT: S14 22
56 CONSTANT: S21 5
57 CONSTANT: S22 9
58 CONSTANT: S23 14
59 CONSTANT: S24 20
60 CONSTANT: S31 4
61 CONSTANT: S32 11
62 CONSTANT: S33 16
63 CONSTANT: S34 23
64 CONSTANT: S41 6
65 CONSTANT: S42 10
66 CONSTANT: S43 15
67 CONSTANT: S44 21
68
69 CONSTANT: a 0
70 CONSTANT: b 1
71 CONSTANT: c 2
72 CONSTANT: d 3
73
74 :: (ABCD) ( x state a b c d k s i quot -- )
75     ! a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s)
76     a state [
77         b state nth-unsafe
78         c state nth-unsafe
79         d state nth-unsafe quot call w+
80         k x nth-unsafe w+
81         i T nth-unsafe w+
82         s bitroll-32
83         b state nth-unsafe w+
84     ] change-nth-unsafe ; inline
85
86 MACRO: with-md5-round ( ops quot -- quot )
87     '[ [ _ (ABCD) ] compose ] map '[ _ 2cleave ] ;
88
89 : (process-md5-block-F) ( block state -- )
90     { uint-array uint-array } declare {
91         [ a b c d 0  S11 1  ]
92         [ d a b c 1  S12 2  ]
93         [ c d a b 2  S13 3  ]
94         [ b c d a 3  S14 4  ]
95         [ a b c d 4  S11 5  ]
96         [ d a b c 5  S12 6  ]
97         [ c d a b 6  S13 7  ]
98         [ b c d a 7  S14 8  ]
99         [ a b c d 8  S11 9  ]
100         [ d a b c 9  S12 10 ]
101         [ c d a b 10 S13 11 ]
102         [ b c d a 11 S14 12 ]
103         [ a b c d 12 S11 13 ]
104         [ d a b c 13 S12 14 ]
105         [ c d a b 14 S13 15 ]
106         [ b c d a 15 S14 16 ]
107     } [ F ] with-md5-round ;
108
109 : (process-md5-block-G) ( block state -- )
110     { uint-array uint-array } declare {
111         [ a b c d 1  S21 17 ]
112         [ d a b c 6  S22 18 ]
113         [ c d a b 11 S23 19 ]
114         [ b c d a 0  S24 20 ]
115         [ a b c d 5  S21 21 ]
116         [ d a b c 10 S22 22 ]
117         [ c d a b 15 S23 23 ]
118         [ b c d a 4  S24 24 ]
119         [ a b c d 9  S21 25 ]
120         [ d a b c 14 S22 26 ]
121         [ c d a b 3  S23 27 ]
122         [ b c d a 8  S24 28 ]
123         [ a b c d 13 S21 29 ]
124         [ d a b c 2  S22 30 ]
125         [ c d a b 7  S23 31 ]
126         [ b c d a 12 S24 32 ]
127     } [ G ] with-md5-round ;
128
129 : (process-md5-block-H) ( block state -- )
130     { uint-array uint-array } declare {
131         [ a b c d 5  S31 33 ]
132         [ d a b c 8  S32 34 ]
133         [ c d a b 11 S33 35 ]
134         [ b c d a 14 S34 36 ]
135         [ a b c d 1  S31 37 ]
136         [ d a b c 4  S32 38 ]
137         [ c d a b 7  S33 39 ]
138         [ b c d a 10 S34 40 ]
139         [ a b c d 13 S31 41 ]
140         [ d a b c 0  S32 42 ]
141         [ c d a b 3  S33 43 ]
142         [ b c d a 6  S34 44 ]
143         [ a b c d 9  S31 45 ]
144         [ d a b c 12 S32 46 ]
145         [ c d a b 15 S33 47 ]
146         [ b c d a 2  S34 48 ]
147     } [ H ] with-md5-round ;
148
149 : (process-md5-block-I) ( block state -- )
150     { uint-array uint-array } declare {
151         [ a b c d 0  S41 49 ]
152         [ d a b c 7  S42 50 ]
153         [ c d a b 14 S43 51 ]
154         [ b c d a 5  S44 52 ]
155         [ a b c d 12 S41 53 ]
156         [ d a b c 3  S42 54 ]
157         [ c d a b 10 S43 55 ]
158         [ b c d a 1  S44 56 ]
159         [ a b c d 8  S41 57 ]
160         [ d a b c 15 S42 58 ]
161         [ c d a b 6  S43 59 ]
162         [ b c d a 13 S44 60 ]
163         [ a b c d 4  S41 61 ]
164         [ d a b c 11 S42 62 ]
165         [ c d a b 2  S43 63 ]
166         [ b c d a 9  S44 64 ]
167     } [ I ] with-md5-round ;
168
169 : byte-array>le ( byte-array -- byte-array )
170     little-endian? [
171         dup 4 <groups> [
172             [ [ 1 2 ] dip exchange-unsafe ]
173             [ [ 0 3 ] dip exchange-unsafe ] bi
174         ] each
175     ] unless ;
176
177 HINTS: byte-array>le byte-array ;
178
179 M: md5-state checksum-block
180     [
181         [ byte-array>le uint cast-array ] [ state>> ] bi* {
182             [ (process-md5-block-F) ]
183             [ (process-md5-block-G) ]
184             [ (process-md5-block-H) ]
185             [ (process-md5-block-I) ]
186         } 2cleave
187     ] [
188         update-md5
189     ] bi ;
190
191 : md5>checksum ( md5 -- bytes )
192     state>> underlying>> byte-array>le ;
193
194 M: md5-state clone
195     call-next-method
196     [ clone ] change-state
197     [ clone ] change-old-state ;
198
199 M: md5-state get-checksum
200     clone
201     [ bytes>> f ] [ bytes-read>> pad-last-block ] [ ] tri
202     [ [ checksum-block ] curry each ] [ md5>checksum ] bi ;
203
204 PRIVATE>