Skip to content

Commit

Permalink
Updated Cryptographic() provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lk-geimfari committed Sep 24, 2017
1 parent 987c8ca commit b367656
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions mimesis/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import random


Expand All @@ -21,3 +22,8 @@ def randints(self, amount=None, a=1, b=100):

return [self.randint(a, b)
for _ in range(amount)]

@staticmethod
def urandom(*args, **kwargs):
"""Return a bytes object containing random bytes."""
return os.urandom(*args, **kwargs)
6 changes: 3 additions & 3 deletions mimesis/providers/cryptographic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from binascii import hexlify
import hashlib
import uuid
import os

from mimesis.exceptions import UnsupportedAlgorithm
from mimesis.providers import BaseProvider
Expand Down Expand Up @@ -51,12 +50,13 @@ def bytes(self, entropy=None):
if entropy is None:
entropy = 32

return os.urandom(entropy)
return self.random.urandom(entropy)

def token(self, entropy=None):
"""Return a random text string, in hexadecimal.
:param entropy: Number of bytes.
:return: Token.
"""
return hexlify(self.bytes(entropy)).decode('ascii')
token = hexlify(self.bytes(entropy))
return token.decode('ascii')
10 changes: 10 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ def test_randints(random):
assert len(result_custom) == 25
# All elements in result_custom equals to 1.
assert result_custom[0] == 1 and result_custom[-1] == 1


@pytest.mark.parametrize(
'n', (8, 16, 32, 64),
)
def test_urandom(random, n):
result = random.urandom(n)

assert len(result) == n
assert isinstance(result, bytes)

0 comments on commit b367656

Please sign in to comment.