Skip to content

Commit acd6bfc

Browse files
committed
Pruned helper_random and removed import from pyelliptic
1 parent e40c5a3 commit acd6bfc

File tree

2 files changed

+20
-71
lines changed

2 files changed

+20
-71
lines changed

src/helper_random.py

Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,14 @@
1-
"""Convenience functions for random operations. Not suitable for security / cryptography operations."""
1+
"""Keep all imports from random in one place"""
22

3-
import os
4-
import random
53

6-
try:
7-
from pyelliptic.openssl import OpenSSL
8-
except ImportError:
9-
from .pyelliptic.openssl import OpenSSL
4+
from random import seed
5+
from random import shuffle as randomshuffle
6+
from random import sample as randomsample
7+
from random import randrange as randomrandrange
8+
from random import choice as randomchoice
9+
from highlevelcrypto import randomBytes
1010

11-
NoneType = type(None)
12-
13-
14-
def seed():
15-
"""Initialize random number generator"""
16-
random.seed()
17-
18-
19-
def randomBytes(n):
20-
"""Method randomBytes."""
21-
try:
22-
return os.urandom(n)
23-
except NotImplementedError:
24-
return OpenSSL.rand(n)
25-
26-
27-
def randomshuffle(population):
28-
"""Method randomShuffle.
29-
30-
shuffle the sequence x in place.
31-
shuffles the elements in list in place,
32-
so they are in a random order.
33-
As Shuffle will alter data in-place,
34-
so its input must be a mutable sequence.
35-
In contrast, sample produces a new list
36-
and its input can be much more varied
37-
(tuple, string, xrange, bytearray, set, etc)
38-
"""
39-
random.shuffle(population)
40-
41-
42-
def randomsample(population, k):
43-
"""Method randomSample.
44-
45-
return a k length list of unique elements
46-
chosen from the population sequence.
47-
Used for random sampling
48-
without replacement, its called
49-
partial shuffle.
50-
"""
51-
return random.sample(population, k)
52-
53-
54-
def randomrandrange(x, y=None):
55-
"""Method randomRandrange.
56-
57-
return a randomly selected element from
58-
range(start, stop). This is equivalent to
59-
choice(range(start, stop)),
60-
but doesnt actually build a range object.
61-
"""
62-
if isinstance(y, NoneType):
63-
return random.randrange(x) # nosec
64-
return random.randrange(x, y) # nosec
65-
66-
67-
def randomchoice(population):
68-
"""Method randomchoice.
69-
70-
Return a random element from the non-empty
71-
sequence seq. If seq is empty, raises
72-
IndexError.
73-
"""
74-
return random.choice(population) # nosec
11+
__all__ = [
12+
'seed', 'randomBytes', 'randomshuffle', 'randomsample', 'randomrandrange',
13+
'randomchoice'
14+
]

src/highlevelcrypto.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77
`More discussion. <https://github.com/yann2192/pyelliptic/issues/32>`_
88
"""
99

10+
import os
1011
from binascii import hexlify
1112

1213
import pyelliptic
1314
from pyelliptic import OpenSSL
1415
from pyelliptic import arithmetic as a
1516

1617

18+
def randomBytes(n):
19+
"""Get n random bytes"""
20+
try:
21+
return os.urandom(n)
22+
except NotImplementedError:
23+
return OpenSSL.rand(n)
24+
25+
1726
def makeCryptor(privkey):
1827
"""Return a private `.pyelliptic.ECC` instance"""
1928
private_key = a.changebase(privkey, 16, 256, minlen=32)

0 commit comments

Comments
 (0)