mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2024-12-21 05:53:12 +00:00
added own aes wrapper
This commit is contained in:
parent
b9567ad25e
commit
405f396f79
1
src/allmydata/crypto/__init__.py
Normal file
1
src/allmydata/crypto/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
|
33
src/allmydata/crypto/aes.py
Normal file
33
src/allmydata/crypto/aes.py
Normal file
@ -0,0 +1,33 @@
|
||||
import os
|
||||
import six
|
||||
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||
|
||||
class AES:
|
||||
|
||||
__DEFAULT_IV = '\x00' * 16
|
||||
|
||||
def __init__(self, key, iv=None):
|
||||
# validate the key
|
||||
if not isinstance(key, six.binary_type):
|
||||
raise TypeError('Key was not bytes')
|
||||
if len(key) not in (16, 32):
|
||||
raise ValueError('Key was not 16 or 32 bytes long')
|
||||
|
||||
# validate the IV
|
||||
if iv is None:
|
||||
iv = self.__DEFAULT_IV
|
||||
if not isinstance(iv, six.binary_type):
|
||||
raise TypeError('IV was not bytes')
|
||||
if len(iv) != 16:
|
||||
raise ValueError('IV was not 16 bytes long')
|
||||
|
||||
self._cipher = Cipher(algorithms.AES(key), modes.CTR(iv), backend=default_backend())
|
||||
self._encryptor = self._cipher.encryptor()
|
||||
|
||||
def process(self, plaintext):
|
||||
if not isinstance(plaintext, six.binary_type):
|
||||
raise TypeError('Plaintext was not bytes')
|
||||
|
||||
return self._encryptor.update(plaintext)
|
Loading…
Reference in New Issue
Block a user