-
Notifications
You must be signed in to change notification settings - Fork 0
/
encutil.py
46 lines (36 loc) · 1.3 KB
/
encutil.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
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
Kind of based on the encryption bits detailed in
http://djangosnippets.org/snippets/1095/
"""
from hashlib import sha256
from django.conf import settings
from django.utils.encoding import smart_str
from binascii import hexlify, unhexlify
import string
# Get best AES implementation we can.
try:
from Crypto.Cipher import AES
except ImportError:
from twofactor import pyaes as AES
# Get best `random` implementation we can.
import random
try:
random = random.SystemRandom()
except:
pass
def _gen_salt(length=16):
return ''.join([random.choice(string.letters+string.digits) for i in range(length)])
def _get_key(salt):
""" Combines `settings.SECRET_KEY` with a salt. """
if not salt: salt = ""
return sha256("%s%s" % (settings.SECRET_KEY, salt)).digest()
def encrypt(data, salt):
cipher = AES.new(_get_key(salt), mode=AES.MODE_ECB)
value = smart_str(data)
padding = cipher.block_size - len(value) % cipher.block_size
if padding and padding < cipher.block_size:
value += "\0" + ''.join([random.choice(string.printable) for index in range(padding-1)])
return hexlify(cipher.encrypt(value))
def decrypt(encrypted_data, salt):
cipher = AES.new(_get_key(salt), mode=AES.MODE_ECB)
return cipher.decrypt(unhexlify(smart_str(encrypted_data))).split('\0')[0]