encode_test.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import unittest
  2. from federatedml.secureprotol.encode import Encode
  3. class TestEncode(unittest.TestCase):
  4. def test_compute(self):
  5. value_list = ["12345", "54321", "111111"]
  6. pre_salt = ""
  7. postfit_salt = "12345"
  8. sha256_base64_value_list = [
  9. "5KCpDlrAfVQ1xvJcTPfMVlvst5e7W4PFFbxCfvMqR3A=",
  10. "O90AvF51EJNqKGewUOtH4tUFyKM2y5NnlbUwRGb7kQw=",
  11. "M1RdblccBKI/fUivm9yrH3cCkK8lnmYYJPBe8E2FDWM="]
  12. sha256_value_list = [
  13. "e4a0a90e5ac07d5435c6f25c4cf7cc565becb797bb5b83c515bc427ef32a4770",
  14. "3bdd00bc5e7510936a2867b050eb47e2d505c8a336cb936795b5304466fb910c",
  15. "33545d6e571c04a23f7d48af9bdcab1f770290af259e661824f05ef04d850d63"]
  16. md5_value_list = ["8cfa2282b17de0a598c010f5f0109e7d",
  17. "64bddc3ca51ad547e43f8e65cb5e2318",
  18. "ff02c8a17f7fd875bd2e6d882fe7677d"]
  19. md5_base64_value_list = ["jPoigrF94KWYwBD18BCefQ==",
  20. "ZL3cPKUa1UfkP45ly14jGA==",
  21. "/wLIoX9/2HW9Lm2IL+dnfQ=="]
  22. sha1_base64_value_list = ["bur67wEzGYIqHzBAelNT93i1l5A=",
  23. "hhrM2zkxjD26v53sNtImywM5Q7k=",
  24. "X1M3cC7+mBF8IIHT6aghS/JJXvc="]
  25. sha1_value_list = ["6eeafaef013319822a1f30407a5353f778b59790",
  26. "861accdb39318c3dbabf9dec36d226cb033943b9",
  27. "5f5337702efe98117c2081d3e9a8214bf2495ef7"]
  28. sha224_base64_value_list = ["uVsQjSHFEq2gQLixymFYv5Ieht4p9v+MHr64kw==",
  29. "zAKzPY0k41ZbCyqTY4cBeNqOo7R7uca0f36Pjg==",
  30. "cNN9OVWrPqJ2g/Ve/w395o30Jxy7W3ol8NrF4w=="]
  31. sha224_value_list = ["b95b108d21c512ada040b8b1ca6158bf921e86de29f6ff8c1ebeb893",
  32. "cc02b33d8d24e3565b0b2a9363870178da8ea3b47bb9c6b47f7e8f8e",
  33. "70d37d3955ab3ea27683f55eff0dfde68df4271cbb5b7a25f0dac5e3"]
  34. sha512_base64_value_list = [
  35. "bnhO4jFWKBnxwBlowI2zlaF0cLRFlDFIBLI1jvRI94Ohq6KkvhZSm+HoC6bzEPaIFzjBzGx3kOZlLdnNlNJaVg==",
  36. "teHJ2xDTAHht99rky6eqMDaiINvTl30OzXq7sL9Dkk3NQ5GOJSd4ozempcTPgi8XD+uVtovhUPPkCOh1zJLLjg==",
  37. "LWE4K5RVIJ3p8RQ6JMuT/YYfIz/T5CPdx/8Z4+ywB7DOOS/7wFsR5pMrAvLb5u3G0auCf6d3lA4v69D7Vk98/Q=="]
  38. sha512_value_list = [
  39. "6e784ee231562819f1c01968c08db395a17470b44594314804b2358ef448f783a1aba2a4be16529be1e80ba6f310f6881738c1cc6c7790e6652dd9cd94d25a56",
  40. "b5e1c9db10d300786df7dae4cba7aa3036a220dbd3977d0ecd7abbb0bf43924dcd43918e252778a337a6a5c4cf822f170feb95b68be150f3e408e875cc92cb8e",
  41. "2d61382b9455209de9f1143a24cb93fd861f233fd3e423ddc7ff19e3ecb007b0ce392ffbc05b11e6932b02f2dbe6edc6d1ab827fa777940e2febd0fb564f7cfd"]
  42. sha384_base64_value_list = ["VQY6S0eKPD7KBYLKeBZ00Ys8Zr1HYUW4D9J82hmaDDBkNpoq9mvWchRo/isr/9Cb",
  43. "iF8kMFkBmrFbmUUrb0qs4j5ZWRXACfpKUUZr/4SqaI96EMJn7Atfk/z8JMoDyK4j",
  44. "wdy0BD5/rrkj2ABLchnCWMXGK6HKUc9NM23cepUdgEX9isxxEjynWfWkNwfObFpe"]
  45. sha384_value_list = [
  46. "55063a4b478a3c3eca0582ca781674d18b3c66bd476145b80fd27cda199a0c3064369a2af66bd6721468fe2b2bffd09b",
  47. "885f243059019ab15b99452b6f4aace23e595915c009fa4a51466bff84aa688f7a10c267ec0b5f93fcfc24ca03c8ae23",
  48. "c1dcb4043e7faeb923d8004b7219c258c5c62ba1ca51cf4d336ddc7a951d8045fd8acc71123ca759f5a43707ce6c5a5e"]
  49. # test sha256, base64 = 1
  50. # encode_sha256_base64 = Encode("sha256", base64=1)
  51. # self.assertEqual(encode_sha256_base64.compute(value_list, pre_salt, postfit_salt)[0], sha256_base64_value_list)
  52. # test sha256, base64 = 0
  53. # encode_sha256 = Encode("sha256", base64=0)
  54. # self.assertEqual(encode_sha256.compute(value_list, pre_salt, postfit_salt)[0], sha256_value_list)
  55. # test md5, base64 = 1
  56. # encode_md5_base64 = Encode("md5", base64=1)
  57. # self.assertEqual(encode_md5_base64.compute(value_list, pre_salt, postfit_salt)[0], md5_base64_value_list)
  58. # test md5, base64 = 0
  59. # encode_md5 = Encode("md5", base64=0)
  60. # self.assertEqual(encode_md5.compute(value_list, pre_salt, postfit_salt)[0], md5_value_list)
  61. # test sha1, base64 = 1
  62. # encode_base64_sha1 = Encode("sha1", base64=1)
  63. # self.assertEqual(encode_base64_sha1.compute(value_list, pre_salt, postfit_salt)[0], sha1_base64_value_list)
  64. # test sha1, base64 = 0
  65. # encode_sha1 = Encode("sha1", base64=0)
  66. # self.assertEqual(encode_sha1.compute(value_list, pre_salt, postfit_salt)[0], sha1_value_list)
  67. # test sha1, base64 = 1
  68. # encode_base_sha224 = Encode("sha224", base64=1)
  69. # self.assertEqual(encode_base_sha224.compute(value_list, pre_salt, postfit_salt)[0], sha224_base64_value_list)
  70. # test sha224, base64 = 0
  71. # encode_sha224 = Encode("sha224", base64=0)
  72. # self.assertEqual(encode_sha224.compute(value_list, pre_salt, postfit_salt)[0], sha224_value_list)
  73. # test sha512, base64 = 1
  74. # encode_base_sha512 = Encode("sha512", base64=1)
  75. # self.assertEqual(encode_base_sha512.compute(value_list, pre_salt, postfit_salt)[0], sha512_base64_value_list)
  76. # test sha224, base64 = 0
  77. # encode_sha512 = Encode("sha512", base64=0)
  78. # self.assertEqual(encode_sha512.compute(value_list, pre_salt, postfit_salt)[0], sha512_value_list)
  79. # test sha384, base64 = 1
  80. # encode_base_sha384 = Encode("sha384", base64=1)
  81. # self.assertEqual(encode_base_sha384.compute(value_list, pre_salt, postfit_salt)[0], sha384_base64_value_list)
  82. # test sha384, base64 = 0
  83. # encode_sha384 = Encode("sha384", base64=0)
  84. # self.assertEqual(encode_sha384.compute(value_list, pre_salt, postfit_salt)[0], sha384_value_list)
  85. #
  86. # test id_map
  87. # encode_sha384 = Encode("sha384", base64=0)
  88. # sha384_value_list = [
  89. # "55063a4b478a3c3eca0582ca781674d18b3c66bd476145b80fd27cda199a0c3064369a2af66bd6721468fe2b2bffd09b",
  90. # "885f243059019ab15b99452b6f4aace23e595915c009fa4a51466bff84aa688f7a10c267ec0b5f93fcfc24ca03c8ae23",
  91. # "c1dcb4043e7faeb923d8004b7219c258c5c62ba1ca51cf4d336ddc7a951d8045fd8acc71123ca759f5a43707ce6c5a5e"]
  92. # encode_value_list, id_map_pair = encode_sha384.compute(value_list, pre_salt, postfit_salt, id_map=True)
  93. # id_map_value_list = []
  94. # for i in range(len(encode_value_list)):
  95. # id_map_value_list.append(id_map_pair[encode_value_list[i]])
  96. #
  97. # self.assertEqual(id_map_value_list, value_list)
  98. ########## test single value #####################
  99. value = value_list[0]
  100. # test sha256, base64 = 1
  101. encode_sha256_base64 = Encode("sha256", base64=1)
  102. sha256_base64_value = sha256_base64_value_list[0]
  103. self.assertEqual(encode_sha256_base64.compute(value, pre_salt, postfit_salt), sha256_base64_value)
  104. # test sha256, base64 = 0
  105. encode_sha256 = Encode("sha256", base64=0)
  106. sha256_value = sha256_value_list[0]
  107. self.assertEqual(encode_sha256.compute(value, pre_salt, postfit_salt), sha256_value)
  108. # test md5, base64 = 1
  109. encode_md5_base64 = Encode("md5", base64=1)
  110. md5_base64_value = md5_base64_value_list[0]
  111. self.assertEqual(encode_md5_base64.compute(value, pre_salt, postfit_salt), md5_base64_value)
  112. # test md5, base64 = 0
  113. encode_md5 = Encode("md5", base64=0)
  114. md5_value = md5_value_list[0]
  115. self.assertEqual(encode_md5.compute(value, pre_salt, postfit_salt), md5_value)
  116. # test sha1, base64 = 1
  117. encode_base64_sha1 = Encode("sha1", base64=1)
  118. sha1_base64_value = sha1_base64_value_list[0]
  119. self.assertEqual(encode_base64_sha1.compute(value, pre_salt, postfit_salt), sha1_base64_value)
  120. # test sha1, base64 = 0
  121. encode_sha1 = Encode("sha1", base64=0)
  122. sha1_value = sha1_value_list[0]
  123. self.assertEqual(encode_sha1.compute(value, pre_salt, postfit_salt), sha1_value)
  124. # test sha1, base64 = 1
  125. encode_base_sha224 = Encode("sha224", base64=1)
  126. sha224_base64_value = sha224_base64_value_list[0]
  127. self.assertEqual(encode_base_sha224.compute(value, pre_salt, postfit_salt), sha224_base64_value)
  128. # test sha224, base64 = 0
  129. encode_sha224 = Encode("sha224", base64=0)
  130. sha224_value = sha224_value_list[0]
  131. self.assertEqual(encode_sha224.compute(value, pre_salt, postfit_salt), sha224_value)
  132. # test sha512, base64 = 1
  133. encode_base_sha512 = Encode("sha512", base64=1)
  134. sha512_base64_value = sha512_base64_value_list[0]
  135. self.assertEqual(encode_base_sha512.compute(value, pre_salt, postfit_salt), sha512_base64_value)
  136. # test sha224, base64 = 0
  137. encode_sha512 = Encode("sha512", base64=0)
  138. sha512_value = sha512_value_list[0]
  139. self.assertEqual(encode_sha512.compute(value, pre_salt, postfit_salt), sha512_value)
  140. # test sha384, base64 = 1
  141. encode_base_sha384 = Encode("sha384", base64=1)
  142. sha384_base64_value = sha384_base64_value_list[0]
  143. self.assertEqual(encode_base_sha384.compute(value, pre_salt, postfit_salt), sha384_base64_value)
  144. # test sha384, base64 = 0
  145. encode_sha384 = Encode("sha384", base64=0)
  146. sha384_value = sha384_value_list[0]
  147. self.assertEqual(encode_sha384.compute(value, pre_salt, postfit_salt), sha384_value)
  148. # test is_support
  149. support_encode_method = ["md5", "sha1", "sha224", "sha256", "sha384", "sha512"]
  150. for method in support_encode_method:
  151. self.assertTrue(Encode.is_support(method))
  152. unsupport_method = "sha2"
  153. self.assertFalse(Encode.is_support(unsupport_method))
  154. # test conpute unsupport method
  155. test_compute = Encode("sha3840000", base64=0)
  156. self.assertEqual(test_compute.compute(value, pre_salt, postfit_salt), value)
  157. if __name__ == '__main__':
  158. unittest.main()