/ python-oneliners / TOTP / pyhmac.py
pyhmac.py
1  def hmac(key: bytes, msg: bytes, hasher: callable) -> bytes:
2      if len(key) > 64:
3          key = hasher(key)
4      key += bytes(64 - len(key))
5      opad = bytes(0x5c ^ i for i in range(256))
6      ipad = bytes(0x36 ^ i for i in range(256))
7      return hasher(key.translate(opad) + hasher(key.translate(ipad) + msg))