1. import uhashlib
    2. import ubinascii
    3. hash_obj = uhashlib.sha256() # 创建hash对象
    4. hash_obj.update(b"QuecPython")
    5. res = hash_obj.digest()
    6. # b"\x1e\xc6gq\xb3\xa9\xac>\xa4\xc4O\x00\x9eTW\x97\xd4.\x9e}Bo\xff\x82u\x89Th\xfe'\xc6\xcd"
    7. # 转成十六进制表示
    8. hex_msg = ubinascii.hexlify(res)
    9. # b'1ec66771b3a9ac3ea4c44f009e545797d42e9e7d426fff8275895468fe27c6cd'
    1. import uhashlib
    2. import ubinascii
    3. # 目前支持 md5,sha1, sha256
    4. data = b"QuecPython" # 待加密数据
    5. hash_obj = uhashlib.md5()
    6. hash_obj.update(data)
    7. res = hash_obj.digest()
    8. hex_msg = ubinascii.hexlify(res)
    9. print("md5加密后的数据:", hex_msg)
    10. # b'37b8419ee7cdb3c64d7e66019216117c'
    11. hash_obj = uhashlib.sha1()
    12. hash_obj.update(data)
    13. res = hash_obj.digest()
    14. hex_msg = ubinascii.hexlify(res)
    15. print("sha1加密后的数据:", hex_msg)
    16. # b'614a4247ef68e9f9793e11353cc86acb932badab'
    17. hash_obj = uhashlib.sha256()
    18. hash_obj.update(data)
    19. res = hash_obj.digest()
    20. hex_msg = ubinascii.hexlify(res)
    21. print("sha256加密后的数据:", hex_msg)
    22. # b'1ec66771b3a9ac3ea4c44f009e545797d42e9e7d426fff8275895468fe27c6cd'
    23. # print("原始数据:", b'\x11\x22\x33123')
    24. # res = ubinascii.b2a_base64('\x11\x22\x33123')
    25. # print("编码base64数据:", res)
    26. # res = ubinascii.a2b_base64(res)
    27. # print("还原base64数据:", res)