HMAC-SHA256 with AES-256 in CBC mode
问题 I recently came across the following code sample for encrypting a file with AES-256 CBC with a SHA-256 HMAC for authentication and validation: aes_key, hmac_key = self.keys # create a PKCS#7 pad to get us to `len(data) % 16 == 0` pad_length = 16 - len(data) % 16 data = data + (pad_length * chr(pad_length)) # get IV iv = os.urandom(16) # create cipher cipher = AES.new(aes_key, AES.MODE_CBC, iv) data = iv + cipher.encrypt(data) sig = hmac.new(hmac_key, data, hashlib.sha256).digest() # return