Search in sources :

Example 1 with Bytes

use of at.favre.lib.bytes.Bytes in project armadillo by patrickfav.

the class DefaultEncryptionProtocol method keyDerivationFunction.

private byte[] keyDerivationFunction(String contentKey, byte[] fingerprint, byte[] contentSalt, byte[] preferenceSalt, @Nullable char[] password) {
    Bytes ikm = Bytes.from(fingerprint, contentSalt, Bytes.from(contentKey, Normalizer.Form.NFKD).array());
    if (password != null) {
        byte[] stretched;
        if ((stretched = derivedPasswordCache.get(contentSalt, password)) == null) {
            stretched = defaultConfig.keyStretchingFunction.stretch(contentSalt, password, STRETCHED_PASSWORD_LENGTH_BYTES);
            derivedPasswordCache.put(contentSalt, password, stretched);
        }
        ikm = ikm.append(stretched);
    }
    return HKDF.fromHmacSha512().extractAndExpand(preferenceSalt, ikm.array(), Bytes.from("DefaultEncryptionProtocol").array(), keyLengthBit / 8);
}
Also used : Bytes(at.favre.lib.bytes.Bytes)

Example 2 with Bytes

use of at.favre.lib.bytes.Bytes in project armadillo by patrickfav.

the class ArmadilloBcryptKeyStretcher method bcrypt.

/**
 * Computes the Bcrypt hash of a password.
 *
 * @param password  the password to hash.
 * @param salt      the salt
 * @param logRounds log2(Iterations). e.g. 12 ==> 2^12 = 4,096 iterations
 * @return the Bcrypt hash of the password
 */
private static byte[] bcrypt(byte[] salt, char[] password, int logRounds) {
    StrictMode.noteSlowCall("bcrypt is a very expensive call and should not be done on the main thread");
    Bytes passwordBytes = Bytes.empty();
    try {
        passwordBytes = Bytes.from(password);
        return BCrypt.with(BCrypt.Version.VERSION_2A).hashRaw(logRounds, HKDF.fromHmacSha256().expand(salt, "bcrypt-salt".getBytes(), 16), HKDF.fromHmacSha256().expand(passwordBytes.array(), "bcrypt-pw".getBytes(), 71)).rawHash;
    } finally {
        passwordBytes.mutable().secureWipe();
    }
}
Also used : Bytes(at.favre.lib.bytes.Bytes)

Example 3 with Bytes

use of at.favre.lib.bytes.Bytes in project armadillo by patrickfav.

the class DerivedPasswordCacheTest method get.

@Test
public void get() {
    Bytes val = Bytes.random(128);
    cache.put(salt, pw, val.copy().array());
    assertEquals(val, Bytes.wrapNullSafe(cache.get(salt, pw)));
    assertEquals(val, Bytes.wrapNullSafe(cache.get(salt, pw)));
    cache.wipe();
    assertNull(cache.get(salt, pw));
}
Also used : Bytes(at.favre.lib.bytes.Bytes) Test(org.junit.Test)

Aggregations

Bytes (at.favre.lib.bytes.Bytes)3 Test (org.junit.Test)1