Encrypts plaintext and returns a JWE compact serialization string. Args: plaintext (bytes): A bytes object to encrypt key (str or dict): The key(s) to use for encrypting the content. Can be individual JWK or JWK set. encryption (str, optional): The content en
(plaintext, key, encryption=ALGORITHMS.A256GCM, algorithm=ALGORITHMS.DIR, zip=None, cty=None, kid=None)
| 12 | |
| 13 | |
| 14 | def encrypt(plaintext, key, encryption=ALGORITHMS.A256GCM, algorithm=ALGORITHMS.DIR, zip=None, cty=None, kid=None): |
| 15 | """Encrypts plaintext and returns a JWE compact serialization string. |
| 16 | |
| 17 | Args: |
| 18 | plaintext (bytes): A bytes object to encrypt |
| 19 | key (str or dict): The key(s) to use for encrypting the content. Can be |
| 20 | individual JWK or JWK set. |
| 21 | encryption (str, optional): The content encryption algorithm used to |
| 22 | perform authenticated encryption on the plaintext to produce the |
| 23 | ciphertext and the Authentication Tag. Defaults to A256GCM. |
| 24 | algorithm (str, optional): The cryptographic algorithm used |
| 25 | to encrypt or determine the value of the CEK. Defaults to dir. |
| 26 | zip (str, optional): The compression algorithm) applied to the |
| 27 | plaintext before encryption. Defaults to None. |
| 28 | cty (str, optional): The media type for the secured content. |
| 29 | See http://www.iana.org/assignments/media-types/media-types.xhtml |
| 30 | kid (str, optional): Key ID for the provided key |
| 31 | |
| 32 | Returns: |
| 33 | bytes: The string representation of the header, encrypted key, |
| 34 | initialization vector, ciphertext, and authentication tag. |
| 35 | |
| 36 | Raises: |
| 37 | JWEError: If there is an error signing the token. |
| 38 | |
| 39 | Examples: |
| 40 | >>> from jose import jwe |
| 41 | >>> jwe.encrypt('Hello, World!', 'asecret128bitkey', algorithm='dir', encryption='A128GCM') |
| 42 | 'eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4R0NNIn0..McILMB3dYsNJSuhcDzQshA.OfX9H_mcUpHDeRM4IA.CcnTWqaqxNsjT4eCaUABSg' |
| 43 | |
| 44 | """ |
| 45 | plaintext = ensure_binary(plaintext) # Make sure it's bytes |
| 46 | if algorithm not in ALGORITHMS.SUPPORTED: |
| 47 | raise JWEError("Algorithm %s not supported." % algorithm) |
| 48 | if encryption not in ALGORITHMS.SUPPORTED: |
| 49 | raise JWEError("Algorithm %s not supported." % encryption) |
| 50 | key = jwk.construct(key, algorithm) |
| 51 | encoded_header = _encoded_header(algorithm, encryption, zip, cty, kid) |
| 52 | |
| 53 | plaintext = _compress(zip, plaintext) |
| 54 | enc_cek, iv, cipher_text, auth_tag = _encrypt_and_auth(key, algorithm, encryption, zip, plaintext, encoded_header) |
| 55 | |
| 56 | jwe_string = _jwe_compact_serialize(encoded_header, enc_cek, iv, cipher_text, auth_tag) |
| 57 | return jwe_string |
| 58 | |
| 59 | |
| 60 | def decrypt(jwe_str, key): |
nothing calls this directly
no test coverage detected
searching dependent graphs…