use hashlib.scrypt() if available, in preference to scrypt.hash()

This commit is contained in:
The MMGen Project 2019-03-17 15:26:23 +00:00
commit 360ef8d3c5
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
2 changed files with 11 additions and 2 deletions

View file

@ -106,12 +106,19 @@ def decrypt_data(enc_data,key,iv=1,desc='data'):
return c.decrypt(enc_data)
def scrypt_hash_passphrase(passwd,salt,hash_preset,buflen=32):
import scrypt
# Buflen arg is for brainwallets only, which use this function to generate
# the seed directly.
N,r,p = get_hash_params(hash_preset)
if type(passwd) == str: passwd = passwd.encode()
return scrypt.hash(passwd,salt,2**N,r,p,buflen=buflen)
try:
assert not g.use_standalone_scrypt_module
from hashlib import scrypt # Python >= v3.6
return scrypt(passwd,salt=salt,n=2**N,r=r,p=p,maxmem=0,dklen=buflen)
except:
import scrypt
return scrypt.hash(passwd,salt,2**N,r,p,buflen=buflen)
def make_key(passwd,salt,hash_preset,desc='encryption key',from_what='passphrase',verbose=False):
if from_what: desc += ' from '

View file

@ -167,6 +167,7 @@ class g(object):
'MMGEN_TESTNET',
'MMGEN_REGTEST',
'MMGEN_TRACEBACK',
'MMGEN_USE_STANDALONE_SCRYPT_MODULE',
'MMGEN_DISABLE_COLOR',
)
@ -195,6 +196,7 @@ class g(object):
key_generators = 'python-ecdsa','secp256k1' # '1','2'
key_generator = 2 # secp256k1 is default
use_standalone_scrypt_module = False
hash_presets = {
# Scrypt params:
# ID N p r (N is an exponent of two)