Skip to content

Commit c7a3bfa

Browse files
g1itchLee Miller
authored andcommitted
Move randomBytes to highlevelcrypto
1 parent feaee60 commit c7a3bfa

4 files changed

Lines changed: 20 additions & 21 deletions

File tree

src/helper_ackPayload.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,26 @@ def genAckPayload(streamNumber=1, stealthLevel=0):
2222
- level 1: a getpubkey request for a (random) dummy key hash
2323
- level 2: a standard message, encrypted to a random pubkey
2424
"""
25-
if stealthLevel == 2: # Generate privacy-enhanced payload
25+
if stealthLevel == 2: # Generate privacy-enhanced payload
2626
# Generate a dummy privkey and derive the pubkey
2727
dummyPubKeyHex = highlevelcrypto.privToPub(
28-
hexlify(helper_random.randomBytes(32)))
28+
hexlify(highlevelcrypto.randomBytes(32)))
2929
# Generate a dummy message of random length
3030
# (the smallest possible standard-formatted message is 234 bytes)
31-
dummyMessage = helper_random.randomBytes(
31+
dummyMessage = highlevelcrypto.randomBytes(
3232
helper_random.randomrandrange(234, 801))
3333
# Encrypt the message using standard BM encryption (ECIES)
3434
ackdata = highlevelcrypto.encrypt(dummyMessage, dummyPubKeyHex)
3535
acktype = 2 # message
3636
version = 1
3737

38-
elif stealthLevel == 1: # Basic privacy payload (random getpubkey)
39-
ackdata = helper_random.randomBytes(32)
38+
elif stealthLevel == 1: # Basic privacy payload (random getpubkey)
39+
ackdata = highlevelcrypto.randomBytes(32)
4040
acktype = 0 # getpubkey
4141
version = 4
4242

4343
else: # Minimum viable payload (non stealth)
44-
ackdata = helper_random.randomBytes(32)
44+
ackdata = highlevelcrypto.randomBytes(32)
4545
acktype = 2 # message
4646
version = 1
4747

src/helper_random.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
"""Convenience functions for random operations. Not suitable for security / cryptography operations."""
22

3-
import os
43
import random
54

6-
try:
7-
from pyelliptic.openssl import OpenSSL
8-
except ImportError:
9-
from .pyelliptic.openssl import OpenSSL
105

116
NoneType = type(None)
127

@@ -16,14 +11,6 @@ def seed():
1611
random.seed()
1712

1813

19-
def randomBytes(n):
20-
"""Method randomBytes."""
21-
try:
22-
return os.urandom(n)
23-
except NotImplementedError:
24-
return OpenSSL.rand(n)
25-
26-
2714
def randomshuffle(population):
2815
"""Method randomShuffle.
2916

src/highlevelcrypto.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99

1010
import hashlib
11+
import os
1112
from binascii import hexlify
1213

1314
import pyelliptic
@@ -17,7 +18,8 @@
1718

1819
__all__ = [
1920
'decodeWalletImportFormat', 'encodeWalletImportFormat',
20-
'encrypt', 'makeCryptor', 'pointMult', 'privToPub', 'sign', 'verify']
21+
'encrypt', 'makeCryptor', 'pointMult', 'privToPub', 'randomBytes',
22+
'sign', 'verify']
2123

2224

2325
# WIF (uses arithmetic ):
@@ -49,6 +51,16 @@ def encodeWalletImportFormat(privKey):
4951
return a.changebase(privKey + checksum, 256, 58)
5052

5153

54+
# Random
55+
56+
def randomBytes(n):
57+
"""Get n random bytes"""
58+
try:
59+
return os.urandom(n)
60+
except NotImplementedError:
61+
return OpenSSL.rand(n)
62+
63+
5264
def makeCryptor(privkey, curve='secp256k1'):
5365
"""Return a private `.pyelliptic.ECC` instance"""
5466
private_key = a.changebase(privkey, 16, 256, minlen=32)

src/network/tcp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import protocol
1717
import state
1818
from bmconfigparser import config
19-
from helper_random import randomBytes
19+
from highlevelcrypto import randomBytes
2020
from inventory import Inventory
2121
from queues import invQueue, receiveDataQueue, UISignalQueue
2222
from tr import _translate

0 commit comments

Comments
 (0)