Skip to content

Commit b3a8be9

Browse files
committed
added encrypt decrypt + test and dependency
1 parent 568e2e0 commit b3a8be9

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
requests~=2.22.0
1+
requests~=2.22.0
2+
pycrypto~=2.6.1

securenative/sn_crypto.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from Crypto.Cipher import AES
2+
from Crypto import Random
3+
from binascii import unhexlify, hexlify
4+
5+
BLOCK_SIZE = 16
6+
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
7+
8+
9+
def encrypt(text, cipherKey):
10+
iv = Random.new().read(AES.block_size);
11+
cipher = AES.new(cipherKey, AES.MODE_CBC, iv)
12+
raw = pad(text)
13+
return hexlify(iv + cipher.encrypt(raw)).decode('utf-8').strip()
14+
15+
16+
def decrypt(encrypted, cipherKey):
17+
content = unhexlify(encrypted)
18+
iv = content[:BLOCK_SIZE]
19+
cipherText = content[BLOCK_SIZE:]
20+
aes = AES.new(cipherKey, AES.MODE_CBC, iv)
21+
return aes.decrypt(cipherText).decode('utf-8').strip()

securenative/sn_crypto_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
3+
from securenative.sn_crypto import encrypt,decrypt
4+
5+
6+
class CryptoTests(unittest.TestCase):
7+
def test_encrypt_decrypt(self):
8+
api_key = '6EA4915349C0AAC6F6572DA4F6B00C42'
9+
data = '{"cid":"198a41ff-a10f-4cda-a2f3-a9ca80c0703b","fp":"6d8cabd95987f8318b1fe01593d5c2a5.24700f9f1986800ab4fcc880530dd0ed"}'
10+
self.assertEqual(data,decrypt(encrypt(data,api_key),api_key))
11+
12+

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
long_description_content_type="text/markdown",
2121
install_requires=[
2222
"requests",
23+
"pycrypro",
2324
],
2425
classifiers=[
2526
'Development Status :: 5 - Production/Stable',

0 commit comments

Comments
 (0)