Java Code Examples for net.bither.bitherj.utils.Utils#wipeBytes()

The following examples show how to use net.bither.bitherj.utils.Utils#wipeBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HDAccountCold.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public boolean checkWithPassword(CharSequence password) {
    try {
        decryptHDSeed(password);
        decryptMnemonicSeed(password);
        byte[] hdCopy = Arrays.copyOf(hdSeed, hdSeed.length);
        boolean hdSeedSafe = Utils.compareString(getFirstAddressFromDb(),
                getFirstAddressFromSeed(null));
        boolean mnemonicSeedSafe = Arrays.equals(seedFromMnemonic(mnemonicSeed), hdCopy);
        Utils.wipeBytes(hdCopy);
        wipeHDSeed();
        wipeMnemonicSeed();
        return hdSeedSafe && mnemonicSeedSafe;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 2
Source File: EnterpriseHDMSeed.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public boolean checkWithPassword(CharSequence password) {
    try {
        decryptHDSeed(password);
        decryptMnemonicSeed(password);
        byte[] hdCopy = Arrays.copyOf(hdSeed, hdSeed.length);
        boolean hdSeedSafe = Utils.compareString(getFirstAddressFromDb(),
                getFirstAddressFromSeed(null));
        boolean mnemonicSeedSafe = Arrays.equals(seedFromMnemonic(mnemonicSeed), hdCopy);
        Utils.wipeBytes(hdCopy);
        wipeHDSeed();
        wipeMnemonicSeed();
        return hdSeedSafe && mnemonicSeedSafe;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 3
Source File: HDMKeychain.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public boolean checkWithPassword(CharSequence password) {
    if (isInRecovery()) {
        return true;
    }
    try {
        decryptHDSeed(password);
        decryptMnemonicSeed(password);
        byte[] hdCopy = Arrays.copyOf(hdSeed, hdSeed.length);
        boolean hdSeedSafe = Utils.compareString(getFirstAddressFromDb(),
                getFirstAddressFromSeed(null));
        boolean mnemonicSeedSafe = Arrays.equals(seedFromMnemonic(mnemonicSeed), hdCopy);
        Utils.wipeBytes(hdCopy);
        wipeHDSeed();
        wipeMnemonicSeed();
        return hdSeedSafe && mnemonicSeedSafe;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 4
Source File: HDAccount.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public boolean checkWithPassword(CharSequence password) {
    if (!hasPrivKey()) {
        return true;
    }
    try {
        decryptHDSeed(password);
        decryptMnemonicSeed(password);
        byte[] hdCopy = Arrays.copyOf(hdSeed, hdSeed.length);
        boolean hdSeedSafe = Utils.compareString(getFirstAddressFromDb(),
                getFirstAddressFromSeed(null));
        boolean mnemonicSeedSafe = Arrays.equals(seedFromMnemonic(mnemonicSeed), hdCopy);
        Utils.wipeBytes(hdCopy);
        wipeHDSeed();
        wipeMnemonicSeed();
        return hdSeedSafe && mnemonicSeedSafe;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 5
Source File: DesktopHDMKeychain.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public boolean checkWithPassword(CharSequence password) {
    try {
        decryptHDSeed(password);
        decryptMnemonicSeed(password);
        byte[] hdCopy = Arrays.copyOf(hdSeed, hdSeed.length);
        boolean hdSeedSafe = Utils.compareString(getFirstAddressFromDb(),
                getFirstAddressFromSeed(null));
        boolean mnemonicSeedSafe = Arrays.equals(seedFromMnemonic(mnemonicSeed), hdCopy);
        Utils.wipeBytes(hdCopy);
        wipeHDSeed();
        wipeMnemonicSeed();
        return hdSeedSafe && mnemonicSeedSafe;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 6
Source File: DumpedPrivateKey.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private static byte[] encode(byte[] keyBytes, boolean compressed) {
    Preconditions.checkArgument(keyBytes.length == 32, "Private keys must be 32 bytes");
    if (!compressed) {
        return keyBytes;
    } else {
        // Keys that have compressed public components have an extra 1 byte on the end in dumped form.
        byte[] bytes = new byte[33];
        System.arraycopy(keyBytes, 0, bytes, 0, 32);
        bytes[32] = 1;
        Utils.wipeBytes(keyBytes);
        return bytes;
    }
}
 
Example 7
Source File: HDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public PasswordSeed createPasswordSeed(CharSequence password) {
    if (isInRecovery()) {
        throw new AssertionError("HDM in recovery can not create passwordSeed");
    }
    String encrypted = AbstractDb.addressProvider.getEncryptMnemonicSeed(hdSeedId);
    byte[] priv = new EncryptedData(encrypted).decrypt(password);
    ECKey k = new ECKey(priv, null);
    String address = k.toAddress();
    Utils.wipeBytes(priv);
    k.clearPrivateKey();
    return new PasswordSeed(address, encrypted);
}
 
Example 8
Source File: HDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public boolean checkSingularBackupWithPassword(CharSequence password) {
    if (isInRecovery()) {
        return true;
    }
    if (getAllCompletedAddresses().size() == 0) {
        return true;
    }
    String backup = AbstractDb.addressProvider.getSingularModeBackup(getHdSeedId());
    if (backup == null) {
        return true;
    }
    EncryptedData encrypted = new EncryptedData(backup);
    byte[] mnemonic = encrypted.decrypt(password);
    boolean result;
    try {
        byte[] seed = seedFromMnemonic(mnemonic);
        byte[] pub = getAllCompletedAddresses().get(0).getPubCold();
        DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(seed);
        DeterministicKey purpose = master.deriveHardened(44);
        DeterministicKey coinType = purpose.deriveHardened(0);
        DeterministicKey account = coinType.deriveHardened(0);
        DeterministicKey external = account.deriveSoftened(0);
        DeterministicKey first = external.deriveSoftened(0);
        master.wipe();
        purpose.wipe();
        coinType.wipe();
        account.wipe();
        external.wipe();
        Utils.wipeBytes(seed);
        result = Arrays.equals(first.getPubKey(), pub);
        first.wipe();
    } catch (MnemonicException.MnemonicLengthException e) {
        e.printStackTrace();
        result = false;
    }
    Utils.wipeBytes(mnemonic);
    return result;
}
 
Example 9
Source File: HDMSingular.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private void setEntropyInterval(byte[] entropy, boolean xrandom) {
    hotMnemonicSeed = Arrays.copyOf(entropy, 32);
    coldMnemonicSeed = Arrays.copyOfRange(entropy, 32, 64);
    Utils.wipeBytes(entropy);
    initHotFirst();
    encryptedColdMnemonicSeed = new EncryptedData(coldMnemonicSeed, password, xrandom);
    coldQr = QRCodeUtil.HDM_QR_CODE_FLAG + PrivateKeyUtil.getFullencryptHDMKeyChain(xrandom, encryptedColdMnemonicSeed.toEncryptedString());
}
 
Example 10
Source File: HDMHotAdd.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public void wipe() {
    if (passwordGetter != null) {
        passwordGetter.wipe();
    }
    if (coldRoot != null) {
        Utils.wipeBytes(coldRoot);
    }
}
 
Example 11
Source File: KeyCrypterScrypt.java    From bitherj with Apache License 2.0 5 votes vote down vote up
/**
 * Decrypt bytes previously encrypted with this class.
 *
 * @param privateKeyToDecode The private key to decrypt
 * @param aesKey             The AES key to use for decryption
 * @return The decrypted bytes
 * @throws KeyCrypterException if bytes could not be decoded to a valid key
 */
@Override
public byte[] decrypt(EncryptedPrivateKey privateKeyToDecode, KeyParameter aesKey) throws KeyCrypterException {
    checkNotNull(privateKeyToDecode);
    checkNotNull(aesKey);

    try {
        ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), privateKeyToDecode.getInitialisationVector());

        // Decrypt the message.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(false, keyWithIv);

        byte[] cipherBytes = privateKeyToDecode.getEncryptedBytes();
        int minimumSize = cipher.getOutputSize(cipherBytes.length);
        byte[] outputBuffer = new byte[minimumSize];
        int length1 = cipher.processBytes(cipherBytes, 0, cipherBytes.length, outputBuffer, 0);
        int length2 = cipher.doFinal(outputBuffer, length1);
        int actualLength = length1 + length2;

        byte[] decryptedBytes = new byte[actualLength];
        System.arraycopy(outputBuffer, 0, decryptedBytes, 0, actualLength);

        Utils.wipeBytes(outputBuffer);

        return decryptedBytes;
    } catch (Exception e) {
        throw new KeyCrypterException("Could not decrypt bytes", e);
    }
}
 
Example 12
Source File: AbstractHD.java    From bitherj with Apache License 2.0 4 votes vote down vote up
protected void wipeHDSeed() {
    if (hdSeed == null) {
        return;
    }
    Utils.wipeBytes(hdSeed);
}
 
Example 13
Source File: AbstractHD.java    From bitherj with Apache License 2.0 4 votes vote down vote up
protected void wipeMnemonicSeed() {
    if (mnemonicSeed == null) {
        return;
    }
    Utils.wipeBytes(mnemonicSeed);
}
 
Example 14
Source File: HDMSingular.java    From bitherj with Apache License 2.0 4 votes vote down vote up
private void wipeCold() {
    Utils.wipeBytes(coldMnemonicSeed);
}
 
Example 15
Source File: HDAccount.java    From bitherj with Apache License 2.0 4 votes vote down vote up
protected void wipeHDSeed() {
    if (hdSeed == null) {
        return;
    }
    Utils.wipeBytes(hdSeed);
}
 
Example 16
Source File: HDAccount.java    From bitherj with Apache License 2.0 4 votes vote down vote up
protected void wipeMnemonicSeed() {
    if (mnemonicSeed == null) {
        return;
    }
    Utils.wipeBytes(mnemonicSeed);
}
 
Example 17
Source File: DeterministicKey.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public void wipe() {
    clearPrivateKey();
    clearChainCode();
    Utils.wipeBytes(pub);
}
 
Example 18
Source File: DeterministicKey.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public void clearChainCode() {
    Utils.wipeBytes(chainCode);
}
 
Example 19
Source File: DumpedPrivateKey.java    From bitherj with Apache License 2.0 4 votes vote down vote up
public void clearPrivateKey() {
    Utils.wipeBytes(bytes);
}