Random key generator.

fixes #396
This commit is contained in:
Antoine Nguyen
2013-05-11 15:43:59 +02:00
parent fd2db74797
commit 3539bc07e2
2 changed files with 20 additions and 2 deletions
+2 -1
View File
@@ -3,6 +3,7 @@ from django.utils.translation import ugettext as _, ugettext_lazy
from modoboa.lib.formutils import YesNoField, SeparatorField, InlineRadioSelect
from modoboa.lib.sysutils import exec_cmd
from modoboa.lib import parameters
from modoboa.auth.lib import random_key
def enabled_applications():
@@ -51,7 +52,7 @@ class GeneralParametersForm(parameters.AdminParametersForm):
secret_key = forms.CharField(
label=ugettext_lazy("Secret key"),
initial="abcdefghijklmnop",
initial=random_key(),
help_text=ugettext_lazy("Key used to encrypt/decrypt passwords")
)
+18 -1
View File
@@ -1,8 +1,21 @@
# coding: utf-8
from Crypto.Cipher import AES
import base64
import random
import string
from modoboa.lib import parameters
def random_key(l=16):
"""Generate a random key
:param integer l: the key's length
:return: a string
"""
char_set = string.printable
return ''.join(random.sample(char_set * l, l))
def encrypt(clear):
key = parameters.get_admin("SECRET_KEY", app="admin")
obj = AES.new(key, AES.MODE_ECB)
@@ -14,11 +27,15 @@ def encrypt(clear):
ciph = base64.b64encode(ciph)
return ciph
def decrypt(ciph):
obj = AES.new(parameters.get_admin("SECRET_KEY", app="admin"), AES.MODE_ECB)
obj = AES.new(
parameters.get_admin("SECRET_KEY", app="admin"), AES.MODE_ECB
)
ciph = base64.b64decode(ciph)
clear = obj.decrypt(ciph)
return clear.rstrip(' ')
def get_password(request):
return decrypt(request.session["password"])