]> gitweb.factorcode.org Git - factor.git/blob - basis/checksums/sha/sha-tests.factor
core: Rename iota to <iota> so we can have TUPLE: iota ... ; instead of TUPLE: iota...
[factor.git] / basis / checksums / sha / sha-tests.factor
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
5
6 : test-checksum ( text identifier -- checksum )
7     checksum-bytes bytes>hex-string ;
8
9 { "a9993e364706816aba3e25717850c26c9cd0d89d" } [ "abc" sha1 checksum-bytes bytes>hex-string ] unit-test
10 { "84983e441c3bd26ebaae4aa1f95129e5e54670f1" } [ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" sha1 checksum-bytes bytes>hex-string ] unit-test
11 ! [ "34aa973cd4c4daa4f61eeb2bdbad27316534016f" ] [ 1000000 CHAR: a fill string>sha1str ] unit-test ! takes a long time...
12 { "dea356a2cddd90c7a7ecedc5ebb563934f460452" } [ "0123456701234567012345670123456701234567012345670123456701234567"
13 10 swap <array> concat sha1 checksum-bytes bytes>hex-string ] unit-test
14
15
16 { "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525" }
17 [
18     "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
19     sha-224 test-checksum
20 ] unit-test
21
22 { "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" }
23 [ "" sha-256 test-checksum ] unit-test
24
25 { "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" }
26 [ "abc" sha-256 test-checksum ] unit-test
27
28 { "f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650" }
29 [ "message digest" sha-256 test-checksum ] unit-test
30
31 { "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73" }
32 [ "abcdefghijklmnopqrstuvwxyz" sha-256 test-checksum ] unit-test
33
34 { "db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0" }
35 [
36     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
37     sha-256 test-checksum
38 ] unit-test
39
40 { "f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e" }
41 [
42     "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
43     sha-256 test-checksum
44 ] unit-test
45
46
47 ! [ "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" ]
48 ! [ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" sha-512 test-checksum ] unit-test
49
50 {
51     t
52 } [
53     <sha1-state> "asdf" binary <byte-reader> add-checksum-stream
54     [ get-checksum ] [ get-checksum ] bi =
55 ] unit-test
56
57 {
58     t
59 } [
60     <sha-256-state> "asdf" binary <byte-reader> add-checksum-stream
61     [ get-checksum ] [ get-checksum ] bi =
62 ] unit-test
63
64 {
65     t
66 } [
67     <sha-224-state> "asdf" binary <byte-reader> add-checksum-stream
68     [ get-checksum ] [ get-checksum ] bi =
69 ] unit-test
70
71
72 CONSTANT: bytes-a B{ 0 1 0 0 0 0 0 0 }
73 CONSTANT: bytes-b B{ 1 2 3 4 5 6 7 8 }
74 { t } [
75     sha1 initialize-checksum-state bytes-a bytes-b append add-checksum-bytes get-checksum
76     sha1 initialize-checksum-state bytes-a add-checksum-bytes bytes-b add-checksum-bytes get-checksum =
77 ] unit-test
78
79 : incremental-checksum ( algorithm seqs -- checksum )
80     [ initialize-checksum-state ] dip
81     [ add-checksum-bytes ] each get-checksum ;
82
83 : one-go-checksum ( algorithm seqs -- checksum )
84     [ initialize-checksum-state ] dip
85     concat add-checksum-bytes get-checksum ;
86
87 ERROR: checksums-differ algorithm seq incremental-checksum one-go-checksum ;
88 : compare-checksum-calculations ( algorithm seq -- ? )
89     2dup [ incremental-checksum ] [ one-go-checksum ] 2bi 2dup = [
90         2drop 2drop t
91     ] [
92         checksums-differ
93     ] if ;
94
95 { t } [ 100 <iota> [ drop sha1 100 [ 100 random random-bytes ] replicate compare-checksum-calculations ] all? ] unit-test
96 { t } [ 100 <iota> [ drop sha1 20 [ 20 random random-bytes ] replicate compare-checksum-calculations ] all? ] unit-test
97 { t } [ 100 <iota> [ drop sha1 10 [ 10 random random-bytes ] replicate compare-checksum-calculations ] all? ] unit-test
98
99 { t } [ sha1 {
100     B{ 105 27 166 214 73 114 110 }
101     B{ 39 162 16 218 0 42 }
102     B{ 129 235 197 233 }
103     B{ 61 29 254 66 67 }
104     B{ 28 236 253 45 240 123 134 191 22 }
105     B{ 220 27 205 59 27 48 }
106     B{ 249 2 196 177 74 195 12 131 91 }
107     B{ 174 102 159 89 250 38 230 5 }
108     B{ 126 22 231 253 118 64 }
109     B{ 185 127 20 126 123 35 204 243 43 }
110     } compare-checksum-calculations
111 ] unit-test