sha1_unittest.cc 5.6 KB

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