Add back crypt module
This commit is contained in:
parent
f17e31d801
commit
36b4fcab93
38
clicada/crypt.py
Normal file
38
clicada/crypt.py
Normal file
@ -0,0 +1,38 @@
|
||||
# standard imports
|
||||
import os
|
||||
import logging
|
||||
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Util import Counter
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Encrypt:
|
||||
|
||||
aesBlockSize = 1 << 7
|
||||
|
||||
def __init__(self, secret, db_dir):
|
||||
fp = os.path.join(db_dir, '.aes_ctr_iv')
|
||||
try:
|
||||
f = open(fp, 'rb')
|
||||
self.iv = f.read()
|
||||
except FileNotFoundError:
|
||||
logg.debug('generating new iv for aes-ctr')
|
||||
self.iv = os.urandom(8)
|
||||
f = open(fp, 'wb')
|
||||
f.write(self.iv)
|
||||
|
||||
f.close()
|
||||
|
||||
iv_num = int.from_bytes(self.iv, 'big')
|
||||
self.ctr = Counter.new(aesBlockSize, initial_value=iv_num)
|
||||
self.cipher = AES.new(secret, AES.MODE_CTR, counter=self.ctr)
|
||||
|
||||
|
||||
def encrypt(self, v):
|
||||
return self.cipher.encrypt(v)
|
||||
|
||||
|
||||
def decrypt(self, v):
|
||||
return self.cipher.decrypt(v)
|
Loading…
Reference in New Issue
Block a user