Search in sources :

Example 1 with Argon2KeyDeriver

use of com.sparrowwallet.drongo.crypto.Argon2KeyDeriver in project drongo by sparrowwallet.

the class WalletTest method encryptTest.

@Test
public void encryptTest() throws MnemonicException {
    String words = "absent essay fox snake vast pumpkin height crouch silent bulb excuse razor";
    DeterministicSeed seed = new DeterministicSeed(words, "pp", 0, DeterministicSeed.Type.BIP39);
    Wallet wallet = new Wallet();
    wallet.setPolicyType(PolicyType.SINGLE);
    wallet.setScriptType(ScriptType.P2PKH);
    Keystore keystore = Keystore.fromSeed(seed, wallet.getScriptType().getDefaultDerivation());
    wallet.getKeystores().add(keystore);
    wallet.setDefaultPolicy(Policy.getPolicy(PolicyType.SINGLE, ScriptType.P2PKH, wallet.getKeystores(), 1));
    KeyDeriver keyDeriver = new Argon2KeyDeriver();
    Key key = keyDeriver.deriveKey("pass");
    wallet.encrypt(key);
    wallet.decrypt("pass");
}
Also used : Argon2KeyDeriver(com.sparrowwallet.drongo.crypto.Argon2KeyDeriver) KeyDeriver(com.sparrowwallet.drongo.crypto.KeyDeriver) Argon2KeyDeriver(com.sparrowwallet.drongo.crypto.Argon2KeyDeriver) ExtendedKey(com.sparrowwallet.drongo.ExtendedKey) Key(com.sparrowwallet.drongo.crypto.Key) Test(org.junit.Test)

Example 2 with Argon2KeyDeriver

use of com.sparrowwallet.drongo.crypto.Argon2KeyDeriver in project sparrow by sparrowwallet.

the class JsonPersistence method getWalletKeyDeriver.

private AsymmetricKeyDeriver getWalletKeyDeriver(InputStream inputStream) throws IOException, StorageException {
    byte[] salt = new byte[SPRW1_PARAMETERS.saltLength];
    if (inputStream != null) {
        byte[] header = new byte[BINARY_HEADER_LENGTH];
        int read = inputStream.read(header);
        if (read != BINARY_HEADER_LENGTH) {
            throw new StorageException("Not a Sparrow wallet - invalid header");
        }
        try {
            byte[] decodedHeader = Base64.getDecoder().decode(header);
            byte[] magic = Arrays.copyOfRange(decodedHeader, 0, HEADER_MAGIC_1.length());
            if (!HEADER_MAGIC_1.equals(new String(magic, StandardCharsets.UTF_8))) {
                throw new StorageException("Not a Sparrow wallet - invalid magic");
            }
            salt = Arrays.copyOfRange(decodedHeader, HEADER_MAGIC_1.length(), decodedHeader.length);
        } catch (IllegalArgumentException e) {
            throw new StorageException("Not a Sparrow wallet - invalid header");
        }
    } else {
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(salt);
    }
    return new Argon2KeyDeriver(salt);
}
Also used : SecureRandom(java.security.SecureRandom) Argon2KeyDeriver(com.sparrowwallet.drongo.crypto.Argon2KeyDeriver)

Example 3 with Argon2KeyDeriver

use of com.sparrowwallet.drongo.crypto.Argon2KeyDeriver in project sparrow by sparrowwallet.

the class DbPersistence method getWalletKeyDeriver.

private AsymmetricKeyDeriver getWalletKeyDeriver(File walletFile) throws IOException {
    if (keyDeriver == null) {
        byte[] salt = new byte[SALT_LENGTH_BYTES];
        if (walletFile != null && walletFile.exists()) {
            try (InputStream inputStream = new FileInputStream(walletFile)) {
                inputStream.skip(H2_ENCRYPT_HEADER.length + H2_ENCRYPT_SALT_LENGTH_BYTES + HEADER_MAGIC_1.length);
                inputStream.read(salt, 0, salt.length);
            }
        } else {
            SecureRandom secureRandom = new SecureRandom();
            secureRandom.nextBytes(salt);
        }
        return new Argon2KeyDeriver(salt);
    }
    return keyDeriver;
}
Also used : SecureRandom(java.security.SecureRandom) Argon2KeyDeriver(com.sparrowwallet.drongo.crypto.Argon2KeyDeriver)

Aggregations

Argon2KeyDeriver (com.sparrowwallet.drongo.crypto.Argon2KeyDeriver)3 SecureRandom (java.security.SecureRandom)2 ExtendedKey (com.sparrowwallet.drongo.ExtendedKey)1 Key (com.sparrowwallet.drongo.crypto.Key)1 KeyDeriver (com.sparrowwallet.drongo.crypto.KeyDeriver)1 Test (org.junit.Test)1