-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashlab.py
33 lines (24 loc) · 851 Bytes
/
hashlab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# class HashLaber(type):
import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
class AESCipher(object):
def __init__(self, key):
self.bs = AES.block_size
self.key = key.encode()
def encrypt(self, raw):
raw = self._pad(raw)
#iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_ECB)
return base64.b64encode( cipher.encrypt(raw.encode()))
def decrypt(self, enc):
enc = base64.b64decode(enc)
#iv = enc[:AES.block_size]
cipher = AES.new(self.key, AES.MODE_ECB)
return self._unpad(cipher.decrypt(enc)).decode('utf-8')
def _pad(self, s):
return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)
@staticmethod
def _unpad(s):
return s[:-ord(s[len(s)-1:])]