| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/sha1.h" | 5 #include "base/sha1.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 TEST(SHA1Test, Test1) { | 12 TEST(SHA1Test, Test1) { |
| 13 // Example A.1 from FIPS 180-2: one-block message. | 13 // Example A.1 from FIPS 180-2: one-block message. |
| 14 std::string input = "abc"; | 14 std::string input = "abc"; |
| 15 | 15 |
| 16 int expected[] = { 0xa9, 0x99, 0x3e, 0x36, | 16 int expected[] = { 0xa9, 0x99, 0x3e, 0x36, |
| 17 0x47, 0x06, 0x81, 0x6a, | 17 0x47, 0x06, 0x81, 0x6a, |
| 18 0xba, 0x3e, 0x25, 0x71, | 18 0xba, 0x3e, 0x25, 0x71, |
| 19 0x78, 0x50, 0xc2, 0x6c, | 19 0x78, 0x50, 0xc2, 0x6c, |
| 20 0x9c, 0xd0, 0xd8, 0x9d }; | 20 0x9c, 0xd0, 0xd8, 0x9d }; |
| 21 | 21 |
| 22 std::string output = base::SHA1HashString(input); | 22 std::string output = base::SHA1HashString(input); |
| 23 for (size_t i = 0; i < base::SHA1_LENGTH; i++) | 23 for (size_t i = 0; i < base::kSHA1Length; i++) |
| 24 EXPECT_EQ(expected[i], output[i] & 0xFF); | 24 EXPECT_EQ(expected[i], output[i] & 0xFF); |
| 25 } | 25 } |
| 26 | 26 |
| 27 TEST(SHA1Test, Test2) { | 27 TEST(SHA1Test, Test2) { |
| 28 // Example A.2 from FIPS 180-2: multi-block message. | 28 // Example A.2 from FIPS 180-2: multi-block message. |
| 29 std::string input = | 29 std::string input = |
| 30 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; | 30 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; |
| 31 | 31 |
| 32 int expected[] = { 0x84, 0x98, 0x3e, 0x44, | 32 int expected[] = { 0x84, 0x98, 0x3e, 0x44, |
| 33 0x1c, 0x3b, 0xd2, 0x6e, | 33 0x1c, 0x3b, 0xd2, 0x6e, |
| 34 0xba, 0xae, 0x4a, 0xa1, | 34 0xba, 0xae, 0x4a, 0xa1, |
| 35 0xf9, 0x51, 0x29, 0xe5, | 35 0xf9, 0x51, 0x29, 0xe5, |
| 36 0xe5, 0x46, 0x70, 0xf1 }; | 36 0xe5, 0x46, 0x70, 0xf1 }; |
| 37 | 37 |
| 38 std::string output = base::SHA1HashString(input); | 38 std::string output = base::SHA1HashString(input); |
| 39 for (size_t i = 0; i < base::SHA1_LENGTH; i++) | 39 for (size_t i = 0; i < base::kSHA1Length; i++) |
| 40 EXPECT_EQ(expected[i], output[i] & 0xFF); | 40 EXPECT_EQ(expected[i], output[i] & 0xFF); |
| 41 } | 41 } |
| 42 | 42 |
| 43 TEST(SHA1Test, Test3) { | 43 TEST(SHA1Test, Test3) { |
| 44 // Example A.3 from FIPS 180-2: long message. | 44 // Example A.3 from FIPS 180-2: long message. |
| 45 std::string input(1000000, 'a'); | 45 std::string input(1000000, 'a'); |
| 46 | 46 |
| 47 int expected[] = { 0x34, 0xaa, 0x97, 0x3c, | 47 int expected[] = { 0x34, 0xaa, 0x97, 0x3c, |
| 48 0xd4, 0xc4, 0xda, 0xa4, | 48 0xd4, 0xc4, 0xda, 0xa4, |
| 49 0xf6, 0x1e, 0xeb, 0x2b, | 49 0xf6, 0x1e, 0xeb, 0x2b, |
| 50 0xdb, 0xad, 0x27, 0x31, | 50 0xdb, 0xad, 0x27, 0x31, |
| 51 0x65, 0x34, 0x01, 0x6f }; | 51 0x65, 0x34, 0x01, 0x6f }; |
| 52 | 52 |
| 53 std::string output = base::SHA1HashString(input); | 53 std::string output = base::SHA1HashString(input); |
| 54 for (size_t i = 0; i < base::SHA1_LENGTH; i++) | 54 for (size_t i = 0; i < base::kSHA1Length; i++) |
| 55 EXPECT_EQ(expected[i], output[i] & 0xFF); | 55 EXPECT_EQ(expected[i], output[i] & 0xFF); |
| 56 } | 56 } |
| 57 | 57 |
| 58 TEST(SHA1Test, Test1Bytes) { | 58 TEST(SHA1Test, Test1Bytes) { |
| 59 // Example A.1 from FIPS 180-2: one-block message. | 59 // Example A.1 from FIPS 180-2: one-block message. |
| 60 std::string input = "abc"; | 60 std::string input = "abc"; |
| 61 unsigned char output[base::SHA1_LENGTH]; | 61 unsigned char output[base::kSHA1Length]; |
| 62 | 62 |
| 63 unsigned char expected[] = { 0xa9, 0x99, 0x3e, 0x36, | 63 unsigned char expected[] = { 0xa9, 0x99, 0x3e, 0x36, |
| 64 0x47, 0x06, 0x81, 0x6a, | 64 0x47, 0x06, 0x81, 0x6a, |
| 65 0xba, 0x3e, 0x25, 0x71, | 65 0xba, 0x3e, 0x25, 0x71, |
| 66 0x78, 0x50, 0xc2, 0x6c, | 66 0x78, 0x50, 0xc2, 0x6c, |
| 67 0x9c, 0xd0, 0xd8, 0x9d }; | 67 0x9c, 0xd0, 0xd8, 0x9d }; |
| 68 | 68 |
| 69 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), | 69 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), |
| 70 input.length(), output); | 70 input.length(), output); |
| 71 for (size_t i = 0; i < base::SHA1_LENGTH; i++) | 71 for (size_t i = 0; i < base::kSHA1Length; i++) |
| 72 EXPECT_EQ(expected[i], output[i]); | 72 EXPECT_EQ(expected[i], output[i]); |
| 73 } | 73 } |
| 74 | 74 |
| 75 TEST(SHA1Test, Test2Bytes) { | 75 TEST(SHA1Test, Test2Bytes) { |
| 76 // Example A.2 from FIPS 180-2: multi-block message. | 76 // Example A.2 from FIPS 180-2: multi-block message. |
| 77 std::string input = | 77 std::string input = |
| 78 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; | 78 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; |
| 79 unsigned char output[base::SHA1_LENGTH]; | 79 unsigned char output[base::kSHA1Length]; |
| 80 | 80 |
| 81 unsigned char expected[] = { 0x84, 0x98, 0x3e, 0x44, | 81 unsigned char expected[] = { 0x84, 0x98, 0x3e, 0x44, |
| 82 0x1c, 0x3b, 0xd2, 0x6e, | 82 0x1c, 0x3b, 0xd2, 0x6e, |
| 83 0xba, 0xae, 0x4a, 0xa1, | 83 0xba, 0xae, 0x4a, 0xa1, |
| 84 0xf9, 0x51, 0x29, 0xe5, | 84 0xf9, 0x51, 0x29, 0xe5, |
| 85 0xe5, 0x46, 0x70, 0xf1 }; | 85 0xe5, 0x46, 0x70, 0xf1 }; |
| 86 | 86 |
| 87 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), | 87 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), |
| 88 input.length(), output); | 88 input.length(), output); |
| 89 for (size_t i = 0; i < base::SHA1_LENGTH; i++) | 89 for (size_t i = 0; i < base::kSHA1Length; i++) |
| 90 EXPECT_EQ(expected[i], output[i]); | 90 EXPECT_EQ(expected[i], output[i]); |
| 91 } | 91 } |
| 92 | 92 |
| 93 TEST(SHA1Test, Test3Bytes) { | 93 TEST(SHA1Test, Test3Bytes) { |
| 94 // Example A.3 from FIPS 180-2: long message. | 94 // Example A.3 from FIPS 180-2: long message. |
| 95 std::string input(1000000, 'a'); | 95 std::string input(1000000, 'a'); |
| 96 unsigned char output[base::SHA1_LENGTH]; | 96 unsigned char output[base::kSHA1Length]; |
| 97 | 97 |
| 98 unsigned char expected[] = { 0x34, 0xaa, 0x97, 0x3c, | 98 unsigned char expected[] = { 0x34, 0xaa, 0x97, 0x3c, |
| 99 0xd4, 0xc4, 0xda, 0xa4, | 99 0xd4, 0xc4, 0xda, 0xa4, |
| 100 0xf6, 0x1e, 0xeb, 0x2b, | 100 0xf6, 0x1e, 0xeb, 0x2b, |
| 101 0xdb, 0xad, 0x27, 0x31, | 101 0xdb, 0xad, 0x27, 0x31, |
| 102 0x65, 0x34, 0x01, 0x6f }; | 102 0x65, 0x34, 0x01, 0x6f }; |
| 103 | 103 |
| 104 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), | 104 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), |
| 105 input.length(), output); | 105 input.length(), output); |
| 106 for (size_t i = 0; i < base::SHA1_LENGTH; i++) | 106 for (size_t i = 0; i < base::kSHA1Length; i++) |
| 107 EXPECT_EQ(expected[i], output[i]); | 107 EXPECT_EQ(expected[i], output[i]); |
| 108 } | 108 } |
| OLD | NEW |