MCPcopy Index your code
hub / github.com/authlib/authlib / encrypt

Method encrypt

authlib/jose/rfc7518/jwe_encs.py:49–69  ·  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

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