-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fx
- Loading branch information
Zaytsev Dmitriy
authored and
qq
committed
Sep 2, 2016
1 parent
2281f68
commit 6d7631a
Showing
5 changed files
with
135 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import base64 | ||
import os | ||
import ConfigParser | ||
|
||
from Crypto.Cipher import AES | ||
from Crypto import Random | ||
|
||
|
||
class AESCipher(object): | ||
|
||
def __init__(self): | ||
self.dir_path = os.path.dirname(os.path.abspath(__file__)) | ||
self.key = self.generate_key() | ||
self.file = None | ||
self.get_current_path() | ||
|
||
@staticmethod | ||
def pad(s): | ||
return s + (16 - len(s) % 16) * chr(16 - len(s) % 16) | ||
|
||
@staticmethod | ||
def unpad(s): | ||
return s[:-ord(s[len(s) - 1:])] | ||
|
||
def get_current_path(self): | ||
self.file = os.path.join(self.dir_path, 'email_settings.ini.enc') | ||
|
||
def generate_key(self): | ||
key_path = os.path.join(self.dir_path, 'pyprind.key') | ||
if not os.path.exists(key_path): | ||
with open(key_path, 'wb') as key_file: | ||
key_file.write(os.urandom(16)) | ||
with open(key_path, 'rb') as f: | ||
key = f.read() | ||
return key | ||
|
||
def encrypt(self, text): | ||
text = self.pad(text) | ||
iv = Random.new().read(AES.block_size) | ||
cipher = AES.new(self.key, AES.MODE_CBC, iv) | ||
encrypted_mes = base64.b64encode(iv + cipher.encrypt(text)) | ||
with open(self.file, 'wb') as f: | ||
f.write(encrypted_mes) | ||
|
||
def decrypt(self): | ||
with open(self.file, 'rb') as f: | ||
enc = base64.b64decode(f.read()) | ||
iv = enc[:16] | ||
cipher = AES.new(self.key, AES.MODE_CBC, iv) | ||
return self.unpad(cipher.decrypt(enc[16:])) | ||
|
||
|
||
def setup_email(smtp_server, smtp_port, username, password): | ||
dir_path = os.path.dirname(os.path.abspath(__file__)) | ||
file_path = os.path.join(dir_path, 'email_settings.ini.enc') | ||
cipher = AESCipher() | ||
config = ConfigParser.ConfigParser() | ||
config.add_section('Email') | ||
config.set('Email', 'smtp_server', smtp_server) | ||
config.set('Email', 'smtp_port', smtp_port) | ||
config.set('Email', 'username', username) | ||
config.set('Email', 'password', password) | ||
with open(file_path, 'wb') as f: | ||
config.write(f) | ||
with open(file_path, 'rb') as af: | ||
cipher.encrypt(af.read()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
psutil>=3.2.0 | ||
psutil>=3.2.0 | ||
pycryptodome==3.4 |