Trait bip38::Encrypt[][src]

pub trait Encrypt {
    fn encrypt(&self, pass: &str, compress: bool) -> Result<String, Error>;
}
Expand description

Allow encryption of bitcoin private keys in [u8; 32] format.

Required methods

Encrypt a bitcoin private key in the format of [u8; 32] (without elliptic curve multiplication) into a String of 58 base58 characters.

When decrypting the boolean flag compress is just an indication, but here it influences on the resulting prefix of the encrypted private key and obviously on the indication when decrypting, but not in the private key itself.

Examples

use bip38::Encrypt;

assert_eq!(
    [0x11; 32].encrypt("weakPass", true).unwrap(),
    "6PYMgbeR6XCsX4yJx8E52vW4PJDoTiu1QeFLn81KoW6Shye5DZ4ZnDauno"
);
assert_eq!(
    [0x11; 32].encrypt("weakPass", false).unwrap(),
    "6PRVo8whL3QbdrXpKk3gP2dGuxDbuvMsMqUq2imVigrm8oyRbvBoRUsbB3"
);

Errors

The only case this function can fail by itself is if the provided private key could not result in a bitcoin address. All other errors are here for safety and are related to dependencies. This function don’t unwrap internally to let the decision to do or not to the developer using the crate.

use bip38::{Encrypt, Error};

assert_eq!([0x00; 32].encrypt("oh_no!", true), Err(Error::PrvKey));
assert_eq!([0xff; 32].encrypt("oh_no!", true), Err(Error::PrvKey));

Passphrase

This function handle the normalization (nfc) of the passphrase as specified on bip-0038.

use bip38::Encrypt;

assert_eq!(
    [
        0x64, 0xee, 0xab, 0x5f, 0x9b, 0xe2, 0xa0, 0x1a, 0x83, 0x65, 0xa5, 0x79, 0x51, 0x1e,
        0xb3, 0x37, 0x3c, 0x87, 0xc4, 0x0d, 0xa6, 0xd2, 0xa2, 0x5f, 0x05, 0xbd, 0xa6, 0x8f,
        0xe0, 0x77, 0xb6, 0x6e
    ].encrypt("\u{03d2}\u{0301}\u{0000}\u{010400}\u{01f4a9}", false).unwrap(),
    "6PRW5o9FLp4gJDDVqJQKJFTpMvdsSGJxMYHtHaQBF3ooa8mwD69bapcDQn"
);

Implementations on Foreign Types

Implementors