MCPcopy Create free account
hub / github.com/authlib/authlib / encrypt

Method encrypt

authlib/jose/rfc7518/jwe_encs.py:47–67  ·  view source on GitHub ↗

Key Encryption with AES_CBC_HMAC_SHA2. :param msg: text to be encrypt in bytes :param aad: additional authenticated data in bytes :param iv: initialization vector in bytes :param key: encrypted key in bytes :return: (ciphertext, iv, tag)

(self, msg, aad, iv, key)

Source from the content-addressed store, hash-verified

45 return d[: self.key_len]
46
47 def encrypt(self, msg, aad, iv, key):
48 """Key Encryption with AES_CBC_HMAC_SHA2.
49
50 :param msg: text to be encrypt in bytes
51 :param aad: additional authenticated data in bytes
52 :param iv: initialization vector in bytes
53 :param key: encrypted key in bytes
54 :return: (ciphertext, iv, tag)
55 """
56 self.check_iv(iv)
57 hkey = key[: self.key_len]
58 ekey = key[self.key_len :]
59
60 pad = PKCS7(AES.block_size).padder()
61 padded_data = pad.update(msg) + pad.finalize()
62
63 cipher = Cipher(AES(ekey), CBC(iv), backend=default_backend())
64 enc = cipher.encryptor()
65 ciphertext = enc.update(padded_data) + enc.finalize()
66 tag = self._hmac(ciphertext, aad, iv, hkey)
67 return ciphertext, tag
68
69 def decrypt(self, ciphertext, aad, iv, tag, key):
70 """Key Decryption with AES AES_CBC_HMAC_SHA2.

Callers

nothing calls this directly

Calls 2

_hmacMethod · 0.95
check_ivMethod · 0.80

Tested by

no test coverage detected