sha1_unittest.cc 3.7 KB

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