def dumpPrivKey(address): ''' retrieve private key from database for given address option to decrypt BIP0038 encrypted keys display as base58 and WIF ''' conn = db.open() c = conn.cursor() #get the needed data from the database c.execute('select p.id,p.privK,v.version,v.prefix,v.length,c.longName from eskimo_privK as p inner join eskimo_master as m on p.id = m.privK inner join eskimo_addresses as a on a.id = m.address inner join eskimo_currencies as c on p.currency = c.id inner join eskimo_versions as v on c.version = v.id where a.address=?;', (address.encode('base64', 'strict'),)) privData = c.fetchone() if privData is None: print(address + ' was not found') return False #check if the private key is bip encoded and get the password reminder if it is c.execute('select reminder, p from eskimo_bip where privK=?;', (privData[0],)) bip = c.fetchone() if bip is None: isBip = False else: isBip = True reminder = bip[0] p = bip[1] privK = privData[1].decode('base64', 'strict') #ask if the user wants to decrypt a bip encrypted key if isBip: print('The private key found is BIP0038 encrypted.') decrypt = raw_input('Would you like to decrypt it? (n) ').lower().strip() if decrypt == 'y': bipPass1 = 'pass1' bipPass2 = 'pass2' while bipPass1 != bipPass2 or len(bipPass1) < 1: bipPass1 = inp.secure_passphrase('Enter your BIP0038 passphrase ' + ('(' + bip[0] + ')' if bip[0] != '' else '')) bipPass2 = inp.secure_passphrase('Re-enter your passphrase to confirm') if bipPass1 != bipPass2: print('The passphrases entered did not match.') elif len(bipPass1) < 1: print('No passphrase was entered') #decrypt the private key using the supplied password privK, addresshash = bip38.decrypt(privK, bipPass1, p) #decode the privK from base 256 privK = enc.decode(privK, 256) #hash the address to check the decryption address = publicKey2Address(privateKey2PublicKey(privK), privData[2], privData[3], privData[4]) if hashlib.sha256(hashlib.sha256(address).digest()).digest()[0:4] != addresshash: print('\nUnable to decrypt.') print('Please try again with a different passphrase.') return False else: print('\n' + privData[5] + ' Address = ' + str(address)) print('\nBIP0038 encrypted private key : ' + privK) return True print('\n' + privData[5] + ' Address = ' + str(address)) print('\nPrivate key : ') print('HEX : ' + enc.encode(privK, 16)) print('WIF : ' + privateKey2Wif(privK, privData[2], privData[3], privData[4])) return True
def getPass(self, confirm=False): self.password = inp.secure_passphrase('Enter your database password') if confirm is True: pass2 = inp.secure_passphrase('Enter it again to confirm') if self.password != pass2: print('The passwords did not match') self.getPass(True) return return self.password
def generate(cur, bip=False): """ public and private key generator. optional BIP0038 encryption """ #check that the given currency is in the system conn = db.open() c = conn.cursor() #pull the version details from the database c.execute('select v.version,v.prefix,v.length,c.id,c.longName,c.version from inuit_versions as v inner join inuit_currencies as c on c.version = v.id where c.currency=?;', (cur.upper(),)) version = c.fetchone() if version is None: print(cur.upper() + ' is not currently listed as a currency') return False #randomly choose a prefix if multiples exist prefixes = version[1].split('|') prefix = prefixes[random.randint(0, (len(prefixes)-1))] #generate the private and public keys privateKey = rand.randomKey(inp.keyboardEntropy()) privK256 = enc.encode(privateKey, 256, 32) publicAddress = publicKey2Address(privateKey2PublicKey(privateKey), version[0], prefix, version[2]) #optional BIP0038 encryption get.flushKeybuffer(get._Getch()) encrypt = 'y' if encrypt == 'y': bipPass1 = 'pass1' bipPass2 = 'pass2' while bipPass1 != bipPass2 or len(bipPass1) < 1: bipPass1 = inp.secure_passphrase('Enter your BIP0038 passphrase') bipPass2 = inp.secure_passphrase('Re-enter your passphrase to confirm') if bipPass1 != bipPass2: print('The passphrases entered did not match.') elif len(bipPass1) < 1: print('No passphrase was entered!') reminder = raw_input('Enter an optional reminder for your password : '******'') #print('Enter the number of rounds of encryption.') #p = raw_input('A smaller number means quicker but less secure. (8) : ').strip() #p = 8 if p == '' else int(p) p = 8 privK = bip38.encrypt(privK256, publicAddress, bipPass1, p) isBip = True else: privK = privateKey isBip = False #save details to the database c.execute('insert into inuit_privK (privK, currency) values (?,?);', (str(privK).encode('base64','strict'), version[3])) privKid = c.lastrowid c.execute('insert into inuit_addresses (address, currency) values (?,?);', (publicAddress.encode('base64','strict'), version[3])) addId = c.lastrowid c.execute('insert into inuit_master (address, privK, version) values (?,?,?);', (addId, privKid, version[5])) if isBip is True: c.execute('insert into inuit_bip (privK, reminder, p) values (?,?,?);', (privKid, reminder, p)) db.close(conn) print('') print(version[4] + ' Address : ' + publicAddress) return privK, publicAddress
def encrypt(passW): ''' encrypt the sqlite database ''' #double check the password checkPass = inp.secure_passphrase('Please enter your database password') if checkPass != passW.password: print('Your passwords don\'t match') passW.getPass(True) print('Encrypting database. Please wait...') bs = 128 inFile = 'igloo.dat' outFile = 'iceblock' if not os.path.isfile(inFile) and os.path.isfile(outFile): print('Database is already encrypted') return salt = str(rand.clockrnd())[:(bs - len('Salted__'))] in_file = open('igloo.dat', 'rb') out_file = open('iceblock', 'wb') key, iv = derive_key_and_iv(passW.password, salt, 32, bs) out_file.write('Salted__' + salt) finished = False while not finished: chunk = in_file.read(1024 * bs) if len(chunk) == 0 or len(chunk) % bs != 0: padding_length = (bs - len(chunk) % bs) or bs chunk += padding_length * chr(padding_length) finished = True out_file.write(aes.encryptData(key, chunk)) in_file.close() out_file.close() os.remove(inFile) return
def editPass(self): currentpass = inp.secure_passphrase('Enter your current password') if currentpass != self.password: print('Incorrect password entered') return self.getPass(True) print('password changed') return
def dumpPrivKey(address): """ retrieve private key from database for given address option to decrypt BIP0038 encrypted keys display as base58 and WIF """ conn = db.open() c = conn.cursor() # get the needed data from the database c.execute( "select p.id,p.privK,v.version,v.prefix,v.length,c.longName from inuit_privK as p inner join inuit_master as m on p.id = m.privK inner join inuit_addresses as a on a.id = m.address inner join inuit_currencies as c on p.currency = c.id inner join inuit_versions as v on c.version = v.id where a.address=?;", (address.encode("base64", "strict"),), ) privData = c.fetchone() if privData is None: print(address + " was not found") return False # check if the private key is bip encoded and get the password reminder if it is c.execute("select reminder, p from inuit_bip where privK=?;", (privData[0],)) bip = c.fetchone() if bip is None: isBip = False else: isBip = True reminder = bip[0] p = bip[1] privK = privData[1].decode("base64", "strict") # ask if the user wants to decrypt a bip encrypted key if isBip: print("The private key found is BIP0038 encrypted.") decrypt = raw_input("Would you like to decrypt it? (n) ").lower().strip() if decrypt == "y": bipPass1 = "pass1" bipPass2 = "pass2" while bipPass1 != bipPass2 or len(bipPass1) < 1: bipPass1 = inp.secure_passphrase( "Enter your BIP0038 passphrase " + ("(" + bip[0] + ")" if bip[0] != "" else "") ) bipPass2 = inp.secure_passphrase("Re-enter your passphrase to confirm") if bipPass1 != bipPass2: print("The passphrases entered did not match.") elif len(bipPass1) < 1: print("No passphrase was entered") # decrypt the private key using the supplied password privK, addresshash = bip38.decrypt(privK, bipPass1, p) # decode the privK from base 256 privK = enc.decode(privK, 256) # hash the address to check the decryption address = publicKey2Address(privateKey2PublicKey(privK), privData[2], privData[3], privData[4]) if hashlib.sha256(hashlib.sha256(address).digest()).digest()[0:4] != addresshash: print("\nUnable to decrypt.") print("Please try again with a different passphrase.") return False else: print("\n" + privData[5] + " Address = " + str(address)) print("\nBIP0038 encrypted private key : " + privK) return True print("\n" + privData[5] + " Address = " + str(address)) print("\nPrivate key : ") print("HEX : " + enc.encode(privK, 16)) print("WIF : " + privateKey2Wif(privK, privData[2], privData[3], privData[4])) return True