Browse Source

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

MMGen 6 years ago
parent
commit
360ef8d3c5
2 changed files with 11 additions and 2 deletions
  1. 9 2
      mmgen/crypto.py
  2. 2 0
      mmgen/globalvars.py

+ 9 - 2
mmgen/crypto.py

@@ -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 '

+ 2 - 0
mmgen/globalvars.py

@@ -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)