1 ! Copyright (C) 2006, 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: 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.uint literals hints ;
13 INSTANCE: md5 stream-checksum
15 TUPLE: md5-state < checksum-state state old-state ;
17 : <md5-state> ( -- md5 )
18 md5-state new-checksum-state
20 uint-array{ HEX: 67452301 HEX: efcdab89 HEX: 98badcfe HEX: 10325476 }
21 [ clone >>state ] [ >>old-state ] bi ;
23 M: md5 initialize-checksum-state drop <md5-state> ;
27 : v-w+ ( v1 v2 -- v3 ) [ w+ ] 2map ;
29 : update-md5 ( md5 -- )
30 [ state>> ] [ old-state>> v-w+ dup clone ] [ ] tri
31 [ (>>old-state) ] [ (>>state) ] bi ;
35 80 iota [ sin abs 32 2^ * >integer ] uint-array{ } map-as
38 :: F ( X Y Z -- FXYZ )
39 #! F(X,Y,Z) = XY v not(X) Z
40 X Y bitand X bitnot Z bitand bitor ; inline
42 :: G ( X Y Z -- GXYZ )
43 #! G(X,Y,Z) = XZ v Y not(Z)
44 X Z bitand Y Z bitnot bitand bitor ; inline
47 #! H(X,Y,Z) = X xor Y xor Z
48 bitxor bitxor ; inline
50 :: I ( X Y Z -- IXYZ )
51 #! I(X,Y,Z) = Y xor (X v not(Z))
52 Z bitnot X bitor Y bitxor ; inline
76 :: (ABCD) ( x state a b c d k s i quot -- )
77 #! a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s)
81 d state nth-unsafe quot call w+
85 b state nth-unsafe w+ 32 bits
86 ] change-nth-unsafe ; inline
88 MACRO: with-md5-round ( ops quot -- )
89 '[ [ _ (ABCD) ] compose ] map '[ _ 2cleave ] ;
91 : (process-md5-block-F) ( block state -- )
103 [ c d a b 10 S13 11 ]
104 [ b c d a 11 S14 12 ]
105 [ a b c d 12 S11 13 ]
106 [ d a b c 13 S12 14 ]
107 [ c d a b 14 S13 15 ]
108 [ b c d a 15 S14 16 ]
109 } [ F ] with-md5-round ;
111 : (process-md5-block-G) ( block state -- )
115 [ c d a b 11 S23 19 ]
118 [ d a b c 10 S22 22 ]
119 [ c d a b 15 S23 23 ]
122 [ d a b c 14 S22 26 ]
125 [ a b c d 13 S21 29 ]
128 [ b c d a 12 S24 32 ]
129 } [ G ] with-md5-round ;
131 : (process-md5-block-H) ( block state -- )
135 [ c d a b 11 S33 35 ]
136 [ b c d a 14 S34 36 ]
140 [ b c d a 10 S34 40 ]
141 [ a b c d 13 S31 41 ]
146 [ d a b c 12 S32 46 ]
147 [ c d a b 15 S33 47 ]
149 } [ H ] with-md5-round ;
151 : (process-md5-block-I) ( block state -- )
155 [ c d a b 14 S43 51 ]
157 [ a b c d 12 S41 53 ]
159 [ c d a b 10 S43 55 ]
162 [ d a b c 15 S42 58 ]
164 [ b c d a 13 S44 60 ]
166 [ d a b c 11 S42 62 ]
169 } [ I ] with-md5-round ;
171 HINTS: (process-md5-block-F) { uint-array md5-state } ;
172 HINTS: (process-md5-block-G) { uint-array md5-state } ;
173 HINTS: (process-md5-block-H) { uint-array md5-state } ;
174 HINTS: (process-md5-block-I) { uint-array md5-state } ;
176 M: md5-state checksum-block ( block state -- )
178 [ byte-array>uint-array ] [ state>> ] bi* {
179 [ (process-md5-block-F) ]
180 [ (process-md5-block-G) ]
181 [ (process-md5-block-H) ]
182 [ (process-md5-block-I) ]
188 : md5>checksum ( md5 -- bytes ) state>> underlying>> ;
190 M: md5-state clone ( md5 -- new-md5 )
192 [ clone ] change-state
193 [ clone ] change-old-state ;
195 M: md5-state get-checksum ( md5 -- bytes )
196 clone [ bytes>> f ] [ bytes-read>> pad-last-block ] [ ] tri
197 [ [ checksum-block ] curry each ] [ md5>checksum ] bi ;
199 M: md5 checksum-stream ( stream checksum -- byte-array )
201 [ <md5-state> ] dip add-checksum-stream get-checksum ;