diff options
Diffstat (limited to 'utils.py')
-rw-r--r-- | utils.py | 14 |
1 files changed, 6 insertions, 8 deletions
@@ -6,7 +6,7 @@ import re from Crypto.Cipher import AES from email.mime.text import MIMEText from functools import wraps -from flask import flash, g, redirect, render_template, request, session, url_for +from flask import current_app, flash, g, redirect, render_template, request, session, url_for from hashlib import sha1 from random import randint from werkzeug.exceptions import Forbidden @@ -68,13 +68,13 @@ def encrypt_password(password): Encrypt the given password with `config.PASSWORD_ENCRYPTION_KEY`. The key must be 32 bytes long. """ - assert len(app.config['PASSWORD_ENCRYPTION_KEY']) == 32 + assert len(current_app.config['PASSWORD_ENCRYPTION_KEY']) == 32 if isinstance(password, unicode): password = password.encode('utf8') iv = ''.join(chr(randint(0, 0xff)) for i in range(16)) - encryptor = AES.new(app.config['PASSWORD_ENCRYPTION_KEY'], AES.MODE_CBC, iv) + encryptor = AES.new(current_app.config['PASSWORD_ENCRYPTION_KEY'], AES.MODE_CBC, iv) return iv + encryptor.encrypt(pad(password)) def decrypt_password(ciphertext): @@ -82,7 +82,7 @@ def decrypt_password(ciphertext): Decrypt the given password with `config.PASSWORD_ENCRYPTION_KEY`. """ iv = ciphertext[:16] - encryptor = AES.new(app.config['PASSWORD_ENCRYPTION_KEY'], AES.MODE_CBC, iv) + encryptor = AES.new(current_app.config['PASSWORD_ENCRYPTION_KEY'], AES.MODE_CBC, iv) return encryptor.decrypt(ciphertext[16:]).rstrip('\0') @@ -93,7 +93,7 @@ def create_confirmation(realm, data): Expects as input a realm to distinguish data for several applications and some data (pickle-able). """ - key = '\0'.join((app.config['SECRET_KEY'], realm)) + key = '\0'.join((current_app.config['SECRET_KEY'], realm)) payload = pickle.dumps(data) mac = hmac.new(key, payload, sha1) return ''.join((mac.digest(), payload)).encode('base64').strip() @@ -106,7 +106,7 @@ def verify_confirmation(realm, token): Verify a confirmation created by `create_confirmation` and, if it is valid, return the original data. """ - key = '\0'.join((app.config['SECRET_KEY'], realm)) + key = '\0'.join((current_app.config['SECRET_KEY'], realm)) token = token.decode('base64') mac = token[:20] @@ -137,5 +137,3 @@ def send_mail(recipient, subject, body, sender=None): if p.wait() != 0: raise RuntimeError('sendmail terminated with %d' % p.returncode) -# circular import -from app import app |