您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

70 行
2.0 KiB

  1. import hashlib
  2. import hmac
  3. import os
  4. from jose.backends.base import Key
  5. from jose.constants import ALGORITHMS
  6. from jose.exceptions import JWKError
  7. from jose.utils import base64url_decode, base64url_encode, is_pem_format, is_ssh_key
  8. def get_random_bytes(num_bytes):
  9. return bytes(os.urandom(num_bytes))
  10. class HMACKey(Key):
  11. """
  12. Performs signing and verification operations using HMAC
  13. and the specified hash function.
  14. """
  15. HASHES = {ALGORITHMS.HS256: hashlib.sha256, ALGORITHMS.HS384: hashlib.sha384, ALGORITHMS.HS512: hashlib.sha512}
  16. def __init__(self, key, algorithm):
  17. if algorithm not in ALGORITHMS.HMAC:
  18. raise JWKError("hash_alg: %s is not a valid hash algorithm" % algorithm)
  19. self._algorithm = algorithm
  20. self._hash_alg = self.HASHES.get(algorithm)
  21. if isinstance(key, dict):
  22. self.prepared_key = self._process_jwk(key)
  23. return
  24. if not isinstance(key, str) and not isinstance(key, bytes):
  25. raise JWKError("Expecting a string- or bytes-formatted key.")
  26. if isinstance(key, str):
  27. key = key.encode("utf-8")
  28. if is_pem_format(key) or is_ssh_key(key):
  29. raise JWKError(
  30. "The specified key is an asymmetric key or x509 certificate and"
  31. " should not be used as an HMAC secret."
  32. )
  33. self.prepared_key = key
  34. def _process_jwk(self, jwk_dict):
  35. if not jwk_dict.get("kty") == "oct":
  36. raise JWKError("Incorrect key type. Expected: 'oct', Received: %s" % jwk_dict.get("kty"))
  37. k = jwk_dict.get("k")
  38. k = k.encode("utf-8")
  39. k = bytes(k)
  40. k = base64url_decode(k)
  41. return k
  42. def sign(self, msg):
  43. return hmac.new(self.prepared_key, msg, self._hash_alg).digest()
  44. def verify(self, msg, sig):
  45. return hmac.compare_digest(sig, self.sign(msg))
  46. def to_dict(self):
  47. return {
  48. "alg": self._algorithm,
  49. "kty": "oct",
  50. "k": base64url_encode(self.prepared_key).decode("ASCII"),
  51. }