Skip to content

Commit 6828283

Browse files
author
Roland Hedberg
committed
Usage of a cryptographically suitable RNG. Proposed by Seth Arnold.
1 parent 7b025c6 commit 6828283

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def run_tests(self):
5151

5252
setup(
5353
name='pysaml2',
54-
version='2.3.0',
55-
description='Python implementation of SAML Version 2 to be used in a WSGI environment',
54+
version='2.4.0beta',
55+
description='Python implementation of SAML Version 2',
5656
# long_description = read("README"),
5757
author='Roland Hedberg',
5858
author_email='[email protected]',

src/saml2/s_utils.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import base64
88
import sys
99
import hmac
10+
import string
1011

1112
# from python 2.5
1213
import imp
@@ -154,31 +155,28 @@ def deflate_and_base64_encode(string_val):
154155
return base64.b64encode(zlib.compress(string_val)[2:-4])
155156

156157

157-
def rndstr(size=16):
158+
def rndstr(size=16, alphabet=""):
158159
"""
159160
Returns a string of random ascii characters or digits
160161
161162
:param size: The length of the string
162163
:return: string
163164
"""
164-
_basech = string.ascii_letters + string.digits
165-
return "".join([random.choice(_basech) for _ in range(size)])
165+
rng = random.SystemRandom()
166+
if not alphabet:
167+
alphabet = string.letters[0:52] + string.digits
168+
return str().join(rng.choice(alphabet) for _ in range(size))
166169

167170

168-
def sid(seed=""):
169-
"""The hash of the server time + seed makes an unique SID for each session.
170-
128-bits long so it fulfills the SAML2 requirements which states
171+
def sid():
172+
"""creates an unique SID for each session.
173+
160-bits long so it fulfills the SAML2 requirements which states
171174
128-160 bits
172175
173-
:param seed: A seed string
174-
:return: The hex version of the digest, prefixed by 'id-' to make it
176+
:return: A random string prefix with 'id-' to make it
175177
compliant with the NCName specification
176178
"""
177-
ident = md5()
178-
ident.update(repr(time.time()))
179-
if seed:
180-
ident.update(seed)
181-
return "id-" + ident.hexdigest()
179+
return "id-" + rndstr(17)
182180

183181

184182
def parse_attribute_map(filenames):

src/saml2/sigver.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from saml2 import ExtensionElement
3434
from saml2 import VERSION
3535

36-
from saml2.s_utils import sid
36+
from saml2.s_utils import sid, rndstr
3737
from saml2.s_utils import Unsupported
3838

3939
from saml2.time_util import instant
@@ -322,18 +322,13 @@ def signed_instance_factory(instance, seccont, elements_to_sign=None):
322322

323323

324324
# --------------------------------------------------------------------------
325-
326-
327-
def create_id():
328-
""" Create a string of 40 random characters from the set [a-p],
329-
can be used as a unique identifier of objects.
330-
331-
:return: The string of random characters
332-
"""
333-
ret = ""
334-
for _ in range(40):
335-
ret += chr(random.randint(0, 15) + ord('a'))
336-
return ret
325+
# def create_id():
326+
# """ Create a string of 40 random characters from the set [a-p],
327+
# can be used as a unique identifier of objects.
328+
#
329+
# :return: The string of random characters
330+
# """
331+
# return rndstr(40, "abcdefghijklmonp")
337332

338333

339334
def make_temp(string, suffix="", decode=True, delete=True):

0 commit comments

Comments
 (0)