James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/sha1.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include "base/basictypes.h" |
Sean Klein | a673836 | 2015-12-02 15:11:37 -0800 | [diff] [blame] | 10 | #include "base/files/scoped_file.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | TEST(SHA1Test, Test1) { |
| 14 | // Example A.1 from FIPS 180-2: one-block message. |
| 15 | std::string input = "abc"; |
| 16 | |
| 17 | int expected[] = { 0xa9, 0x99, 0x3e, 0x36, |
| 18 | 0x47, 0x06, 0x81, 0x6a, |
| 19 | 0xba, 0x3e, 0x25, 0x71, |
| 20 | 0x78, 0x50, 0xc2, 0x6c, |
| 21 | 0x9c, 0xd0, 0xd8, 0x9d }; |
| 22 | |
| 23 | std::string output = base::SHA1HashString(input); |
| 24 | for (size_t i = 0; i < base::kSHA1Length; i++) |
| 25 | EXPECT_EQ(expected[i], output[i] & 0xFF); |
| 26 | } |
| 27 | |
| 28 | TEST(SHA1Test, Test2) { |
| 29 | // Example A.2 from FIPS 180-2: multi-block message. |
| 30 | std::string input = |
| 31 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; |
| 32 | |
| 33 | int expected[] = { 0x84, 0x98, 0x3e, 0x44, |
| 34 | 0x1c, 0x3b, 0xd2, 0x6e, |
| 35 | 0xba, 0xae, 0x4a, 0xa1, |
| 36 | 0xf9, 0x51, 0x29, 0xe5, |
| 37 | 0xe5, 0x46, 0x70, 0xf1 }; |
| 38 | |
| 39 | std::string output = base::SHA1HashString(input); |
| 40 | for (size_t i = 0; i < base::kSHA1Length; i++) |
| 41 | EXPECT_EQ(expected[i], output[i] & 0xFF); |
| 42 | } |
| 43 | |
| 44 | TEST(SHA1Test, Test3) { |
| 45 | // Example A.3 from FIPS 180-2: long message. |
| 46 | std::string input(1000000, 'a'); |
| 47 | |
| 48 | int expected[] = { 0x34, 0xaa, 0x97, 0x3c, |
| 49 | 0xd4, 0xc4, 0xda, 0xa4, |
| 50 | 0xf6, 0x1e, 0xeb, 0x2b, |
| 51 | 0xdb, 0xad, 0x27, 0x31, |
| 52 | 0x65, 0x34, 0x01, 0x6f }; |
| 53 | |
| 54 | std::string output = base::SHA1HashString(input); |
| 55 | for (size_t i = 0; i < base::kSHA1Length; i++) |
| 56 | EXPECT_EQ(expected[i], output[i] & 0xFF); |
| 57 | } |
| 58 | |
| 59 | TEST(SHA1Test, Test1Bytes) { |
| 60 | // Example A.1 from FIPS 180-2: one-block message. |
| 61 | std::string input = "abc"; |
| 62 | unsigned char output[base::kSHA1Length]; |
| 63 | |
| 64 | unsigned char expected[] = { 0xa9, 0x99, 0x3e, 0x36, |
| 65 | 0x47, 0x06, 0x81, 0x6a, |
| 66 | 0xba, 0x3e, 0x25, 0x71, |
| 67 | 0x78, 0x50, 0xc2, 0x6c, |
| 68 | 0x9c, 0xd0, 0xd8, 0x9d }; |
| 69 | |
| 70 | base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), |
| 71 | input.length(), output); |
| 72 | for (size_t i = 0; i < base::kSHA1Length; i++) |
| 73 | EXPECT_EQ(expected[i], output[i]); |
| 74 | } |
| 75 | |
| 76 | TEST(SHA1Test, Test2Bytes) { |
| 77 | // Example A.2 from FIPS 180-2: multi-block message. |
| 78 | std::string input = |
| 79 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; |
| 80 | unsigned char output[base::kSHA1Length]; |
| 81 | |
| 82 | unsigned char expected[] = { 0x84, 0x98, 0x3e, 0x44, |
| 83 | 0x1c, 0x3b, 0xd2, 0x6e, |
| 84 | 0xba, 0xae, 0x4a, 0xa1, |
| 85 | 0xf9, 0x51, 0x29, 0xe5, |
| 86 | 0xe5, 0x46, 0x70, 0xf1 }; |
| 87 | |
| 88 | base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), |
| 89 | input.length(), output); |
| 90 | for (size_t i = 0; i < base::kSHA1Length; i++) |
| 91 | EXPECT_EQ(expected[i], output[i]); |
| 92 | } |
| 93 | |
| 94 | TEST(SHA1Test, Test3Bytes) { |
| 95 | // Example A.3 from FIPS 180-2: long message. |
| 96 | std::string input(1000000, 'a'); |
| 97 | unsigned char output[base::kSHA1Length]; |
| 98 | |
| 99 | unsigned char expected[] = { 0x34, 0xaa, 0x97, 0x3c, |
| 100 | 0xd4, 0xc4, 0xda, 0xa4, |
| 101 | 0xf6, 0x1e, 0xeb, 0x2b, |
| 102 | 0xdb, 0xad, 0x27, 0x31, |
| 103 | 0x65, 0x34, 0x01, 0x6f }; |
| 104 | |
| 105 | base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()), |
| 106 | input.length(), output); |
| 107 | for (size_t i = 0; i < base::kSHA1Length; i++) |
| 108 | EXPECT_EQ(expected[i], output[i]); |
| 109 | } |
Sean Klein | a673836 | 2015-12-02 15:11:37 -0800 | [diff] [blame] | 110 | |
| 111 | // Helper function to test SHA1HashFile. |
| 112 | void doHashFile(const std::string& input, unsigned char* output) { |
| 113 | std::string file_name = "sha1_test_data.txt"; |
| 114 | base::ScopedFILE file(fopen(file_name.c_str(), "w+")); |
| 115 | EXPECT_NE(nullptr, file); |
| 116 | EXPECT_EQ(input.length(), |
| 117 | fwrite(input.c_str(), sizeof(char), input.length(), file.get())); |
| 118 | EXPECT_EQ(0, fflush(file.get())); |
| 119 | EXPECT_EQ(true, base::SHA1HashFile(file_name, output)); |
| 120 | EXPECT_EQ(0, unlink(file_name.c_str())); |
| 121 | } |
| 122 | |
| 123 | TEST(SHA1Test, Test1File) { |
| 124 | // Example A.1 from FIPS 180-2: one-block message. |
| 125 | std::string input = "abc"; |
| 126 | unsigned char output[base::kSHA1Length]; |
| 127 | |
| 128 | const unsigned char expected[] = { 0xa9, 0x99, 0x3e, 0x36, |
| 129 | 0x47, 0x06, 0x81, 0x6a, |
| 130 | 0xba, 0x3e, 0x25, 0x71, |
| 131 | 0x78, 0x50, 0xc2, 0x6c, |
| 132 | 0x9c, 0xd0, 0xd8, 0x9d }; |
| 133 | |
| 134 | doHashFile(input, output); |
| 135 | for (size_t i = 0; i < base::kSHA1Length; i++) |
| 136 | EXPECT_EQ(expected[i], output[i]); |
| 137 | } |
| 138 | |
| 139 | TEST(SHA1Test, Test2File) { |
| 140 | // Example A.2 from FIPS 180-2: multi-block message. |
| 141 | std::string input = |
| 142 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; |
| 143 | unsigned char output[base::kSHA1Length]; |
| 144 | |
| 145 | const unsigned char expected[] = { 0x84, 0x98, 0x3e, 0x44, |
| 146 | 0x1c, 0x3b, 0xd2, 0x6e, |
| 147 | 0xba, 0xae, 0x4a, 0xa1, |
| 148 | 0xf9, 0x51, 0x29, 0xe5, |
| 149 | 0xe5, 0x46, 0x70, 0xf1 }; |
| 150 | |
| 151 | doHashFile(input, output); |
| 152 | for (size_t i = 0; i < base::kSHA1Length; i++) |
| 153 | EXPECT_EQ(expected[i], output[i]); |
| 154 | } |
| 155 | |
| 156 | TEST(SHA1Test, Test3File) { |
| 157 | // Example A.3 from FIPS 180-2: long message. |
| 158 | std::string input(1000000, 'a'); |
| 159 | unsigned char output[base::kSHA1Length]; |
| 160 | |
| 161 | const unsigned char expected[] = { 0x34, 0xaa, 0x97, 0x3c, |
| 162 | 0xd4, 0xc4, 0xda, 0xa4, |
| 163 | 0xf6, 0x1e, 0xeb, 0x2b, |
| 164 | 0xdb, 0xad, 0x27, 0x31, |
| 165 | 0x65, 0x34, 0x01, 0x6f }; |
| 166 | |
| 167 | doHashFile(input, output); |
| 168 | for (size_t i = 0; i < base::kSHA1Length; i++) |
| 169 | EXPECT_EQ(expected[i], output[i]); |
| 170 | } |