Trait bip38::Decrypt [−][src]
pub trait Decrypt {
fn decrypt(&self, pass: &str) -> Result<([u8; 32], bool), Error>;
fn decrypt_to_wif(&self, pass: &str) -> Result<String, Error>;
}Expand description
Allow decryption of bitcoin encrypted private keys in srt format.
Required methods
Decrypt an encrypted bitcoin private key in strformat (both non-ec and ec).
This function targets strings of 58 base58 characters with the version prefix 6P and
returns a tuple containing the decrypted private key ([u8; 32]) and a boolean indication
of if this private key is intended to result in a compressed public key or not. So, if the
flag is true, create an compressed public key (33 bytes), in case of false, use the
full 65 bytes of the public key.
Examples
use bip38::Decrypt; // decryption of non elliptic curve multiplication assert_eq!( "6PYMgbeR6XCsX4yJx8E52vW4PJDoTiu1QeFLn81KoW6Shye5DZ4ZnDauno".decrypt("weakPass"), Ok(([0x11; 32], true)) // indication to compress the public key of this private key ); assert_eq!( "6PRVo8whL3QbdrXpKk3gP2dGuxDbuvMsMqUq2imVigrm8oyRbvBoRUsbB3".decrypt("weakPass"), Ok(([0x11; 32], false)) // indication do not compress the public key ); // decryption of elliptic curve multiplication assert!( "6PnPQGcDuPhCMmXzTebiryx8zHxr8PZvUJccSxarn9nLHVLX7yVj6Wcoj9".decrypt("weakPass").is_ok() ); assert!( "6PfVV4eYCodt6tRiHbHH356MX818xZvcN54oNd1rCr8Cbme3273xWAgBhx".decrypt("notWeak?").is_ok() );
Errors
-
Error::Passis returned if an invalid passphrase is inserted. -
Error::EncKeyis returned if the targetstris not an valid encrypted private key. -
Error::Checksumis returned if the targetstrhas valid encrypted private key format but invalid checksum. -
Error::Base58is returned if an nonbase58character is found.
use bip38::{Decrypt, Error}; assert!( "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByq".decrypt("Satoshi").is_ok() ); assert_eq!( "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByq".decrypt("Nakamoto"), Err(Error::Pass) ); assert_eq!( "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByQ".decrypt("Satoshi"), // ← Q Err(Error::Checksum) ); assert_eq!( "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWBy".decrypt("Satoshi"), // ← q? Err(Error::EncKey) ); assert_eq!( "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWBy!".decrypt("Satoshi"), // ← ! Err(Error::Base58) );
Passphrase
This function handle the normalization (nfc) of the passphrase as specified on
bip-0038.
use bip38::Decrypt; assert!( "6PRW5o9FLp4gJDDVqJQKJFTpMvdsSGJxMYHtHaQBF3ooa8mwD69bapcDQn" .decrypt("\u{03d2}\u{0301}\u{0000}\u{010400}\u{01f4a9}").is_ok() );
Decrypt an encrypted bitcoin private key in strformat (both non-ec and ec), resulting on
a wif private key.
This function targets strings of 58 base58 characters with the version prefix 6P and
returns a String of base58 characters representing the decrypted private key in the wif
format.
Examples
use bip38::Decrypt; // decryption of non elliptic curve multiplication assert_eq!( "6PYMgbeR6XCsX4yJx8E52vW4PJDoTiu1QeFLn81KoW6Shye5DZ4ZnDauno".decrypt_to_wif("weakPass"), Ok(String::from("KwntMbt59tTsj8xqpqYqRRWufyjGunvhSyeMo3NTYpFYzZbXJ5Hp")) ); assert_eq!( "6PRVo8whL3QbdrXpKk3gP2dGuxDbuvMsMqUq2imVigrm8oyRbvBoRUsbB3".decrypt_to_wif("weakPass"), Ok(String::from("5HwoXVkHoRM8sL2KmNRS217n1g8mPPBomrY7yehCuXC1115WWsh")) ); // decryption of elliptic curve multiplication assert_eq!( "6PnPQGcDuPhCMmXzTebiryx8zHxr8PZvUJccSxarn9nLHVLX7yVj6Wcoj9".decrypt_to_wif("weakPass"), Ok(String::from("Kyo7rXp5Qygn5qUFS39wR9DsxfrznTUc8XJeaHVSWhkVnhC4NVWE")) ); assert_eq!( "6PfVV4eYCodt6tRiHbHH356MX818xZvcN54oNd1rCr8Cbme3273xWAgBhx".decrypt_to_wif("notWeak?"), Ok(String::from("5JPeaPgoWj6R9CEnSrvmGJh36e3cxRpGGpJ3mzTeK6M88Hc3Ttm")) );
Errors
-
Error::Passis returned if an invalid passphrase is inserted. -
Error::EncKeyis returned if the targetstris not an valid encrypted private key. -
Error::Checksumis returned if the targetstrhas valid encrypted private key format but invalid checksum. -
Error::Base58is returned if an nonbase58character is found.
use bip38::{Decrypt, Error}; assert_eq!( "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByq".decrypt_to_wif("Satoshi"), Ok(String::from("5HtasZ6ofTHP6HCwTqTkLDuLQisYPah7aUnSKfC7h4hMUVw2gi5")) ); assert_eq!( "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByq".decrypt_to_wif("Nakamoto"), Err(Error::Pass) ); assert_eq!( // Q "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWByQ".decrypt_to_wif("Satoshi"), Err(Error::Checksum) ); assert_eq!( // q? "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWBy".decrypt_to_wif("Satoshi"), Err(Error::EncKey) ); assert_eq!( // ! "6PRNFFkZc2NZ6dJqFfhRoFNMR9Lnyj7dYGrzdgXXVMXcxoKTePPX1dWBy!".decrypt_to_wif("Satoshi"), Err(Error::Base58) );
Passphrase
This function handle the normalization (nfc) of the passphrase as specified on
bip-0038.
use bip38::Decrypt; assert_eq!( "6PRW5o9FLp4gJDDVqJQKJFTpMvdsSGJxMYHtHaQBF3ooa8mwD69bapcDQn" .decrypt_to_wif("\u{03d2}\u{0301}\u{0000}\u{010400}\u{01f4a9}"), Ok(String::from("5Jajm8eQ22H3pGWLEVCXyvND8dQZhiQhoLJNKjYXk9roUFTMSZ4")) );