1 // Copyright 2011 The Chromium Authors
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/hash/sha1.h"
6
7 #include <stddef.h>
8
9 #include <string>
10
11 #include "base/base64.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
TEST(SHA1Test,Test1)14 TEST(SHA1Test, Test1) {
15 // Example A.1 from FIPS 180-2: one-block message.
16 std::string input = "abc";
17
18 static constexpr int kExpected[] = {0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81,
19 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50,
20 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d};
21
22 std::string output = base::SHA1HashString(input);
23 ASSERT_EQ(base::kSHA1Length, output.size());
24 for (size_t i = 0; i < base::kSHA1Length; i++)
25 EXPECT_EQ(kExpected[i], output[i] & 0xFF);
26 }
27
TEST(SHA1Test,Test2)28 TEST(SHA1Test, Test2) {
29 // Example A.2 from FIPS 180-2: multi-block message.
30 std::string input =
31 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
32
33 static constexpr int kExpected[] = {0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2,
34 0x6e, 0xba, 0xae, 0x4a, 0xa1, 0xf9, 0x51,
35 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1};
36
37 std::string output = base::SHA1HashString(input);
38 ASSERT_EQ(base::kSHA1Length, output.size());
39 for (size_t i = 0; i < base::kSHA1Length; i++)
40 EXPECT_EQ(kExpected[i], output[i] & 0xFF);
41 }
42
TEST(SHA1Test,Test3)43 TEST(SHA1Test, Test3) {
44 // Example A.3 from FIPS 180-2: long message.
45 std::string input(1000000, 'a');
46
47 static constexpr int kExpected[] = {0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda,
48 0xa4, 0xf6, 0x1e, 0xeb, 0x2b, 0xdb, 0xad,
49 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f};
50
51 std::string output = base::SHA1HashString(input);
52 ASSERT_EQ(base::kSHA1Length, output.size());
53 for (size_t i = 0; i < base::kSHA1Length; i++)
54 EXPECT_EQ(kExpected[i], output[i] & 0xFF);
55 }
56
TEST(SHA1Test,Test1BytesAndSpan)57 TEST(SHA1Test, Test1BytesAndSpan) {
58 // Example A.1 from FIPS 180-2: one-block message.
59 std::string input = "abc";
60 unsigned char output[base::kSHA1Length];
61
62 static constexpr unsigned char kExpected[] = {
63 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
64 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d};
65
66 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()),
67 input.size(), output);
68 for (size_t i = 0; i < base::kSHA1Length; i++)
69 EXPECT_EQ(kExpected[i], output[i]);
70
71 base::SHA1Digest output_array = base::SHA1HashSpan(base::as_byte_span(input));
72 for (size_t i = 0; i < base::kSHA1Length; i++)
73 EXPECT_EQ(kExpected[i], output_array[i]);
74 }
75
TEST(SHA1Test,Test2BytesAndSpan)76 TEST(SHA1Test, Test2BytesAndSpan) {
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 static constexpr unsigned char kExpected[] = {
83 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae,
84 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1};
85
86 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()),
87 input.size(), output);
88 for (size_t i = 0; i < base::kSHA1Length; i++)
89 EXPECT_EQ(kExpected[i], output[i]);
90
91 base::SHA1Digest output_array = base::SHA1HashSpan(base::as_byte_span(input));
92 for (size_t i = 0; i < base::kSHA1Length; i++)
93 EXPECT_EQ(kExpected[i], output_array[i]);
94 }
95
TEST(SHA1Test,Test3BytesAndSpan)96 TEST(SHA1Test, Test3BytesAndSpan) {
97 // Example A.3 from FIPS 180-2: long message.
98 std::string input(1000000, 'a');
99 unsigned char output[base::kSHA1Length];
100
101 static constexpr unsigned char kExpected[] = {
102 0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e,
103 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f};
104
105 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(input.c_str()),
106 input.size(), output);
107 for (size_t i = 0; i < base::kSHA1Length; i++)
108 EXPECT_EQ(kExpected[i], output[i]);
109
110 base::SHA1Digest output_array = base::SHA1HashSpan(base::as_byte_span(input));
111 for (size_t i = 0; i < base::kSHA1Length; i++)
112 EXPECT_EQ(kExpected[i], output_array[i]);
113 }
114
TEST(SHA1Test,StreamingSHA1WithWholeInput)115 TEST(SHA1Test, StreamingSHA1WithWholeInput) {
116 // Example A.1 from FIPS 180-2: one-block message.
117 std::string input = "abc";
118 static constexpr unsigned char kExpected[] = {
119 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e,
120 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d};
121
122 base::SHA1Context context;
123 base::SHA1Digest digest;
124 base::SHA1Init(context);
125 base::SHA1Update(input, context);
126 base::SHA1Final(context, digest);
127
128 unsigned char* digest_array = digest.data();
129 for (size_t i = 0; i < base::kSHA1Length; ++i) {
130 EXPECT_EQ(kExpected[i], digest_array[i]);
131 }
132 }
133
TEST(SHA1Test,StreamingSHA1WithChunkedInput)134 TEST(SHA1Test, StreamingSHA1WithChunkedInput) {
135 // Example A.3 from FIPS 180-2: long message, split into 2 updates.
136 std::string input1(500000, 'a');
137 std::string input2(500000, 'a');
138 std::string input = input1 + input2;
139 static constexpr unsigned char kExpected[] = {
140 0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e,
141 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f};
142
143 base::SHA1Context context;
144 base::SHA1Digest digest;
145
146 base::SHA1Init(context);
147 base::SHA1Update(input1, context);
148 base::SHA1Update(input2, context);
149 base::SHA1Final(context, digest);
150
151 unsigned char* digest_array = digest.data();
152 for (size_t i = 0; i < base::kSHA1Length; ++i) {
153 EXPECT_EQ(kExpected[i], digest_array[i]);
154 }
155 }
156