net.bither.bitherj.qrcode.QRCodeUtil Java Examples

The following examples show how to use net.bither.bitherj.qrcode.QRCodeUtil. 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: BackupUtil.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
private static ECKey getEckeyFormBackupHot(String address, CharSequence password) {
    File file = FileUtil.getBackupFile();
    String str = Utils.readFile(file);
    if (str.contains(address)) {
        String[] backupStrArray = str.split(PrivateKeyUtil.BACKUP_KEY_SPLIT_MUTILKEY_STRING);
        for (String backupStr : backupStrArray) {
            if (backupStr.contains(address)) {
                String[] strArray = QRCodeUtil.splitString(backupStr);
                if (strArray.length > 3) {
                    String keyString = backupStr.substring(strArray[0]
                            .length() + 1);
                    return PrivateKeyUtil.getECKeyFromSingleString(
                            keyString, password);
                }
            }
        }
    }
    return null;
}
 
Example #2
Source File: EnterpriseHDMSendCollectSignatureActivity.java    From bither-android with Apache License 2.0 6 votes vote down vote up
private void addSignature(String qr) {
    boolean success;
    try {
        String[] stringArray = QRCodeUtil.splitString(qr);
        ArrayList<byte[]> sigs = new ArrayList<byte[]>();
        for (String str : stringArray) {
            if (!Utils.isEmpty(str)) {
                sigs.add(Utils.hexStringToByteArray(str));
            }
        }
        success = pool.addSignature(sigs);
    } catch (Exception e) {
        e.printStackTrace();
        success = false;
    }
    if (!success) {
        DropdownMessage.showDropdownMessage(this, R.string
                .enterprise_hdm_keychain_payment_proposal_sign_failed);
    }
}
 
Example #3
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 #4
Source File: DialogPrivateKeyQrCode.java    From bither-android with Apache License 2.0 6 votes vote down vote up
public DialogPrivateKeyQrCode(Activity context, String keyString,
                              BitherSetting.QRCodeType type, String address) {
    super(context, R.style.tipsDialog);
    this.activity = context;
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    getWindow().getAttributes().dimAmount = 0.8f;
    setCanceledOnTouchOutside(true);
    if (type == BitherSetting.QRCodeType.Bither) {
        this.content = QRCodeUtil.encodeQrCodeString(keyString);
    } else {
        this.content = keyString;
    }
    this.address = address;
    setContentView(R.layout.dialog_private_key_qr_code);
    setOnDismissListener(this);
    initView();
    getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT);
    new FancyQrCodeThread(this.content, ivQr.getLayoutParams().width, Color.BLACK,
            Color.WHITE, this, false).start();
}
 
Example #5
Source File: BackupUtil.java    From bither-android with Apache License 2.0 6 votes vote down vote up
private static ECKey getEckeyFormBackupHot(String address, CharSequence password) {
    File file = FileUtil.getBackupKeyOfHot();
    String str = Utils.readFile(file);
    if (str.contains(address)) {
        String[] backupStrArray = str.split(PrivateKeyUtil.BACKUP_KEY_SPLIT_MUTILKEY_STRING);
        for (String backupStr : backupStrArray) {
            if (backupStr.contains(address)) {
                String[] strArray = QRCodeUtil.splitString(backupStr);
                if (strArray.length > 3) {
                    String keyString = backupStr.substring(strArray[0]
                            .length() + 1);
                    return PrivateKeyUtil.getECKeyFromSingleString(
                            keyString, password);
                }
            }
        }
    }
    return null;
}
 
Example #6
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 #7
Source File: BackupUtil.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
private static ECKey getEckeyFormBackupHot(String address, CharSequence password) {
    File file = FileUtil.getBackupFile();
    String str = Utils.readFile(file);
    if (str.contains(address)) {
        String[] backupStrArray = str.split(PrivateKeyUtil.BACKUP_KEY_SPLIT_MUTILKEY_STRING);
        for (String backupStr : backupStrArray) {
            if (backupStr.contains(address)) {
                String[] strArray = QRCodeUtil.splitString(backupStr);
                if (strArray.length > 3) {
                    String keyString = backupStr.substring(strArray[0]
                            .length() + 1);
                    return PrivateKeyUtil.getECKeyFromSingleString(
                            keyString, password);
                }
            }
        }
    }
    return null;
}
 
Example #8
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 #9
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static String getEncryptPrivateKeyStringFromAllAddresses() {
    String content = "";
    List<Address> privates = AddressManager.getInstance().getPrivKeyAddresses();
    for (int i = 0;
         i < privates.size();
         i++) {
        Address address = privates.get(i);
        content += address.getFullEncryptPrivKey();
        if (i < privates.size() - 1) {
            content += QRCodeUtil.QR_CODE_SPLIT;
        }
    }
    HDMKeychain keychain = AddressManager.getInstance().getHdmKeychain();
    if (keychain != null) {
        if (Utils.isEmpty(content)) {
            content += keychain.getQRCodeFullEncryptPrivKey();
        } else {
            content += QRCodeUtil.QR_CODE_SPLIT + keychain.getQRCodeFullEncryptPrivKey();
        }
    }
    HDAccount hdAccount = AddressManager.getInstance().getHDAccountHot();
    if (hdAccount != null) {
        if (Utils.isEmpty(content)) {
            content += hdAccount.getQRCodeFullEncryptPrivKey();
        } else {
            content += QRCodeUtil.QR_CODE_SPLIT + hdAccount.getQRCodeFullEncryptPrivKey();
        }
    }
    HDAccountCold hdAccountCold = AddressManager.getInstance().getHDAccountCold();
    if (hdAccountCold != null) {
        if (Utils.isEmpty(content)) {
            content += hdAccountCold.getQRCodeFullEncryptPrivKey();
        } else {
            content += QRCodeUtil.QR_CODE_SPLIT + hdAccountCold.getQRCodeFullEncryptPrivKey();
        }
    }
    return content;
}
 
Example #10
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static String getEncryptedString(ECKey ecKey) {
    String salt = "1";
    if (ecKey.getKeyCrypter() instanceof KeyCrypterScrypt) {
        KeyCrypterScrypt scrypt = (KeyCrypterScrypt) ecKey.getKeyCrypter();
        salt = Utils.bytesToHexString(scrypt.getSalt());
    }
    EncryptedPrivateKey key = ecKey.getEncryptedPrivateKey();
    return Utils.bytesToHexString(key.getEncryptedBytes()) + QRCodeUtil.QR_CODE_SPLIT + Utils
            .bytesToHexString(key.getInitialisationVector()) + QRCodeUtil.QR_CODE_SPLIT + salt;
}
 
Example #11
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 #12
Source File: ColdAdvanceActivity.java    From bither-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onOptionIndexSelected(int index) {
    if (index >= 0 && index < getOptionCount()) {
        AppSharedPreference.getInstance().setQRQuality(QRCodeUtil.QRQuality.values()
                [index]);
    }
}
 
Example #13
Source File: UserPreference.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
public void setQRQuality(QRCodeUtil.QRQuality qrQuality) {
    int index = -1;
    if (qrQuality != null) {
        index = qrQuality.ordinal();
    }
    setValue(QR_QUALITY, Integer.toString(index));

}
 
Example #14
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static String getFullencryptPrivateKey(Address address, String encryptPrivKey) {
    String[] strings = QRCodeUtil.splitString(encryptPrivKey);
    byte[] salt = Utils.hexStringToByteArray(strings[2]);
    if (salt.length == KeyCrypterScrypt.SALT_LENGTH) {
        SaltForQRCode saltForQRCode = new SaltForQRCode(salt, address.isCompressed(), address.isFromXRandom());
        strings[2] = Utils.bytesToHexString(saltForQRCode.getQrCodeSalt());
    }
    return Utils.joinString(strings, QRCodeUtil.QR_CODE_SPLIT);
}
 
Example #15
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public static String getFullencryptHDMKeyChain(boolean isFromXRandom, String encryptPrivKey) {
    String[] strings = QRCodeUtil.splitString(encryptPrivKey);
    byte[] salt = Utils.hexStringToByteArray(strings[2]);
    if (salt.length == KeyCrypterScrypt.SALT_LENGTH) {
        SaltForQRCode saltForQRCode = new SaltForQRCode(salt, true, isFromXRandom);
        strings[2] = Utils.bytesToHexString(saltForQRCode.getQrCodeSalt()).toUpperCase();
    }
    return Utils.joinString(strings, QRCodeUtil.QR_CODE_SPLIT);
}
 
Example #16
Source File: AddEnterpriseHDMSeedActivity.java    From bither-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ImportFromQRTag) {
        final String seedStr = data.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
        if (seedStr.indexOf(QRCodeUtil.Enterprise_HDM_QR_CODE_FLAG) == 0) {
            DialogPassword dialogPassword = new DialogPassword(this, new
                    ImportSeedPasswordListener(seedStr));
            dialogPassword.setCheckPre(false);
            dialogPassword.setCheckPasswordListener(new ICheckPasswordListener() {
                @Override
                public boolean checkPassword(SecureCharSequence password) {
                    String keyString = seedStr.substring(1);
                    String[] passwordSeeds = QRCodeUtil.splitOfPasswordSeed(keyString);
                    String encreyptString = Utils.joinString(new String[]{passwordSeeds[0],
                            passwordSeeds[1], passwordSeeds[2]}, QRCodeUtil.QR_CODE_SPLIT);
                    EncryptedData encryptedData = new EncryptedData(encreyptString);
                    byte[] result = null;
                    try {
                        result = encryptedData.decrypt(password);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return result != null;
                }
            });
            dialogPassword.setTitle(R.string.import_private_key_qr_code_password);
            dialogPassword.show();
        } else {
            DropdownMessage.showDropdownMessage(AddEnterpriseHDMSeedActivity.this, R.string
                    .enterprise_hdm_seed_import_format_error);
        }
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}
 
Example #17
Source File: UpgradeAddressUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private static List<Address> initPrivateKeyListByDesc() {
    List<Address> privKeyAddresses = new ArrayList<Address>();

    File[] files = Utils.getPrivateDir().listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.getName().contains(Address.PUBLIC_KEY_FILE_NAME_SUFFIX)) {
                String content = Utils.readFile(file);
                String[] strings = content.split(Address.KEY_SPLIT_STRING);
                String address = file.getName().substring(0,
                        file.getName().length() - Address.PUBLIC_KEY_FILE_NAME_SUFFIX.length());
                String publicKey = strings[0];
                int isSyncComplete = Integer.valueOf(strings[1]);
                long sortTime = Long.valueOf(strings[2]);
                boolean isFromXRandom = false;
                if (strings.length == 4) {
                    isFromXRandom = Utils.compareString(strings[3], QRCodeUtil.XRANDOM_FLAG);
                }
                String privateKeyFullFileName = Utils.format(BitherjSettings
                        .PRIVATE_KEY_FILE_NAME, Utils.getPrivateDir(), address);
                String encryptPrivate = QRCodeUtil.getNewVersionEncryptPrivKey(Utils.readFile(new File(privateKeyFullFileName)));
                encryptPrivate = PrivateKeyUtil.formatEncryptPrivateKeyForDb(encryptPrivate);
                Address add = new Address(address, Utils.hexStringToByteArray(publicKey), sortTime
                        , isSyncComplete == 1, isFromXRandom, false, encryptPrivate);
                privKeyAddresses.add(add);

            }
        }
        if (privKeyAddresses.size() > 0) {
            Collections.sort(privKeyAddresses);
        }
    }
    return privKeyAddresses;
}
 
Example #18
Source File: UpgradeAddressUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private static List<Address> initWatchOnlyListByDesc() {
    List<Address> watchOnlyAddresses = new ArrayList<Address>();
    File[] files = Utils.getWatchOnlyDir().listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.getName().contains(Address.PUBLIC_KEY_FILE_NAME_SUFFIX)) {
                String content = Utils.readFile(file);
                String[] strings = content.split(Address.KEY_SPLIT_STRING);
                String address = file.getName().substring(0,
                        file.getName().length() - Address.PUBLIC_KEY_FILE_NAME_SUFFIX.length());
                String publicKey = strings[0];
                int isSyncComplete = Integer.valueOf(strings[1]);
                long sortTime = Long.valueOf(strings[2]);
                boolean isFromXRandom = false;
                if (strings.length == 4) {
                    isFromXRandom = Utils.compareString(strings[3], QRCodeUtil.XRANDOM_FLAG);
                }
                Address add = new Address(address, Utils.hexStringToByteArray(publicKey), sortTime
                        , isSyncComplete == 1, isFromXRandom, false, null);
                watchOnlyAddresses.add(add);

            }
        }
        if (watchOnlyAddresses.size() > 0) {
            Collections.sort(watchOnlyAddresses);
        }
    }
    return watchOnlyAddresses;
}
 
Example #19
Source File: UpgradeAddressUtil.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private static List<Address> initTrashListByDesc() {
    File[] files = Utils.getTrashDir().listFiles();
    List<Address> trashAddresses = new ArrayList<Address>();
    if (files != null) {
        for (File file : files) {
            if (file.getName().contains(Address.PUBLIC_KEY_FILE_NAME_SUFFIX)) {
                String content = Utils.readFile(file);
                String[] strings = content.split(Address.KEY_SPLIT_STRING);
                String address = file.getName().substring(0,
                        file.getName().length() - Address.PUBLIC_KEY_FILE_NAME_SUFFIX.length());
                String publicKey = strings[0];
                int isSyncComplete = Integer.valueOf(strings[1]);
                long sortTime = Long.valueOf(strings[2]);
                boolean isFromXRandom = false;
                if (strings.length == 4) {
                    isFromXRandom = Utils.compareString(strings[3], QRCodeUtil.XRANDOM_FLAG);
                }

                String privateKeyFullFileName = Utils.format(BitherjSettings
                        .PRIVATE_KEY_FILE_NAME, Utils.getTrashDir(), address);
                String encryptPrivate = QRCodeUtil.getNewVersionEncryptPrivKey(Utils.readFile(new File(privateKeyFullFileName)));
                encryptPrivate = PrivateKeyUtil.formatEncryptPrivateKeyForDb(encryptPrivate);
                Address add = new Address(address, Utils.hexStringToByteArray(publicKey), sortTime
                        , isSyncComplete == 1, isFromXRandom, true, encryptPrivate);
                add.setTrashed(true);
                trashAddresses.add(add);
            }
        }
        if (trashAddresses.size() > 0) {
            Collections.sort(trashAddresses);
        }
    }
    return trashAddresses;
}
 
Example #20
Source File: ImportPrivateKey.java    From bitherj with Apache License 2.0 5 votes vote down vote up
private Address addECKey(ECKey ecKey) {
    String encryptedPrivateString;
    if (importPrivateKeyType == ImportPrivateKeyType.BitherQrcode) {
        encryptedPrivateString = QRCodeUtil.getNewVersionEncryptPrivKey(content);
    } else {
        ecKey = PrivateKeyUtil.encrypt(ecKey, password);
        encryptedPrivateString = PrivateKeyUtil.getEncryptedString(ecKey);
    }
    Address address = new Address(ecKey.toAddress(), ecKey.getPubKey(), encryptedPrivateString
            , false, ecKey.isFromXRandom());
    if (AddressManager.getInstance().getWatchOnlyAddresses().contains(address)) {
        password.wipe();
        importError(CAN_NOT_IMPORT_BITHER_COLD_PRIVATE_KEY);
        return null;
    } else if (AddressManager.getInstance().getPrivKeyAddresses().contains(address)) {
        password.wipe();
        importError(PRIVATE_KEY_ALREADY_EXISTS);
        return null;

    } else {
        if (importPrivateKeyType == ImportPrivateKeyType.BitherQrcode) {
            PasswordSeed passwordSeed = PasswordSeed.getPasswordSeed();
            if (passwordSeed != null && !passwordSeed.checkPassword(password)) {
                password.wipe();
                importError(PASSWORD_IS_DIFFEREND_LOCAL);
                return null;
            }
        } else {
            password.wipe();
        }
        return address;


    }

}
 
Example #21
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 #22
Source File: PasswordSeed.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public String toPasswordSeedString() {
    try {
        String passwordSeedString = Base58.bas58ToHexWithAddress(this.address) + QRCodeUtil.QR_CODE_SPLIT
                + QRCodeUtil.getNewVersionEncryptPrivKey(this.keyStr);
        return passwordSeedString;
    } catch (AddressFormatException e) {
        throw new RuntimeException("passwordSeed  address is format error ," + this.address);

    }

}
 
Example #23
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 #24
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 #25
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;
}
 
Example #26
Source File: HDAccountCold.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public String accountPubExtendedString(CharSequence password) throws MnemonicException
        .MnemonicLengthException {
    byte[] extended = accountPubExtended(password);
    String result = "";
    if (isFromXRandom) {
        result += QRCodeUtil.XRANDOM_FLAG;
    }
    result += Utils.bytesToHexString(extended).toUpperCase();
    return result;
}
 
Example #27
Source File: StringTest.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testString() {
    boolean result =
            QRCodeUtil.verifyBitherQRCode("-E0B56EB20152755D3287BEBAAB612BB4049E736A44301729D878D54CA0912DF84F88BBDBE7330CA412FF700991BE8FE1/989A374A3B0F301808654DCD6264F368/01f120397c1017c628");
    if (result) {

    } else {

    }
}
 
Example #28
Source File: BackupUtil.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
private static ECKey getEckeyFormBackupCold(String address, CharSequence password) {

        try {
            File[] files = FileUtil.getBackupDir().listFiles();
            if (files == null) {
                return null;
            }
            files = FileUtil.orderByDateDesc(files);
            for (int i = files.length - 1;
                 i >= 0;
                 i++) {
                File file = files[i];
                String str = Utils.readFile(file);
                if (str.contains(address)) {
                    String[] backupStrArray = str.split(PrivateKeyUtil.BACKUP_KEY_SPLIT_MUTILKEY_STRING);
                    for (String backupStr : backupStrArray) {
                        if (backupStr.contains(address)) {
                            String[] strArray = QRCodeUtil.splitString(backupStr);
                            if (strArray.length > 3) {
                                String keyString = backupStr
                                        .substring(strArray[0].length() + 1);
                                return PrivateKeyUtil.getECKeyFromSingleString(
                                        keyString, password);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
 
Example #29
Source File: SendHDMBitcoinPanel.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
public boolean setQrCodeResult(String qr) {

            if (!Utils.isEmpty(qr)) {
                try {
                    String[] stringArray = QRCodeUtil.splitString(qr);
                    sigs = new ArrayList<TransactionSignature>();
                    for (String str : stringArray) {
                        if (!Utils.isEmpty(str)) {
                            TransactionSignature transactionSignature = new
                                    TransactionSignature(ECKey.ECDSASignature.decodeFromDER
                                    (Utils.hexStringToByteArray(str)),
                                    TransactionSignature.SigHash.ALL, false);
                            sigs.add(transactionSignature);
                        }
                    }
                } catch (Exception e) {
                    sigs = null;
                    e.printStackTrace();
                    new MessageDialog(LocaliserUtils.getString("send_failed")).showMsg();
                }
            } else {
                sigs = null;
            }
            try {
                lock.lock();
                fetchedCondition.signal();
            } finally {
                lock.unlock();
            }
            return true;

        }
 
Example #30
Source File: UserPreference.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
public QRCodeUtil.QRQuality getQRQuality() {
    int ordinal = getInt(QR_QUALITY, -1);
    if (ordinal < QRCodeUtil.QRQuality.values().length && ordinal >= 0) {
        return QRCodeUtil.QRQuality.values()[ordinal];
    }
    return QRCodeUtil.QRQuality.Normal;

}