Java Code Examples for net.bither.bitherj.qrcode.QRCodeUtil#splitOfPasswordSeed()

The following examples show how to use net.bither.bitherj.qrcode.QRCodeUtil#splitOfPasswordSeed() . 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: PrivateKeyUtil.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public static HDAccountCold getHDAccountCold(String str, CharSequence password) {
    HDAccountCold hdAccountCold = null;
    String[] strs = QRCodeUtil.splitOfPasswordSeed(str);
    if (strs.length % 3 != 0) {
        log.error("Backup: PrivateKeyFromString format error");
        return null;
    }
    for (int i = 0;
         i < strs.length;
         i += 3) {

        if (strs[i].indexOf(QRCodeUtil.HD_QR_CODE_FLAG) == 0) {
            try {
                String encryptedString = strs[i].substring(1) + QRCodeUtil.QR_CODE_SPLIT + strs[i + 1]
                        + QRCodeUtil.QR_CODE_SPLIT + strs[i + 2];
                hdAccountCold = new HDAccountCold(new EncryptedData(encryptedString), password);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return hdAccountCold;
}
 
Example 2
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public static String formatEncryptPrivateKeyForDb(String encryptPrivateKey) {
    if (Utils.isEmpty(encryptPrivateKey)) {
        return encryptPrivateKey;
    }
    String[] strs = QRCodeUtil.splitOfPasswordSeed(encryptPrivateKey);
    byte[] temp = Utils.hexStringToByteArray(strs[2]);
    byte[] salt = new byte[KeyCrypterScrypt.SALT_LENGTH];
    if (temp.length == KeyCrypterScrypt.SALT_LENGTH + 1) {
        System.arraycopy(temp, 1, salt, 0, salt.length);
    } else {
        salt = temp;
    }
    strs[2] = Utils.bytesToHexString(salt);
    return Utils.joinString(strs, QRCodeUtil.QR_CODE_SPLIT);

}
 
Example 3
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static String changePassword(String str, CharSequence oldpassword, CharSequence newPassword) {
    String[] strs = QRCodeUtil.splitOfPasswordSeed(str);
    if (strs.length != 3) {
        log.error("change Password: PrivateKeyFromString format error");
        return null;
    }

    byte[] temp = Utils.hexStringToByteArray(strs[2]);
    if (temp.length != KeyCrypterScrypt.SALT_LENGTH + 1 && temp.length != KeyCrypterScrypt.SALT_LENGTH) {
        log.error("decryption:  salt lenth is {} not {}", temp.length, KeyCrypterScrypt.SALT_LENGTH + 1);
        return null;
    }
    byte[] salt = new byte[KeyCrypterScrypt.SALT_LENGTH];
    if (temp.length == KeyCrypterScrypt.SALT_LENGTH) {
        salt = temp;
    } else {
        System.arraycopy(temp, 1, salt, 0, salt.length);
    }
    KeyCrypterScrypt crypter = new KeyCrypterScrypt(salt);
    EncryptedPrivateKey epk = new EncryptedPrivateKey(Utils.hexStringToByteArray
            (strs[1]), Utils.hexStringToByteArray(strs[0]));

    byte[] decrypted = crypter.decrypt(epk, crypter.deriveKey(oldpassword));
    EncryptedPrivateKey encryptedPrivateKey = crypter.encrypt(decrypted, crypter.deriveKey(newPassword));
    byte[] newDecrypted = crypter.decrypt(encryptedPrivateKey, crypter.deriveKey(newPassword));
    if (!Arrays.equals(decrypted, newDecrypted)) {
        throw new KeyCrypterException("change Password, cannot be successfully decrypted after encryption so aborting wallet encryption.");
    }
    Utils.wipeBytes(decrypted);
    Utils.wipeBytes(newDecrypted);
    return Utils.bytesToHexString(encryptedPrivateKey.getEncryptedBytes())
            + QRCodeUtil.QR_CODE_SPLIT + Utils.bytesToHexString(encryptedPrivateKey.getInitialisationVector())
            + QRCodeUtil.QR_CODE_SPLIT + strs[2];

}
 
Example 4
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static List<Address> getECKeysFromBackupString(String str, CharSequence password) {
    String[] strs = QRCodeUtil.splitOfPasswordSeed(str);
    if (strs.length % 3 != 0) {
        log.error("Backup: PrivateKeyFromString format error");
        return null;
    }
    ArrayList<Address> list = new ArrayList<Address>();
    for (int i = 0;
         i < strs.length;
         i += 3) {
        if (strs[i].indexOf(QRCodeUtil.HDM_QR_CODE_FLAG) == 0) {
            continue;
        }
        if (strs[i].indexOf(QRCodeUtil.HD_QR_CODE_FLAG) == 0){
            continue;
        }
        String encryptedString = strs[i] + QRCodeUtil.QR_CODE_SPLIT + strs[i + 1]
                + QRCodeUtil.QR_CODE_SPLIT + strs[i + 2];
        ECKey key = getECKeyFromSingleString(encryptedString, password);

        if (key == null) {
            return null;
        } else {
            Address address = new Address(key.toAddress(), key.getPubKey(), encryptedString,
                    false, key.isFromXRandom());
            key.clearPrivateKey();
            list.add(address);
        }
    }
    return list;
}
 
Example 5
Source File: EncryptedData.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public EncryptedData(String str) {
    String[] strs = QRCodeUtil.splitOfPasswordSeed(str);
    if (strs.length != 3) {
        log.error("decryption: EncryptedData format error");
    }
    initialisationVector = Utils.hexStringToByteArray
            (strs[1]);
    encryptedData = Utils.hexStringToByteArray(strs[0]);
    byte[] saltQRCodes = Utils.hexStringToByteArray(strs[2]);
    saltForQRCode = new SaltForQRCode(saltQRCodes);
}
 
Example 6
Source File: DesktopHDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static boolean checkPassword(String keysString, CharSequence password) throws
        MnemonicException.MnemonicLengthException {
    String[] passwordSeeds = QRCodeUtil.splitOfPasswordSeed(keysString);
    String address = Base58.hexToBase58WithAddress(passwordSeeds[0]);
    String encreyptString = Utils.joinString(new String[]{passwordSeeds[1], passwordSeeds[2],
            passwordSeeds[3]}, QRCodeUtil.QR_CODE_SPLIT);
    byte[] seed = new EncryptedData(encreyptString).decrypt(password);
    MnemonicCode mnemonic = MnemonicCode.instance();

    byte[] s = mnemonic.toSeed(mnemonic.toMnemonic(seed), "");

    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(s);

    DeterministicKey purpose = master.deriveHardened(44);

    DeterministicKey coinType = purpose.deriveHardened(0);

    DeterministicKey account = coinType.deriveHardened(0);

    DeterministicKey external = account.deriveSoftened(0);

    external.clearPrivateKey();

    DeterministicKey key = external.deriveSoftened(0);
    boolean result = Utils.compareString(address, Utils.toAddress(key.getPubKeyHash()));
    key.wipe();

    return result;
}
 
Example 7
Source File: HDMKeychain.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static boolean checkPassword(String keysString, CharSequence password) throws
        MnemonicException.MnemonicLengthException {
    String[] passwordSeeds = QRCodeUtil.splitOfPasswordSeed(keysString);
    String address = Base58.hexToBase58WithAddress(passwordSeeds[0]);
    String encreyptString = Utils.joinString(new String[]{passwordSeeds[1], passwordSeeds[2],
            passwordSeeds[3]}, QRCodeUtil.QR_CODE_SPLIT);
    byte[] seed = new EncryptedData(encreyptString).decrypt(password);
    MnemonicCode mnemonic = MnemonicCode.instance();

    byte[] s = mnemonic.toSeed(mnemonic.toMnemonic(seed), "");

    DeterministicKey master = HDKeyDerivation.createMasterPrivateKey(s);

    DeterministicKey purpose = master.deriveHardened(44);

    DeterministicKey coinType = purpose.deriveHardened(0);

    DeterministicKey account = coinType.deriveHardened(0);

    DeterministicKey external = account.deriveSoftened(0);

    external.clearPrivateKey();

    DeterministicKey key = external.deriveSoftened(0);
    boolean result = Utils.compareString(address, Utils.toAddress(key.getPubKeyHash()));
    key.wipe();

    return result;
}