xbase64.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # update: 2021-6-30-14
  2. import base64
  3. import hashlib
  4. def str_to_b64(string):
  5. """b64加密"""
  6. try:
  7. return str(base64.b64encode(str(string).encode('utf-8')), 'utf-8')
  8. except Exception as exception:
  9. print(f"exception: {exception.__class__.__name__}")
  10. return str()
  11. def b64_to_str(string):
  12. """b64加密"""
  13. try:
  14. return str(base64.b64decode(str(string).encode('utf-8')), 'utf-8')
  15. except Exception as exception:
  16. print(f"exception: {exception.__class__.__name__}")
  17. return str()
  18. def text_to_byte(text, mode='utf-8'):
  19. return text.encode(mode)
  20. def byte_to_text(byte, mode='utf-8'):
  21. return byte.decode(mode)
  22. def text_to_b64(text):
  23. """b64编码"""
  24. return str(base64.b64encode(str(text).encode('utf-8')), 'utf-8')
  25. def b64_to_text(text):
  26. """b64解码"""
  27. return str(base64.b64decode(str(text).encode('utf-8')), 'utf-8')
  28. def byte_to_b64(byte):
  29. """b64加密"""
  30. return base64.b64encode(byte)
  31. def byte_to_md5(byte):
  32. """计算md5"""
  33. m = hashlib.md5()
  34. m.update(byte)
  35. return m.hexdigest()