test_hashes.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # test_hashes.py
  2. #
  3. # Copyright (C) 2006-2016 wolfSSL Inc.
  4. #
  5. # This file is part of wolfSSL. (formerly known as CyaSSL)
  6. #
  7. # wolfSSL is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # wolfSSL is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. import unittest
  21. from wolfcrypt.hashes import *
  22. from wolfcrypt.utils import t2b, h2b
  23. class TestSha(unittest.TestCase):
  24. _class = Sha
  25. digest = t2b("1b6182d68ae91ce0853bd9c6b6edfedd4b6a510d")
  26. def setUp(self):
  27. self.hash = self._class()
  28. def test_new(self):
  29. # update inside constructor
  30. assert self._class.new("wolfcrypt").hexdigest() == self.digest
  31. def test_hash_update_001(self):
  32. self.hash.update("wolfcrypt")
  33. assert self.hash.hexdigest() == self.digest
  34. assert self.hash.digest() == h2b(self.digest)
  35. def test_hash_update_002(self):
  36. self.hash.update("wolf")
  37. self.hash.update("crypt")
  38. assert self.hash.hexdigest() == self.digest
  39. assert self.hash.digest() == h2b(self.digest)
  40. def test_hash_copy(self):
  41. copy = self.hash.copy()
  42. assert self.hash.hexdigest() == copy.hexdigest()
  43. self.hash.update("wolfcrypt")
  44. assert self.hash.hexdigest() != copy.hexdigest()
  45. copy.update("wolfcrypt")
  46. assert self.hash.hexdigest() == copy.hexdigest() == self.digest
  47. class TestSha256(TestSha):
  48. _class = Sha256
  49. digest = t2b("96e02e7b1cbcd6f104fe1fdb4652027a" \
  50. + "5505b68652b70095c6318f9dce0d1844")
  51. class TestSha384(TestSha):
  52. _class = Sha384
  53. digest = t2b("4c79d80531203a16f91bee325f18c6aada47f9382fe44fc1" \
  54. + "1f92917837e9b7902f5dccb7d3656f667a1dce3460bc884b")
  55. class TestSha512(TestSha):
  56. _class = Sha512
  57. digest = t2b("88fcf67ffd8558d713f9cedcd852db47" \
  58. + "9e6573f0bd9955610a993f609637553c" \
  59. + "e8fff55e644ee8a106aae19c07f91b3f" \
  60. + "2a2a6d40dfa7302c0fa6a1a9a5bfa03f")
  61. _HMAC_KEY = "python"
  62. class TestHmacSha(unittest.TestCase):
  63. _class = HmacSha
  64. digest = t2b("5dfabcfb3a25540824867cd21f065f52f73491e0")
  65. def setUp(self):
  66. self.hash = self._class(_HMAC_KEY)
  67. def test_new(self):
  68. # update inside constructor
  69. assert self._class.new(_HMAC_KEY,"wolfcrypt").hexdigest() == self.digest
  70. def test_hash_update_001(self):
  71. self.hash.update("wolfcrypt")
  72. assert self.hash.hexdigest() == self.digest
  73. def test_hash_update_002(self):
  74. self.hash.update("wolf")
  75. self.hash.update("crypt")
  76. assert self.hash.hexdigest() == self.digest
  77. def test_hash_copy(self):
  78. copy = self.hash.copy()
  79. assert self.hash.hexdigest() == copy.hexdigest()
  80. self.hash.update("wolfcrypt")
  81. assert self.hash.hexdigest() != copy.hexdigest()
  82. copy.update("wolfcrypt")
  83. assert self.hash.hexdigest() == copy.hexdigest() == self.digest
  84. class TestHmacSha256(TestHmacSha):
  85. _class = HmacSha256
  86. digest = t2b("4b641d721493d80f019d9447830ebfee" \
  87. + "89234a7d594378b89f8bb73873576bf6")
  88. class TestHmacSha384(TestHmacSha):
  89. _class = HmacSha384
  90. digest = t2b("e72c72070c9c5c78e3286593068a510c1740cdf9dc34b512" \
  91. + "ccec97320295db1fe673216b46fe72e81f399a9ec04780ab")
  92. class TestHmacSha512(TestHmacSha):
  93. _class = HmacSha512
  94. digest = t2b("c7f48db79314fc2b5be9a93fd58601a1" \
  95. + "bf42f397ec7f66dba034d44503890e6b" \
  96. + "5708242dcd71a248a78162d815c685f6" \
  97. + "038a4ac8cb34b8bf18986dbd300c9b41")