Java Code Examples for org.spongycastle.crypto.generators.SCrypt#generate()

The following examples show how to use org.spongycastle.crypto.generators.SCrypt#generate() . 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: Bip38.java    From BlockchainWallet-Crypto with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Perform BIP38 compatible password stretching on a password to derive the
 * BIP38 key material
 *
 * @throws InterruptedException
 */
public static byte[] bip38Stretch1(CharSequence passphrase, byte[] salt, int outputSize)
        throws InterruptedException {
    byte[] passwordBytes = null;
    byte[] derived;
    try {
        passwordBytes = convertToByteArray(passphrase);
        derived = SCrypt.generate(passwordBytes, salt, SCRYPT_N, SCRYPT_R, SCRYPT_P, outputSize
        );
        return derived;
    } finally {
        // Zero the password bytes.
        if (passwordBytes != null) {
            java.util.Arrays.fill(passwordBytes, (byte) 0);
        }
    }
}
 
Example 2
Source File: ExtendedKey.java    From BlockchainWallet-Crypto with GNU General Public License v3.0 6 votes vote down vote up
public byte[] encrypt(String passphrase, boolean production) throws ValidationException {
    try {
        byte[] key = SCrypt.generate(passphrase.getBytes("UTF-8"), BITCOIN_SEED, 16384, 8, 8,
                32);
        SecretKeySpec keyspec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
        cipher.init(Cipher.ENCRYPT_MODE, keyspec);
        byte[] iv = cipher.getIV();
        byte[] c = cipher.doFinal(serialize(production).getBytes());
        byte[] result = new byte[iv.length + c.length];
        System.arraycopy(iv, 0, result, 0, iv.length);
        System.arraycopy(c, 0, result, iv.length, c.length);
        return result;
    } catch (UnsupportedEncodingException | NoSuchAlgorithmException |
            NoSuchProviderException | NoSuchPaddingException | InvalidKeyException
            | IllegalBlockSizeException | BadPaddingException e) {
        throw new ValidationException(e);
    }
}
 
Example 3
Source File: KeystoreFormat.java    From aion with MIT License 4 votes vote down vote up
private static byte[] scrypt(byte[] pass, byte[] salt, int n, int r, int p, int dkLen)
        throws GeneralSecurityException {
    return SCrypt.generate(pass, salt, n, r, p, dkLen);
}
 
Example 4
Source File: KeystoreFormat.java    From aion_api with MIT License 4 votes vote down vote up
private static byte[] scrypt(byte[] pass, byte[] salt, int n, int r, int p, int dkLen) {
    return SCrypt.generate(pass, salt, n, r, p, dkLen);
}
 
Example 5
Source File: Wallet.java    From gsc-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static byte[] generateDerivedScryptKey(
        byte[] password, byte[] salt, int n, int r, int p, int dkLen) throws CipherException {
    return SCrypt.generate(password, salt, n, r, p, dkLen);
}
 
Example 6
Source File: KeyStore.java    From BlockchainWallet-Crypto with GNU General Public License v3.0 4 votes vote down vote up
private static byte[] generateDerivedScryptKey(
        byte[] password, byte[] salt, int n, int r, int p, int dkLen) {
    return SCrypt.generate(password, salt, n, r, p, dkLen);
}
 
Example 7
Source File: LWallet.java    From dapp-wallet-demo with Apache License 2.0 4 votes vote down vote up
private static byte[] generateDerivedScryptKey(
        byte[] password, byte[] salt, int n, int r, int p, int dkLen) throws CipherException {
    return SCrypt.generate(password, salt, n, r, p, dkLen);
}
 
Example 8
Source File: CommonWallet.java    From wkcwallet-java with Apache License 2.0 4 votes vote down vote up
@Override
public String toV3(String password, int n, int p, int r) {

    // derived key
    SecureRandom random = new SecureRandom();
    byte[] salt = new byte[64];
    random.nextBytes(salt);
    int dkLen = 32;
    byte[] derivedkey = SCrypt.generate(password.getBytes(), salt, n, r, p, dkLen);
    byte[] dk = Arrays.copyOf(derivedkey, 16);
    byte[] vk = Arrays.copyOfRange(derivedkey, 16, 32);

    // Encrypt
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES/CTR/NoPadding");
        SecretKey aesKey = new SecretKeySpec(dk, "AES");
        byte[] iv = new byte[16];
        random.nextBytes(iv);
        IvParameterSpec iv_spec = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey, iv_spec);
        byte[] ciphertext = cipher.doFinal(getPrivateKey());

        // Calc MAC
        byte[] mac = HashUtil.sha3(Arrays.concatenate(vk, ciphertext));

        // Output
        StringBuilder sb = new StringBuilder();
        sb.append("{\"address\":\"").append(getAddressString()).append('"');
        sb.append(",\"crypto\":{\"cipher\":\"aes-128-ctr\"");
        sb.append(",\"ciphertext\":\"").append(Hex.toHexString(ciphertext)).append('"');
        sb.append(",\"cipherparams\":{");
        sb.append("\"iv\":\"").append(Hex.toHexString(iv)).append('"');
        sb.append("}");
        sb.append(",\"kdf\":\"").append("scrypt").append('"');
        sb.append(",\"kdfparams\":{");
        sb.append("\"dklen\":").append(dkLen);
        sb.append(",\"n\":").append(n);
        sb.append(",\"r\":").append(r);
        sb.append(",\"p\":").append(p);
        sb.append(",\"salt\":\"").append(Hex.toHexString(salt)).append('"');
        sb.append('}');
        sb.append(",\"mac\":\"").append(Hex.toHexString(mac)).append('"');
        sb.append('}');
        sb.append(",\"id\":\"").append(UUID.randomUUID()).append('"');
        sb.append(",\"version\":3}");
        return sb.toString();
    } catch (Exception e) {
        throw new RuntimeCryptoException();
    }

}