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 checksums.stream combinators fry grouping hints
5 kernel kernel.private literals locals macros math math.bitwise
6 math.functions sequences sequences.private specialized-arrays ;
7 FROM: sequences.private => change-nth-unsafe ;
8 SPECIALIZED-ARRAY: uint
13 INSTANCE: md5 stream-checksum
15 TUPLE: md5-state < checksum-state
17 { old-state uint-array } ;
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 : update-md5 ( md5 -- )
30 [ state>> ] [ old-state>> [ w+ ] 2map dup clone ] [ ] tri
31 [ old-state<< ] [ state<< ] bi ; inline
34 80 iota [ sin abs 32 2^ * >integer ] uint-array{ } map-as
37 :: F ( X Y Z -- FXYZ )
38 #! F(X,Y,Z) = XY v not(X) Z
39 X Y bitand X bitnot Z bitand bitor ; inline
41 :: G ( X Y Z -- GXYZ )
42 #! G(X,Y,Z) = XZ v Y not(Z)
43 X Z bitand Y Z bitnot bitand bitor ; inline
46 #! H(X,Y,Z) = X xor Y xor Z
47 bitxor bitxor ; inline
49 :: I ( X Y Z -- IXYZ )
50 #! I(X,Y,Z) = Y xor (X v not(Z))
51 Z bitnot X bitor Y bitxor ; inline
75 :: (ABCD) ( x state a b c d k s i quot -- )
76 #! a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s)
80 d state nth-unsafe quot call w+
85 ] change-nth-unsafe ; inline
87 MACRO: with-md5-round ( ops quot -- )
88 '[ [ _ (ABCD) ] compose ] map '[ _ 2cleave ] ;
90 : (process-md5-block-F) ( block state -- )
91 { uint-array uint-array } declare {
102 [ c d a b 10 S13 11 ]
103 [ b c d a 11 S14 12 ]
104 [ a b c d 12 S11 13 ]
105 [ d a b c 13 S12 14 ]
106 [ c d a b 14 S13 15 ]
107 [ b c d a 15 S14 16 ]
108 } [ F ] with-md5-round ;
110 : (process-md5-block-G) ( block state -- )
111 { uint-array uint-array } declare {
114 [ c d a b 11 S23 19 ]
117 [ d a b c 10 S22 22 ]
118 [ c d a b 15 S23 23 ]
121 [ d a b c 14 S22 26 ]
124 [ a b c d 13 S21 29 ]
127 [ b c d a 12 S24 32 ]
128 } [ G ] with-md5-round ;
130 : (process-md5-block-H) ( block state -- )
131 { uint-array uint-array } declare {
134 [ c d a b 11 S33 35 ]
135 [ b c d a 14 S34 36 ]
139 [ b c d a 10 S34 40 ]
140 [ a b c d 13 S31 41 ]
145 [ d a b c 12 S32 46 ]
146 [ c d a b 15 S33 47 ]
148 } [ H ] with-md5-round ;
150 : (process-md5-block-I) ( block state -- )
151 { uint-array uint-array } declare {
154 [ c d a b 14 S43 51 ]
156 [ a b c d 12 S41 53 ]
158 [ c d a b 10 S43 55 ]
161 [ d a b c 15 S42 58 ]
163 [ b c d a 13 S44 60 ]
165 [ d a b c 11 S42 62 ]
168 } [ I ] with-md5-round ;
170 : byte-array>le ( byte-array -- byte-array )
173 [ [ 1 2 ] dip exchange-unsafe ]
174 [ [ 0 3 ] dip exchange-unsafe ] bi
178 HINTS: byte-array>le byte-array ;
180 M: md5-state checksum-block
182 [ byte-array>le uint cast-array ] [ state>> ] bi* {
183 [ (process-md5-block-F) ]
184 [ (process-md5-block-G) ]
185 [ (process-md5-block-H) ]
186 [ (process-md5-block-I) ]
192 : md5>checksum ( md5 -- bytes )
193 state>> underlying>> byte-array>le ;
197 [ clone ] change-state
198 [ clone ] change-old-state ;
200 M: md5-state get-checksum
202 [ bytes>> f ] [ bytes-read>> pad-last-block ] [ ] tri
203 [ [ checksum-block ] curry each ] [ md5>checksum ] bi ;
205 M: md5 checksum-stream
207 [ <md5-state> ] dip add-checksum-stream get-checksum ;