OLD | NEW |
---|---|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/sha2.h" | |
wtc
2011/04/07 05:35:53
It is correct to include "crypto/sha2.h" first.
| |
6 | |
7 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "crypto/sha2.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
9 | 8 |
10 TEST(Sha256Test, Test1) { | 9 TEST(Sha256Test, Test1) { |
11 // Example B.1 from FIPS 180-2: one-block message. | 10 // Example B.1 from FIPS 180-2: one-block message. |
12 std::string input1 = "abc"; | 11 std::string input1 = "abc"; |
13 int expected1[] = { 0xba, 0x78, 0x16, 0xbf, | 12 int expected1[] = { 0xba, 0x78, 0x16, 0xbf, |
14 0x8f, 0x01, 0xcf, 0xea, | 13 0x8f, 0x01, 0xcf, 0xea, |
15 0x41, 0x41, 0x40, 0xde, | 14 0x41, 0x41, 0x40, 0xde, |
16 0x5d, 0xae, 0x22, 0x23, | 15 0x5d, 0xae, 0x22, 0x23, |
17 0xb0, 0x03, 0x61, 0xa3, | 16 0xb0, 0x03, 0x61, 0xa3, |
18 0x96, 0x17, 0x7a, 0x9c, | 17 0x96, 0x17, 0x7a, 0x9c, |
19 0xb4, 0x10, 0xff, 0x61, | 18 0xb4, 0x10, 0xff, 0x61, |
20 0xf2, 0x00, 0x15, 0xad }; | 19 0xf2, 0x00, 0x15, 0xad }; |
21 | 20 |
22 uint8 output1[base::SHA256_LENGTH]; | 21 uint8 output1[crypto::SHA256_LENGTH]; |
23 base::SHA256HashString(input1, output1, sizeof(output1)); | 22 crypto::SHA256HashString(input1, output1, sizeof(output1)); |
24 for (size_t i = 0; i < base::SHA256_LENGTH; i++) | 23 for (size_t i = 0; i < crypto::SHA256_LENGTH; i++) |
25 EXPECT_EQ(expected1[i], static_cast<int>(output1[i])); | 24 EXPECT_EQ(expected1[i], static_cast<int>(output1[i])); |
26 | 25 |
27 uint8 output_truncated1[4]; // 4 bytes == 32 bits | 26 uint8 output_truncated1[4]; // 4 bytes == 32 bits |
28 base::SHA256HashString(input1, output_truncated1, sizeof(output_truncated1)); | 27 crypto::SHA256HashString(input1, |
28 output_truncated1, sizeof(output_truncated1)); | |
29 for (size_t i = 0; i < sizeof(output_truncated1); i++) | 29 for (size_t i = 0; i < sizeof(output_truncated1); i++) |
30 EXPECT_EQ(expected1[i], static_cast<int>(output_truncated1[i])); | 30 EXPECT_EQ(expected1[i], static_cast<int>(output_truncated1[i])); |
31 } | 31 } |
32 | 32 |
33 TEST(Sha256Test, Test1_String) { | 33 TEST(Sha256Test, Test1_String) { |
34 // Same as the above, but using the wrapper that returns a std::string. | 34 // Same as the above, but using the wrapper that returns a std::string. |
35 // Example B.1 from FIPS 180-2: one-block message. | 35 // Example B.1 from FIPS 180-2: one-block message. |
36 std::string input1 = "abc"; | 36 std::string input1 = "abc"; |
37 int expected1[] = { 0xba, 0x78, 0x16, 0xbf, | 37 int expected1[] = { 0xba, 0x78, 0x16, 0xbf, |
38 0x8f, 0x01, 0xcf, 0xea, | 38 0x8f, 0x01, 0xcf, 0xea, |
39 0x41, 0x41, 0x40, 0xde, | 39 0x41, 0x41, 0x40, 0xde, |
40 0x5d, 0xae, 0x22, 0x23, | 40 0x5d, 0xae, 0x22, 0x23, |
41 0xb0, 0x03, 0x61, 0xa3, | 41 0xb0, 0x03, 0x61, 0xa3, |
42 0x96, 0x17, 0x7a, 0x9c, | 42 0x96, 0x17, 0x7a, 0x9c, |
43 0xb4, 0x10, 0xff, 0x61, | 43 0xb4, 0x10, 0xff, 0x61, |
44 0xf2, 0x00, 0x15, 0xad }; | 44 0xf2, 0x00, 0x15, 0xad }; |
45 | 45 |
46 std::string output1 = base::SHA256HashString(input1); | 46 std::string output1 = crypto::SHA256HashString(input1); |
47 ASSERT_EQ(base::SHA256_LENGTH, output1.size()); | 47 ASSERT_EQ(crypto::SHA256_LENGTH, output1.size()); |
48 for (size_t i = 0; i < base::SHA256_LENGTH; i++) | 48 for (size_t i = 0; i < crypto::SHA256_LENGTH; i++) |
49 EXPECT_EQ(expected1[i], static_cast<uint8>(output1[i])); | 49 EXPECT_EQ(expected1[i], static_cast<uint8>(output1[i])); |
50 } | 50 } |
51 | 51 |
52 TEST(Sha256Test, Test2) { | 52 TEST(Sha256Test, Test2) { |
53 // Example B.2 from FIPS 180-2: multi-block message. | 53 // Example B.2 from FIPS 180-2: multi-block message. |
54 std::string input2 = | 54 std::string input2 = |
55 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; | 55 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; |
56 int expected2[] = { 0x24, 0x8d, 0x6a, 0x61, | 56 int expected2[] = { 0x24, 0x8d, 0x6a, 0x61, |
57 0xd2, 0x06, 0x38, 0xb8, | 57 0xd2, 0x06, 0x38, 0xb8, |
58 0xe5, 0xc0, 0x26, 0x93, | 58 0xe5, 0xc0, 0x26, 0x93, |
59 0x0c, 0x3e, 0x60, 0x39, | 59 0x0c, 0x3e, 0x60, 0x39, |
60 0xa3, 0x3c, 0xe4, 0x59, | 60 0xa3, 0x3c, 0xe4, 0x59, |
61 0x64, 0xff, 0x21, 0x67, | 61 0x64, 0xff, 0x21, 0x67, |
62 0xf6, 0xec, 0xed, 0xd4, | 62 0xf6, 0xec, 0xed, 0xd4, |
63 0x19, 0xdb, 0x06, 0xc1 }; | 63 0x19, 0xdb, 0x06, 0xc1 }; |
64 | 64 |
65 uint8 output2[base::SHA256_LENGTH]; | 65 uint8 output2[crypto::SHA256_LENGTH]; |
66 base::SHA256HashString(input2, output2, sizeof(output2)); | 66 crypto::SHA256HashString(input2, output2, sizeof(output2)); |
67 for (size_t i = 0; i < base::SHA256_LENGTH; i++) | 67 for (size_t i = 0; i < crypto::SHA256_LENGTH; i++) |
68 EXPECT_EQ(expected2[i], static_cast<int>(output2[i])); | 68 EXPECT_EQ(expected2[i], static_cast<int>(output2[i])); |
69 | 69 |
70 uint8 output_truncated2[6]; | 70 uint8 output_truncated2[6]; |
71 base::SHA256HashString(input2, output_truncated2, sizeof(output_truncated2)); | 71 crypto::SHA256HashString(input2, |
72 output_truncated2, sizeof(output_truncated2)); | |
72 for (size_t i = 0; i < sizeof(output_truncated2); i++) | 73 for (size_t i = 0; i < sizeof(output_truncated2); i++) |
73 EXPECT_EQ(expected2[i], static_cast<int>(output_truncated2[i])); | 74 EXPECT_EQ(expected2[i], static_cast<int>(output_truncated2[i])); |
74 } | 75 } |
75 | 76 |
76 TEST(Sha256Test, Test3) { | 77 TEST(Sha256Test, Test3) { |
77 // Example B.3 from FIPS 180-2: long message. | 78 // Example B.3 from FIPS 180-2: long message. |
78 std::string input3(1000000, 'a'); // 'a' repeated a million times | 79 std::string input3(1000000, 'a'); // 'a' repeated a million times |
79 int expected3[] = { 0xcd, 0xc7, 0x6e, 0x5c, | 80 int expected3[] = { 0xcd, 0xc7, 0x6e, 0x5c, |
80 0x99, 0x14, 0xfb, 0x92, | 81 0x99, 0x14, 0xfb, 0x92, |
81 0x81, 0xa1, 0xc7, 0xe2, | 82 0x81, 0xa1, 0xc7, 0xe2, |
82 0x84, 0xd7, 0x3e, 0x67, | 83 0x84, 0xd7, 0x3e, 0x67, |
83 0xf1, 0x80, 0x9a, 0x48, | 84 0xf1, 0x80, 0x9a, 0x48, |
84 0xa4, 0x97, 0x20, 0x0e, | 85 0xa4, 0x97, 0x20, 0x0e, |
85 0x04, 0x6d, 0x39, 0xcc, | 86 0x04, 0x6d, 0x39, 0xcc, |
86 0xc7, 0x11, 0x2c, 0xd0 }; | 87 0xc7, 0x11, 0x2c, 0xd0 }; |
87 | 88 |
88 uint8 output3[base::SHA256_LENGTH]; | 89 uint8 output3[crypto::SHA256_LENGTH]; |
89 base::SHA256HashString(input3, output3, sizeof(output3)); | 90 crypto::SHA256HashString(input3, output3, sizeof(output3)); |
90 for (size_t i = 0; i < base::SHA256_LENGTH; i++) | 91 for (size_t i = 0; i < crypto::SHA256_LENGTH; i++) |
91 EXPECT_EQ(expected3[i], static_cast<int>(output3[i])); | 92 EXPECT_EQ(expected3[i], static_cast<int>(output3[i])); |
92 | 93 |
93 uint8 output_truncated3[12]; | 94 uint8 output_truncated3[12]; |
94 base::SHA256HashString(input3, output_truncated3, sizeof(output_truncated3)); | 95 crypto::SHA256HashString(input3, |
96 output_truncated3, sizeof(output_truncated3)); | |
95 for (size_t i = 0; i < sizeof(output_truncated3); i++) | 97 for (size_t i = 0; i < sizeof(output_truncated3); i++) |
96 EXPECT_EQ(expected3[i], static_cast<int>(output_truncated3[i])); | 98 EXPECT_EQ(expected3[i], static_cast<int>(output_truncated3[i])); |
97 } | 99 } |
OLD | NEW |