From 8bacd0f657a931754d8c03e2de86c5e00ac5f791 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Sat, 30 Apr 2016 13:28:46 +0200 Subject: [PATCH] Make sure key has the required size. see #867 --- modoboa/lib/cryptutils.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/modoboa/lib/cryptutils.py b/modoboa/lib/cryptutils.py index 0c778cda8..74c1e2657 100644 --- a/modoboa/lib/cryptutils.py +++ b/modoboa/lib/cryptutils.py @@ -1,19 +1,26 @@ # coding: utf-8 -from Crypto.Cipher import AES +"""Crypto related utilities.""" + import base64 import random import string + +from Crypto.Cipher import AES + from modoboa.lib import parameters def random_key(l=16): - """Generate a random key + """Generate a random key. :param integer l: the key's length :return: a string """ - char_set = string.digits + string.letters + string.punctuation - return ''.join(random.sample(char_set * l, l)) + population = string.digits + string.letters + string.punctuation + while True: + key = "".join(random.sample(population * l, l)) + if len(key) == l: + return key def encrypt(clear):