-
[Python] HMAC SHA256 sign 및 verifyPython 2023. 10. 28. 20:07728x90반응형
이번에는 HMAC SHA256을 진행해보려한다.
보통 HMAC은 Message Authentication Code를 해쉬화하는것으로 메세지 인증 코드이다.
나는 코드를 업로드하려고했으니 자세한 설명은 다른 문서를 참고하길바란다.
Signature 코드는 다음과같다.
from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives import hmac h = hmac.HMAC(mac_key, hashes.SHA256(), backend=default_backend()) h.update(bytes(message)) hmac_tag = h.finalize()비교적 간단하다.
하지만 본인이 sign한 결과가 맞는지 확인이 필요하고
남들도 그렇기에 verification의 코드도 필요했고
그 코드는 다음과같다.
from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives import hmac h = hmac.HMAC(mac_key, hashes.SHA256(), backend=default_backend()) h.update(bytes(message)) hmac_tag = h.finalize() if hmac_tag != hmac_buf: print("Failed for verifying the data") exit() else: print("Success for verifying the data")사실 그냥 해당 메세지를 똑같이 hmac해보고 이전에 했던 buffer와 비교를 진행할 뿐이다.
이는 다른 사람이 HMAC buffer를 봤을 때 확인하는데에 사용하는 코드라고 생각하면될듯?
나는 서버와 클라이언트간의 통신에서 쓰이기에 이렇게 구현해보았다.
이렇게 HMAC SHA256에대한 코드 완료.
728x90반응형'Python' 카테고리의 다른 글
[Python] 패스워드를 이용한 암호화/복호화 (0) 2024.03.28 Window Pyinstaller error '파일에 바이러스 또는 기타 사용자 동의없이 설치된 소프트웨어가 있기 때문에 작업이 완료되지 않았습니다' (0) 2024.01.05 [Python] AES CBC모드 암호화 및 복호화 (0) 2023.10.28 [Python] cryptography 라이브러리로 RSA encrypt와 sign하기 (0) 2023.10.25 [Python] cryptography 라이브러리로 RSA에 쓰일 .pem file 로드하기 (0) 2023.10.24