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