From ec2c2bc44ea7586dcff6e895ea71bbc651bb4df8 Mon Sep 17 00:00:00 2001 From: Luca Bigliardi <shammash@google.com> Date: Wed, 26 Aug 2009 11:34:14 +0100 Subject: [PATCH] GenerateSecret: specify the length of the secret Add a parameter to specify the length of the bytes sequence represented by the returned hex string. Defaulting to 20 so it's compatible with previous SHA1 hash. Signed-off-by: Luca Bigliardi <shammash@google.com> Reviewed-by: Michael Hanselmann <hansmi@google.com> --- lib/utils.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/utils.py b/lib/utils.py index d5ca93d55..1489ed579 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -1157,17 +1157,19 @@ def NewUUID(): f.close() -def GenerateSecret(): +def GenerateSecret(numbytes=20): """Generates a random secret. - This will generate a pseudo-random secret, and return its sha digest + This will generate a pseudo-random secret returning an hex string (so that it can be used where an ASCII string is needed). + @param numbytes: the number of bytes which will be represented by the returned + string (defaulting to 20, the length of a SHA1 hash) @rtype: str - @return: a sha1 hexdigest of a block of 64 random bytes + @return: an hex representation of the pseudo-random sequence """ - return sha1(os.urandom(64)).hexdigest() + return os.urandom(numbytes).encode('hex') def EnsureDirs(dirs): -- GitLab