Skip to content

Instantly share code, notes, and snippets.

@yasudacloud
Created July 8, 2025 14:19
Show Gist options
  • Select an option

  • Save yasudacloud/fd9e2792fc3f596581e53258cc519e7a to your computer and use it in GitHub Desktop.

Select an option

Save yasudacloud/fd9e2792fc3f596581e53258cc519e7a to your computer and use it in GitHub Desktop.
from cryptography.hazmat.primitives import cmac
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.ciphers import algorithms
# Sesamiの公開鍵
public_bytes = payload[3:67]
# private_bytesはアプリ側の秘密鍵
private_key_num = int.from_bytes(private_bytes, byteorder='big')
curve = ec.SECP256R1()
private_key = ec.derive_private_key(private_key_num, curve)
if len(public_bytes) == 64:
public_bytes = b'\x04' + public_bytes
public_key = ec.EllipticCurvePublicKey.from_encoded_point(curve, public_bytes)
secret_key = private_key.exchange(ec.ECDH(), public_key)
# 永続化を推奨
secret = secret_key[0:16]
c = cmac.CMAC(algorithms.AES(secret))
# 初期化で受け取った4バイトの値
c.update(random_code)
# トークンは永続化しない
token = c.finalize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment