hash_factory.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import base64
  2. import hashlib
  3. from fate_crypto.hash import sm3_hash
  4. from federatedml.util import consts
  5. SUPPORT_METHOD = [consts.MD5, consts.SHA1, consts.SHA224, consts.SHA256,
  6. consts.SHA384, consts.SHA512, consts.SM3, "none"]
  7. def compute_md5(value):
  8. return hashlib.md5(bytes(value, encoding='utf-8')).hexdigest()
  9. def compute_md5_base64(value):
  10. return str(base64.b64encode(hashlib.md5(bytes(value, encoding='utf-8')).digest()), "utf-8")
  11. def compute_md5_bytes(value):
  12. return hashlib.md5(bytes(value, encoding='utf-8')).digest()
  13. def compute_sha256(value):
  14. return hashlib.sha256(bytes(value, encoding='utf-8')).hexdigest()
  15. def compute_sha256_base64(value):
  16. return str(base64.b64encode(hashlib.sha256(bytes(value, encoding='utf-8')).digest()), "utf-8")
  17. def compute_sha256_bytes(value):
  18. return hashlib.sha256(bytes(value, encoding='utf-8')).digest()
  19. def compute_sha1(value):
  20. return hashlib.sha1(bytes(value, encoding='utf-8')).hexdigest()
  21. def compute_sha1_base64(value):
  22. return str(base64.b64encode(hashlib.sha1(bytes(value, encoding='utf-8')).digest()), "utf-8")
  23. def compute_sha1_bytes(value):
  24. return hashlib.sha1(bytes(value, encoding='utf-8')).digest()
  25. def compute_sha224(value):
  26. return hashlib.sha224(bytes(value, encoding='utf-8')).hexdigest()
  27. def compute_sha224_base64(value):
  28. return str(base64.b64encode(hashlib.sha224(bytes(value, encoding='utf-8')).digest()), "utf-8")
  29. def compute_sha224_bytes(value):
  30. return hashlib.sha224(bytes(value, encoding='utf-8')).digest()
  31. def compute_sha512(value):
  32. return hashlib.sha512(bytes(value, encoding='utf-8')).hexdigest()
  33. def compute_sha512_base64(value):
  34. return str(base64.b64encode(hashlib.sha512(bytes(value, encoding='utf-8')).digest()), "utf-8")
  35. def compute_sha512_bytes(value):
  36. return hashlib.sha512(bytes(value, encoding='utf-8')).digest()
  37. def compute_sha384(value):
  38. return hashlib.sha384(bytes(value, encoding='utf-8')).hexdigest()
  39. def compute_sha384_base64(value):
  40. return str(base64.b64encode(hashlib.sha384(bytes(value, encoding='utf-8')).digest()), "utf-8")
  41. def compute_sha384_bytes(value):
  42. return hashlib.sha384(bytes(value, encoding='utf-8')).digest()
  43. def compute_sm3(value):
  44. return sm3_hash(bytes(value, encoding='utf-8')).hex()
  45. def compute_sm3_base64(value):
  46. return str(base64.b64encode(sm3_hash(bytes(value, encoding='utf-8'))), "utf-8")
  47. def compute_sm3_bytes(value):
  48. return bytes(sm3_hash(bytes(value, encoding='utf-8')))
  49. def compute_no_hash(value):
  50. return str(value)
  51. def compute_no_hash_base64(value):
  52. return str(base64.b64encode(bytes(value, encoding='utf-8')), 'utf-8')
  53. def compute_no_hash_bytes(value):
  54. return bytes(str(value), encoding='utf-8')
  55. HASH_HEX_FUNCTION = {
  56. consts.MD5: compute_md5,
  57. consts.SHA1: compute_sha1,
  58. consts.SHA224: compute_sha224,
  59. consts.SHA256: compute_sha256,
  60. consts.SHA384: compute_sha384,
  61. consts.SHA512: compute_sha512,
  62. consts.SM3: compute_sm3,
  63. "none": compute_no_hash
  64. }
  65. HASH_BYTE_FUNCTION = {
  66. consts.MD5: compute_md5_bytes,
  67. consts.SHA1: compute_sha1_bytes,
  68. consts.SHA224: compute_sha224_bytes,
  69. consts.SHA256: compute_sha256_bytes,
  70. consts.SHA384: compute_sha384_bytes,
  71. consts.SHA512: compute_sha512_bytes,
  72. consts.SM3: compute_sm3_bytes,
  73. "none": compute_no_hash_bytes
  74. }
  75. HASH_BASE64_FUNCTION = {
  76. consts.MD5: compute_md5_base64,
  77. consts.SHA1: compute_sha1_base64,
  78. consts.SHA224: compute_sha224_base64,
  79. consts.SHA256: compute_sha256_base64,
  80. consts.SHA384: compute_sha384_base64,
  81. consts.SHA512: compute_sha512_base64,
  82. consts.SM3: compute_sm3_base64,
  83. "none": compute_no_hash_base64
  84. }
  85. class Hash:
  86. def __init__(self, method, base64=0, hex_output=True):
  87. self.method = method
  88. if self.method not in SUPPORT_METHOD:
  89. raise ValueError("Hash does not support method:{}".format(self.method))
  90. self.base64 = base64
  91. self.hex_output = hex_output
  92. if self.base64:
  93. self.hash_operator = HASH_BASE64_FUNCTION[self.method]
  94. if self.hex_output:
  95. self.hash_operator = HASH_HEX_FUNCTION[self.method]
  96. else:
  97. self.hash_operator = HASH_BYTE_FUNCTION[self.method]
  98. def compute(self, value, prefix_salt=None, suffix_salt=None):
  99. value = str(value)
  100. if prefix_salt:
  101. value = prefix_salt + value
  102. if suffix_salt:
  103. value = value + suffix_salt
  104. return self.hash_operator(value)