MCPcopy Create free account
hub / github.com/ActiveState/code / encrypt

Method encrypt

recipes/Python/576980_Authenticated/recipe-576980.py:36–45  ·  view source on GitHub ↗

encrypt data with AES-CBC and sign it with HMAC-SHA256

(self, data)

Source from the content-addressed store, hash-verified

34 return key[:-cls.SIG_SIZE], key[-cls.SIG_SIZE:]
35
36 def encrypt(self, data):
37 """encrypt data with AES-CBC and sign it with HMAC-SHA256"""
38 aes_key, hmac_key = self.keys
39 pad = self.AES_BLOCK_SIZE - len(data) % self.AES_BLOCK_SIZE
40 data = data + pad * chr(pad)
41 iv_bytes = os.urandom(self.AES_BLOCK_SIZE)
42 cypher = AES.new(aes_key, AES.MODE_CBC, iv_bytes)
43 data = iv_bytes + cypher.encrypt(data)
44 sig = hmac.new(hmac_key, data, hashlib.sha256).digest()
45 return data + sig
46
47 def decrypt(self, data):
48 """verify HMAC-SHA256 signature and decrypt data with AES-CBC"""

Callers 1

dumpsMethod · 0.95

Calls 2

digestMethod · 0.80
newMethod · 0.45

Tested by

no test coverage detected