2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PythonでHMACをBase64にする

Posted at

目的

PythonでメッセージをHMACで暗号化して、それをBase64形式にする方法です。
Rest通信でHMACをBase64にする必要があったので試した。

サンプル

今回はHMACsha256形式で暗号化しました。
暗号化形式を変えたい場合は、hmac.newの引数digestmod=hashlib.sha256を変更します。


import hashlib
import hmac

SECRET_KEY = "abcdefghijklnmopqrstuvwxyz0123456789" # 任意のシークレットキー
message = "Hello World!!!!!"  # 任意のメッセージ

hmac_message = hmac.new(key=bytes(SECRET_KEY, 'UTF-8'), msg=message.encode('utf-8'), digestmod=hashlib.sha256).digest()
hmac_message_base64 = base64.b64encode(hmac_message).decode()
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?