1 ! Copyright (C) 2006, 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types alien.data kernel io io.binary io.files
4 io.streams.byte-array math math.functions math.parser namespaces
5 splitting grouping strings sequences byte-arrays locals
6 sequences.private macros fry io.encodings.binary math.bitwise
7 checksums accessors checksums.common checksums.stream
8 combinators combinators.smart specialized-arrays literals hints ;
9 FROM: sequences.private => change-nth-unsafe ;
10 SPECIALIZED-ARRAY: uint
15 INSTANCE: md5 stream-checksum
17 TUPLE: md5-state < checksum-state state old-state ;
19 : <md5-state> ( -- md5 )
20 md5-state new-checksum-state
22 uint-array{ 0x67452301 0xefcdab89 0x98badcfe 0x10325476 }
23 [ clone >>state ] [ >>old-state ] bi ;
25 M: md5 initialize-checksum-state drop <md5-state> ;
29 : v-w+ ( v1 v2 -- v3 ) [ w+ ] 2map ;
31 : update-md5 ( md5 -- )
32 [ state>> ] [ old-state>> v-w+ dup clone ] [ ] tri
33 [ old-state<< ] [ state<< ] bi ;
37 80 iota [ sin abs 32 2^ * >integer ] uint-array{ } map-as
40 :: F ( X Y Z -- FXYZ )
41 #! F(X,Y,Z) = XY v not(X) Z
42 X Y bitand X bitnot Z bitand bitor ; inline
44 :: G ( X Y Z -- GXYZ )
45 #! G(X,Y,Z) = XZ v Y not(Z)
46 X Z bitand Y Z bitnot bitand bitor ; inline
49 #! H(X,Y,Z) = X xor Y xor Z
50 bitxor bitxor ; inline
52 :: I ( X Y Z -- IXYZ )
53 #! I(X,Y,Z) = Y xor (X v not(Z))
54 Z bitnot X bitor Y bitxor ; inline
78 :: (ABCD) ( x state a b c d k s i quot -- )
79 #! a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s)
83 d state nth-unsafe quot call w+
87 b state nth-unsafe w+ 32 bits
88 ] change-nth-unsafe ; inline
90 MACRO: with-md5-round ( ops quot -- )
91 '[ [ _ (ABCD) ] compose ] map '[ _ 2cleave ] ;
93 : (process-md5-block-F) ( block state -- )
105 [ c d a b 10 S13 11 ]
106 [ b c d a 11 S14 12 ]
107 [ a b c d 12 S11 13 ]
108 [ d a b c 13 S12 14 ]
109 [ c d a b 14 S13 15 ]
110 [ b c d a 15 S14 16 ]
111 } [ F ] with-md5-round ;
113 : (process-md5-block-G) ( block state -- )
117 [ c d a b 11 S23 19 ]
120 [ d a b c 10 S22 22 ]
121 [ c d a b 15 S23 23 ]
124 [ d a b c 14 S22 26 ]
127 [ a b c d 13 S21 29 ]
130 [ b c d a 12 S24 32 ]
131 } [ G ] with-md5-round ;
133 : (process-md5-block-H) ( block state -- )
137 [ c d a b 11 S33 35 ]
138 [ b c d a 14 S34 36 ]
142 [ b c d a 10 S34 40 ]
143 [ a b c d 13 S31 41 ]
148 [ d a b c 12 S32 46 ]
149 [ c d a b 15 S33 47 ]
151 } [ H ] with-md5-round ;
153 : (process-md5-block-I) ( block state -- )
157 [ c d a b 14 S43 51 ]
159 [ a b c d 12 S41 53 ]
161 [ c d a b 10 S43 55 ]
164 [ d a b c 15 S42 58 ]
166 [ b c d a 13 S44 60 ]
168 [ d a b c 11 S42 62 ]
171 } [ I ] with-md5-round ;
173 HINTS: (process-md5-block-F) { uint-array md5-state } ;
174 HINTS: (process-md5-block-G) { uint-array md5-state } ;
175 HINTS: (process-md5-block-H) { uint-array md5-state } ;
176 HINTS: (process-md5-block-I) { uint-array md5-state } ;
178 : byte-array>le ( byte-array -- byte-array )
180 dup 4 <sliced-groups> [
181 [ [ 1 2 ] dip exchange-unsafe ]
182 [ [ 0 3 ] dip exchange-unsafe ] bi
186 : uint-array-cast-le ( byte-array -- uint-array )
187 byte-array>le uint cast-array ;
189 HINTS: uint-array-cast-le byte-array ;
191 : uint-array>byte-array-le ( uint-array -- byte-array )
192 underlying>> byte-array>le ;
194 HINTS: uint-array>byte-array-le uint-array ;
196 M: md5-state checksum-block ( block state -- )
198 [ uint-array-cast-le ] [ state>> ] bi* {
199 [ (process-md5-block-F) ]
200 [ (process-md5-block-G) ]
201 [ (process-md5-block-H) ]
202 [ (process-md5-block-I) ]
208 : md5>checksum ( md5 -- bytes ) state>> uint-array>byte-array-le ;
210 M: md5-state clone ( md5 -- new-md5 )
212 [ clone ] change-state
213 [ clone ] change-old-state ;
215 M: md5-state get-checksum ( md5 -- bytes )
216 clone [ bytes>> f ] [ bytes-read>> pad-last-block ] [ ] tri
217 [ [ checksum-block ] curry each ] [ md5>checksum ] bi ;
219 M: md5 checksum-stream ( stream checksum -- byte-array )
221 [ <md5-state> ] dip add-checksum-stream get-checksum ;