Russ Cox | e3b7930 | 2008-11-24 12:30:40 -0800 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Andreas Auernhammer | 25db5d1 | 2017-05-03 01:08:31 +0200 | [diff] [blame] | 5 | // SHA-1 hash algorithm. See RFC 3174. |
Russ Cox | e3b7930 | 2008-11-24 12:30:40 -0800 | [diff] [blame] | 6 | |
Robin Eklind | d137a2c | 2013-02-19 10:02:01 -0500 | [diff] [blame] | 7 | package sha1 |
Russ Cox | e3b7930 | 2008-11-24 12:30:40 -0800 | [diff] [blame] | 8 | |
| 9 | import ( |
Tim Cooper | 731b632 | 2017-09-27 19:46:58 -0300 | [diff] [blame] | 10 | "bytes" |
Brad Fitzpatrick | 14c5c8a | 2014-02-12 11:27:36 -0800 | [diff] [blame] | 11 | "crypto/rand" |
Tim Cooper | 731b632 | 2017-09-27 19:46:58 -0300 | [diff] [blame] | 12 | "encoding" |
Robert Griesemer | 5a1d332 | 2009-12-15 15:33:31 -0800 | [diff] [blame] | 13 | "fmt" |
| 14 | "io" |
| 15 | "testing" |
Russ Cox | e3b7930 | 2008-11-24 12:30:40 -0800 | [diff] [blame] | 16 | ) |
| 17 | |
Russ Cox | d2cdcfc | 2009-01-16 10:14:28 -0800 | [diff] [blame] | 18 | type sha1Test struct { |
Tim Cooper | 731b632 | 2017-09-27 19:46:58 -0300 | [diff] [blame] | 19 | out string |
| 20 | in string |
| 21 | halfState string // marshaled hash state after first half of in written, used by TestGoldenMarshal |
Russ Cox | e3b7930 | 2008-11-24 12:30:40 -0800 | [diff] [blame] | 22 | } |
| 23 | |
Russ Cox | 22c98a3 | 2009-10-06 14:55:06 -0700 | [diff] [blame] | 24 | var golden = []sha1Test{ |
Tim Cooper | 731b632 | 2017-09-27 19:46:58 -0300 | [diff] [blame] | 25 | {"76245dbf96f661bd221046197ab8b9f063f11bad", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n", "sha\x01\v\xa0)I\xdeq(8h\x9ev\xe5\x88[\xf8\x81\x17\xba4Daaaaaaaaaaaaaaaaaaaaaa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x96"}, |
Russ Cox | 1d547e4 | 2017-12-06 02:17:24 -0500 | [diff] [blame] | 26 | {"da39a3ee5e6b4b0d3255bfef95601890afd80709", "", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"}, |
| 27 | {"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", "a", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"}, |
| 28 | {"da23614e02469a0d7c7bd1bdab5c9c474b1904dc", "ab", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"}, |
| 29 | {"a9993e364706816aba3e25717850c26c9cd0d89d", "abc", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"}, |
| 30 | {"81fe8bfe87576c3ecb22426f8e57847382917acf", "abcd", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0ab\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02"}, |
| 31 | {"03de6c570bfe24bfc328ccd7ca46b76eadaf4334", "abcde", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0ab\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02"}, |
| 32 | {"1f8ac10f23c5b5bc1167bda84b833e5c057a77d2", "abcdef", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0abc\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03"}, |
| 33 | {"2fb5e13419fc89246865e7a324f476ec624e8740", "abcdefg", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0abc\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03"}, |
| 34 | {"425af12a0743502b322e93a015bcf868e324d56a", "abcdefgh", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0abcd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04"}, |
| 35 | {"c63b19f1e4c8b5f76b25c49b8b87f57d8e4872a1", "abcdefghi", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0abcd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04"}, |
| 36 | {"d68c19a0a345b7eab78d5e11e991c026ec60db63", "abcdefghij", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0abcde\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05"}, |
| 37 | {"ebf81ddcbe5bf13aaabdc4d65354fdf2044f38a7", "Discard medicine more than two years old.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0Discard medicine mor\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14"}, |
| 38 | {"e5dea09392dd886ca63531aaa00571dc07554bb6", "He who has a shady past knows that nice guys finish last.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0He who has a shady past know\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c"}, |
| 39 | {"45988f7234467b94e3e9494434c96ee3609d8f8f", "I wouldn't marry him with a ten foot pole.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0I wouldn't marry him \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x15"}, |
| 40 | {"55dee037eb7460d5a692d1ce11330b260e40c988", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0Free! Free!/A trip/to Mars/f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c"}, |
| 41 | {"b7bc5fb91080c7de6b582ea281f8a396d7c0aee8", "The days of the digital watch are numbered. -Tom Stoppard", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0The days of the digital watch\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1d"}, |
| 42 | {"c3aed9358f7c77f523afe86135f06b95b3999797", "Nepal premier won't resign.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0Nepal premier\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\r"}, |
| 43 | {"6e29d302bf6e3a5e4305ff318d983197d6906bb9", "For every action there is an equal and opposite government program.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0For every action there is an equa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00!"}, |
| 44 | {"597f6a540010f94c15d71806a99a2c8710e747bd", "His money is twice tainted: 'taint yours and 'taint mine.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0His money is twice tainted: \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c"}, |
| 45 | {"6859733b2590a8a091cecf50086febc5ceef1e80", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0There is no reason for any individual to hav\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00,"}, |
| 46 | {"514b2630ec089b8aee18795fc0cf1f4860cdacad", "It's a tiny change to the code and not completely disgusting. - Bob Manchek", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0It's a tiny change to the code and no\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00%"}, |
| 47 | {"c5ca0d4a7b6676fc7aa72caa41cc3d5df567ed69", "size: a.out: bad magic", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0size: a.out\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\f"}, |
| 48 | {"74c51fa9a04eadc8c1bbeaa7fc442f834b90a00a", "The major problem is with sendmail. -Mark Horton", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0The major problem is wit\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18"}, |
| 49 | {"0b4c4ce5f52c3ad2821852a8dc00217fa18b8b66", "Give me a rock, paper and scissors and I will move the world. CCFestoon", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0Give me a rock, paper and scissors a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$"}, |
| 50 | {"3ae7937dd790315beb0f48330e8642237c61550a", "If the enemy is within range, then so are you.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0If the enemy is within \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x17"}, |
| 51 | {"410a2b296df92b9a47412b13281df8f830a9f44b", "It's well we cannot hear the screams/That we create in others' dreams.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0It's well we cannot hear the scream\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00#"}, |
| 52 | {"841e7c85ca1adcddbdd0187f1289acb5c642f7f5", "You remind me of a TV show, but that's all right: I watch it anyway.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0You remind me of a TV show, but th\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\""}, |
| 53 | {"163173b825d03b952601376b25212df66763e1db", "C is as portable as Stonehedge!!", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0C is as portable\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10"}, |
| 54 | {"32b0377f2687eb88e22106f133c586ab314d5279", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0Even if I could be Shakespeare, I think I sh\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00,"}, |
| 55 | {"0885aaf99b569542fd165fa44e322718f4a984e0", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule", "sha\x01x}\xf4\r\xeb\xf2\x10\x87\xe8[\xb2JA$D\xb7\u063ax8em\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00B"}, |
| 56 | {"6627d6904d71420b0bf3886ab629623538689f45", "How can you write a big system without C++? -Paul Glick", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0How can you write a big syst\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c"}, |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 57 | } |
Russ Cox | e3b7930 | 2008-11-24 12:30:40 -0800 | [diff] [blame] | 58 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 59 | func TestGolden(t *testing.T) { |
Russ Cox | e3b7930 | 2008-11-24 12:30:40 -0800 | [diff] [blame] | 60 | for i := 0; i < len(golden); i++ { |
Robert Griesemer | 5a1d332 | 2009-12-15 15:33:31 -0800 | [diff] [blame] | 61 | g := golden[i] |
Rob Pike | 4cf7389 | 2013-06-25 17:04:18 -0700 | [diff] [blame] | 62 | s := fmt.Sprintf("%x", Sum([]byte(g.in))) |
| 63 | if s != g.out { |
| 64 | t.Fatalf("Sum function: sha1(%s) = %s want %s", g.in, s, g.out) |
| 65 | } |
Robin Eklind | d137a2c | 2013-02-19 10:02:01 -0500 | [diff] [blame] | 66 | c := New() |
Filippo Valsorda | f28cf83 | 2015-12-23 02:03:44 +0000 | [diff] [blame] | 67 | for j := 0; j < 4; j++ { |
| 68 | var sum []byte |
| 69 | switch j { |
| 70 | case 0, 1: |
Russ Cox | b9f26c3 | 2010-01-19 10:50:04 -0800 | [diff] [blame] | 71 | io.WriteString(c, g.in) |
Filippo Valsorda | f28cf83 | 2015-12-23 02:03:44 +0000 | [diff] [blame] | 72 | sum = c.Sum(nil) |
| 73 | case 2: |
Russ Cox | b9f26c3 | 2010-01-19 10:50:04 -0800 | [diff] [blame] | 74 | io.WriteString(c, g.in[0:len(g.in)/2]) |
Adam Langley | bac7bc5 | 2011-12-01 12:35:37 -0500 | [diff] [blame] | 75 | c.Sum(nil) |
Russ Cox | b9f26c3 | 2010-01-19 10:50:04 -0800 | [diff] [blame] | 76 | io.WriteString(c, g.in[len(g.in)/2:]) |
Filippo Valsorda | f28cf83 | 2015-12-23 02:03:44 +0000 | [diff] [blame] | 77 | sum = c.Sum(nil) |
| 78 | case 3: |
| 79 | io.WriteString(c, g.in[0:len(g.in)/2]) |
| 80 | c.(*digest).ConstantTimeSum(nil) |
| 81 | io.WriteString(c, g.in[len(g.in)/2:]) |
| 82 | sum = c.(*digest).ConstantTimeSum(nil) |
Russ Cox | b9f26c3 | 2010-01-19 10:50:04 -0800 | [diff] [blame] | 83 | } |
Filippo Valsorda | f28cf83 | 2015-12-23 02:03:44 +0000 | [diff] [blame] | 84 | s := fmt.Sprintf("%x", sum) |
Russ Cox | b112d42 | 2009-05-22 22:40:26 -0700 | [diff] [blame] | 85 | if s != g.out { |
Russ Cox | b9f26c3 | 2010-01-19 10:50:04 -0800 | [diff] [blame] | 86 | t.Fatalf("sha1[%d](%s) = %s want %s", j, g.in, s, g.out) |
Russ Cox | b112d42 | 2009-05-22 22:40:26 -0700 | [diff] [blame] | 87 | } |
Robert Griesemer | 5a1d332 | 2009-12-15 15:33:31 -0800 | [diff] [blame] | 88 | c.Reset() |
Russ Cox | e3b7930 | 2008-11-24 12:30:40 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
Andrew Gerrand | 3e804f9 | 2012-02-18 11:48:33 +1100 | [diff] [blame] | 92 | |
Tim Cooper | 731b632 | 2017-09-27 19:46:58 -0300 | [diff] [blame] | 93 | func TestGoldenMarshal(t *testing.T) { |
| 94 | h := New() |
| 95 | h2 := New() |
| 96 | for _, g := range golden { |
| 97 | h.Reset() |
| 98 | h2.Reset() |
| 99 | |
| 100 | io.WriteString(h, g.in[:len(g.in)/2]) |
| 101 | |
| 102 | state, err := h.(encoding.BinaryMarshaler).MarshalBinary() |
| 103 | if err != nil { |
| 104 | t.Errorf("could not marshal: %v", err) |
| 105 | continue |
| 106 | } |
| 107 | |
| 108 | if string(state) != g.halfState { |
Russ Cox | 1d547e4 | 2017-12-06 02:17:24 -0500 | [diff] [blame] | 109 | t.Errorf("sha1(%q) state = %+q, want %+q", g.in, state, g.halfState) |
Tim Cooper | 731b632 | 2017-09-27 19:46:58 -0300 | [diff] [blame] | 110 | continue |
| 111 | } |
| 112 | |
| 113 | if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil { |
| 114 | t.Errorf("could not unmarshal: %v", err) |
| 115 | continue |
| 116 | } |
| 117 | |
| 118 | io.WriteString(h, g.in[len(g.in)/2:]) |
| 119 | io.WriteString(h2, g.in[len(g.in)/2:]) |
| 120 | |
| 121 | if actual, actual2 := h.Sum(nil), h2.Sum(nil); !bytes.Equal(actual, actual2) { |
| 122 | t.Errorf("sha1(%q) = 0x%x != marshaled 0x%x", g.in, actual, actual2) |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Shawn Smith | 0333e80 | 2013-12-31 23:13:05 +1100 | [diff] [blame] | 127 | func TestSize(t *testing.T) { |
| 128 | c := New() |
| 129 | if got := c.Size(); got != Size { |
| 130 | t.Errorf("Size = %d; want %d", got, Size) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func TestBlockSize(t *testing.T) { |
| 135 | c := New() |
| 136 | if got := c.BlockSize(); got != BlockSize { |
| 137 | t.Errorf("BlockSize = %d; want %d", got, BlockSize) |
| 138 | } |
| 139 | } |
| 140 | |
Michael Munday | 63ceeaf | 2016-04-18 17:41:50 -0400 | [diff] [blame] | 141 | // Tests that blockGeneric (pure Go) and block (in assembly for some architectures) match. |
Brad Fitzpatrick | 14c5c8a | 2014-02-12 11:27:36 -0800 | [diff] [blame] | 142 | func TestBlockGeneric(t *testing.T) { |
Ilya Tocar | 805eaee | 2016-05-17 20:55:55 +0300 | [diff] [blame] | 143 | for i := 1; i < 30; i++ { // arbitrary factor |
| 144 | gen, asm := New().(*digest), New().(*digest) |
| 145 | buf := make([]byte, BlockSize*i) |
| 146 | rand.Read(buf) |
| 147 | blockGeneric(gen, buf) |
| 148 | block(asm, buf) |
| 149 | if *gen != *asm { |
| 150 | t.Errorf("For %#v block and blockGeneric resulted in different states", buf) |
| 151 | } |
Brad Fitzpatrick | 14c5c8a | 2014-02-12 11:27:36 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Robin Eklind | d137a2c | 2013-02-19 10:02:01 -0500 | [diff] [blame] | 155 | var bench = New() |
Eric Roshan-Eisner | 03c52a5 | 2012-11-01 16:21:18 -0400 | [diff] [blame] | 156 | var buf = make([]byte, 8192) |
Russ Cox | 992a11b | 2012-05-29 12:45:40 -0400 | [diff] [blame] | 157 | |
Eric Roshan-Eisner | 03c52a5 | 2012-11-01 16:21:18 -0400 | [diff] [blame] | 158 | func benchmarkSize(b *testing.B, size int) { |
| 159 | b.SetBytes(int64(size)) |
| 160 | sum := make([]byte, bench.Size()) |
| 161 | for i := 0; i < b.N; i++ { |
| 162 | bench.Reset() |
| 163 | bench.Write(buf[:size]) |
| 164 | bench.Sum(sum[:0]) |
Russ Cox | 992a11b | 2012-05-29 12:45:40 -0400 | [diff] [blame] | 165 | } |
Eric Roshan-Eisner | 03c52a5 | 2012-11-01 16:21:18 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | func BenchmarkHash8Bytes(b *testing.B) { |
| 169 | benchmarkSize(b, 8) |
Russ Cox | 992a11b | 2012-05-29 12:45:40 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Ilya Tocar | fafadc5 | 2016-04-29 16:17:14 +0300 | [diff] [blame] | 172 | func BenchmarkHash320Bytes(b *testing.B) { |
| 173 | benchmarkSize(b, 320) |
| 174 | } |
| 175 | |
Russ Cox | 992a11b | 2012-05-29 12:45:40 -0400 | [diff] [blame] | 176 | func BenchmarkHash1K(b *testing.B) { |
Eric Roshan-Eisner | 03c52a5 | 2012-11-01 16:21:18 -0400 | [diff] [blame] | 177 | benchmarkSize(b, 1024) |
Russ Cox | 992a11b | 2012-05-29 12:45:40 -0400 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | func BenchmarkHash8K(b *testing.B) { |
Eric Roshan-Eisner | 03c52a5 | 2012-11-01 16:21:18 -0400 | [diff] [blame] | 181 | benchmarkSize(b, 8192) |
Russ Cox | 992a11b | 2012-05-29 12:45:40 -0400 | [diff] [blame] | 182 | } |