1 USING: arrays checksums checksums.common checksums.sha
2 checksums.sha.private io.encodings.binary io.streams.byte-array
3 kernel math.parser sequences tools.test random ;
4 IN: checksums.sha.tests
6 { "a9993e364706816aba3e25717850c26c9cd0d89d" } [ "abc" sha1 checksum-bytes bytes>hex-string ] unit-test
7 { "84983e441c3bd26ebaae4aa1f95129e5e54670f1" } [ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" sha1 checksum-bytes bytes>hex-string ] unit-test
8 ! [ "34aa973cd4c4daa4f61eeb2bdbad27316534016f" ] [ 1000000 CHAR: a fill string>sha1str ] unit-test ! takes a long time...
9 { "dea356a2cddd90c7a7ecedc5ebb563934f460452" } [ "0123456701234567012345670123456701234567012345670123456701234567"
10 10 swap <array> concat sha1 checksum-bytes bytes>hex-string ] unit-test
13 { "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525" }
15 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
16 sha-224 checksum-bytes bytes>hex-string
19 { "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" }
20 [ "" sha-256 checksum-bytes bytes>hex-string ] unit-test
22 { "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" }
23 [ "abc" sha-256 checksum-bytes bytes>hex-string ] unit-test
25 { "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650" }
26 [ "message digest" sha-256 checksum-bytes bytes>hex-string ] unit-test
28 { "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73" }
29 [ "abcdefghijklmnopqrstuvwxyz" sha-256 checksum-bytes bytes>hex-string ] unit-test
31 { "db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0" }
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
34 sha-256 checksum-bytes bytes>hex-string
37 { "f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e" }
39 "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
40 sha-256 checksum-bytes bytes>hex-string
44 ! [ "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" ]
45 ! [ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" sha-512 checksum-bytes bytes>hex-string ] unit-test
50 <sha1-state> "asdf" binary <byte-reader> add-checksum-stream
51 [ get-checksum ] [ get-checksum ] bi =
57 <sha-256-state> "asdf" binary <byte-reader> add-checksum-stream
58 [ get-checksum ] [ get-checksum ] bi =
64 <sha-224-state> "asdf" binary <byte-reader> add-checksum-stream
65 [ get-checksum ] [ get-checksum ] bi =
69 CONSTANT: bytes-a B{ 0 1 0 0 0 0 0 0 }
70 CONSTANT: bytes-b B{ 1 2 3 4 5 6 7 8 }
72 sha1 initialize-checksum-state bytes-a bytes-b append add-checksum-bytes get-checksum
73 sha1 initialize-checksum-state bytes-a add-checksum-bytes bytes-b add-checksum-bytes get-checksum =
76 : incremental-checksum ( algorithm seqs -- checksum )
77 [ initialize-checksum-state ] dip
78 [ add-checksum-bytes ] each get-checksum ;
80 : one-go-checksum ( algorithm seqs -- checksum )
81 [ initialize-checksum-state ] dip
82 concat add-checksum-bytes get-checksum ;
84 ERROR: checksums-differ algorithm seq incremental-checksum one-go-checksum ;
85 : compare-checksum-calculations ( algorithm seq -- ? )
86 2dup [ incremental-checksum ] [ one-go-checksum ] 2bi 2dup = [
92 { t } [ 100 <iota> [ drop sha1 100 [ 100 random random-bytes ] replicate compare-checksum-calculations ] all? ] unit-test
93 { t } [ 100 <iota> [ drop sha1 20 [ 20 random random-bytes ] replicate compare-checksum-calculations ] all? ] unit-test
94 { t } [ 100 <iota> [ drop sha1 10 [ 10 random random-bytes ] replicate compare-checksum-calculations ] all? ] unit-test
97 B{ 105 27 166 214 73 114 110 }
98 B{ 39 162 16 218 0 42 }
101 B{ 28 236 253 45 240 123 134 191 22 }
102 B{ 220 27 205 59 27 48 }
103 B{ 249 2 196 177 74 195 12 131 91 }
104 B{ 174 102 159 89 250 38 230 5 }
105 B{ 126 22 231 253 118 64 }
106 B{ 185 127 20 126 123 35 204 243 43 }
107 } compare-checksum-calculations