Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

90 lignes
2.2 KiB

  1. from ..utils import base64url_encode, ensure_binary
  2. class Key:
  3. """
  4. A simple interface for implementing JWK keys.
  5. """
  6. def __init__(self, key, algorithm):
  7. pass
  8. def sign(self, msg):
  9. raise NotImplementedError()
  10. def verify(self, msg, sig):
  11. raise NotImplementedError()
  12. def public_key(self):
  13. raise NotImplementedError()
  14. def to_pem(self):
  15. raise NotImplementedError()
  16. def to_dict(self):
  17. raise NotImplementedError()
  18. def encrypt(self, plain_text, aad=None):
  19. """
  20. Encrypt the plain text and generate an auth tag if appropriate
  21. Args:
  22. plain_text (bytes): Data to encrypt
  23. aad (bytes, optional): Authenticated Additional Data if key's algorithm supports auth mode
  24. Returns:
  25. (bytes, bytes, bytes): IV, cipher text, and auth tag
  26. """
  27. raise NotImplementedError()
  28. def decrypt(self, cipher_text, iv=None, aad=None, tag=None):
  29. """
  30. Decrypt the cipher text and validate the auth tag if present
  31. Args:
  32. cipher_text (bytes): Cipher text to decrypt
  33. iv (bytes): IV if block mode
  34. aad (bytes): Additional Authenticated Data to verify if auth mode
  35. tag (bytes): Authentication tag if auth mode
  36. Returns:
  37. bytes: Decrypted value
  38. """
  39. raise NotImplementedError()
  40. def wrap_key(self, key_data):
  41. """
  42. Wrap the the plain text key data
  43. Args:
  44. key_data (bytes): Key data to wrap
  45. Returns:
  46. bytes: Wrapped key
  47. """
  48. raise NotImplementedError()
  49. def unwrap_key(self, wrapped_key):
  50. """
  51. Unwrap the the wrapped key data
  52. Args:
  53. wrapped_key (bytes): Wrapped key data to unwrap
  54. Returns:
  55. bytes: Unwrapped key
  56. """
  57. raise NotImplementedError()
  58. class DIRKey(Key):
  59. def __init__(self, key_data, algorithm):
  60. self._key = ensure_binary(key_data)
  61. self._alg = algorithm
  62. def to_dict(self):
  63. return {
  64. "alg": self._alg,
  65. "kty": "oct",
  66. "k": base64url_encode(self._key),
  67. }