From 1e715113ab28b13592dd05939cb463e926c26416 Mon Sep 17 00:00:00 2001 From: Janus Date: Wed, 18 Jul 2018 14:34:59 +0200 Subject: [PATCH] remove pbkdf2 dependency, use stdlib instead --- contrib/requirements/requirements.txt | 1 - electrum/keystore.py | 7 +++---- electrum/mnemonic.py | 3 +-- electrum/plugins/digitalbitbox/digitalbitbox.py | 4 ++-- electrum/storage.py | 6 +++--- run_electrum | 1 - 6 files changed, 9 insertions(+), 13 deletions(-) diff --git a/contrib/requirements/requirements.txt b/contrib/requirements/requirements.txt index 26b1b3d3ae68..4fd62530afb6 100644 --- a/contrib/requirements/requirements.txt +++ b/contrib/requirements/requirements.txt @@ -1,6 +1,5 @@ pyaes>=0.1a1 ecdsa>=0.9 -pbkdf2 requests qrcode protobuf diff --git a/electrum/keystore.py b/electrum/keystore.py index a8e068bbf2a6..c822be13eb0a 100644 --- a/electrum/keystore.py +++ b/electrum/keystore.py @@ -552,13 +552,12 @@ def bip39_normalize_passphrase(passphrase): return normalize('NFKD', passphrase or '') def bip39_to_seed(mnemonic, passphrase): - import pbkdf2, hashlib, hmac + import hashlib, hmac PBKDF2_ROUNDS = 2048 mnemonic = normalize('NFKD', ' '.join(mnemonic.split())) passphrase = bip39_normalize_passphrase(passphrase) - return pbkdf2.PBKDF2(mnemonic, 'mnemonic' + passphrase, - iterations = PBKDF2_ROUNDS, macmodule = hmac, - digestmodule = hashlib.sha512).read(64) + return hashlib.pbkdf2_hmac('sha512', mnemonic.encode('utf-8'), + b'mnemonic' + passphrase.encode('utf-8'), iterations = PBKDF2_ROUNDS) # returns tuple (is_checksum_valid, is_wordlist_valid) def bip39_is_checksum_valid(mnemonic): diff --git a/electrum/mnemonic.py b/electrum/mnemonic.py index 17c2cafca5d9..a22913ae3559 100644 --- a/electrum/mnemonic.py +++ b/electrum/mnemonic.py @@ -30,7 +30,6 @@ import string import ecdsa -import pbkdf2 from .util import print_error from .bitcoin import is_old_seed, is_new_seed @@ -131,7 +130,7 @@ def mnemonic_to_seed(self, mnemonic, passphrase): PBKDF2_ROUNDS = 2048 mnemonic = normalize_text(mnemonic) passphrase = normalize_text(passphrase) - return pbkdf2.PBKDF2(mnemonic, 'electrum' + passphrase, iterations = PBKDF2_ROUNDS, macmodule = hmac, digestmodule = hashlib.sha512).read(64) + return hashlib.pbkdf2_hmac('sha512', mnemonic.encode('utf-8'), b'electrum' + passphrase.encode('utf-8'), iterations = PBKDF2_ROUNDS) def mnemonic_encode(self, i): n = len(self.wordlist) diff --git a/electrum/plugins/digitalbitbox/digitalbitbox.py b/electrum/plugins/digitalbitbox/digitalbitbox.py index ed8c05f8d520..37db2644ae5f 100644 --- a/electrum/plugins/digitalbitbox/digitalbitbox.py +++ b/electrum/plugins/digitalbitbox/digitalbitbox.py @@ -120,8 +120,8 @@ def dbb_has_password(self): def stretch_key(self, key): - import pbkdf2, hmac - return to_hexstr(pbkdf2.PBKDF2(key, b'Digital Bitbox', iterations = 20480, macmodule = hmac, digestmodule = hashlib.sha512).read(64)) + import hmac + return to_hexstr(hashlib.pbkdf2_hmac('sha512', key.encode('utf-8'), b'Digital Bitbox', iterations = 20480)) def backup_password_dialog(self): diff --git a/electrum/storage.py b/electrum/storage.py index 70fb19a55665..0524cd23f5ae 100644 --- a/electrum/storage.py +++ b/electrum/storage.py @@ -29,7 +29,7 @@ import copy import re import stat -import pbkdf2, hmac, hashlib +import hmac, hashlib import base64 import zlib from collections import defaultdict @@ -165,7 +165,7 @@ def file_exists(self): @staticmethod def get_eckey_from_password(password): - secret = pbkdf2.PBKDF2(password, '', iterations=1024, macmodule=hmac, digestmodule=hashlib.sha512).read(64) + secret = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'), b'', iterations=1024) ec_key = ecc.ECPrivkey.from_arbitrary_size_secret(secret) return ec_key @@ -637,7 +637,7 @@ def raise_unsupported_version(self, seed_version): # version 1.9.8 created v6 wallets when an incorrect seed was entered in the restore dialog msg += '\n\nThis file was created because of a bug in version 1.9.8.' if self.get('master_public_keys') is None and self.get('master_private_keys') is None and self.get('imported_keys') is None: - # pbkdf2 was not included with the binaries, and wallet creation aborted. + # pbkdf2 (at that time an additional dependency) was not included with the binaries, and wallet creation aborted. msg += "\nIt does not contain any keys, and can safely be removed." else: # creation was complete if electrum was run from source diff --git a/run_electrum b/run_electrum index b5d5af00dbd1..96b055e01186 100755 --- a/run_electrum +++ b/run_electrum @@ -46,7 +46,6 @@ def check_imports(): import ecdsa import requests import qrcode - import pbkdf2 import google.protobuf import jsonrpclib except ImportError as e: