python3 hmac 모듈
전송된 키와 메시지는 모두
bytes
형식이고 str
형식은 먼저 bytes
로 인코딩해야 합니다.# coding:utf-8
import hmac
secret_key1 = b'This is my secret key'
message1 = b'Hello world'
hex_res1 = hmac.new(secret_key1, message1, digestmod="MD5").hexdigest()
print(hex_res1) # b8908a20bd70f465330b434e18441d3b
secret_key2 = b'This is my secret key'
message2 = b'Hello world'
hex_res2 = hmac.new(secret_key2, message2, digestmod="MD5").hexdigest()
print(hex_res2) # b8908a20bd70f465330b434e18441d3b
compare_res = hmac.compare_digest(hex_res1, hex_res2) #
print(compare_res) # True
secret_key3 = b'This is my secret key'
message3 = b'Hello world!'
hex_res3 = hmac.new(secret_key3, message3, digestmod="MD5").hexdigest()
print(hex_res3) # a314490e13ff3d1dfa9cd18db8c4c3e8
compare_res = hmac.compare_digest(hex_res1, hex_res3) #
print(compare_res) # False
hmac_hex = hmac.new(secret_key3, message3, digestmod='md5').hexdigest()
print(hmac_hex) # a314490e13ff3d1dfa9cd18db8c4c3e8
print(hmac.new(secret_key3, message3, 'md5').digest()) # b'\xa3\x14I\x0e\x13\xff=\x1d\xfa\x9c\xd1\x8d\xb8\xc4\xc3\xe8'
print(len(hmac.new(secret_key3, message3, 'md5').digest())) # 16
content = "hello world"
content_bytes = content.encode("utf-8")
content_bytes_upper = content_bytes.upper() # , bytes upper
print(content_bytes_upper.decode("utf-8")) # HELLO WORLD
전재 대상:https://www.cnblogs.com/lilyxiaoyy/p/10942922.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.