Java Code Examples for org.spongycastle.crypto.generators.PKCS5S2ParametersGenerator#init()

The following examples show how to use org.spongycastle.crypto.generators.PKCS5S2ParametersGenerator#init() . 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: PBKDF2Crypto.java    From token-core-android with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] generateDerivedKey(byte[] password) {
  PBKDF2Params params = this.kdfparams;
  if (!PBKDF2Params.PRF.equals(params.getPrf())) {
    throw new TokenException(Messages.PRF_UNSUPPORTED);
  }

  PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator(new SHA256Digest());
  generator.init(password, NumericUtil.hexToBytes(params.getSalt()), params.getC());
  return ((KeyParameter) generator.generateDerivedParameters(256)).getKey();
}
 
Example 2
Source File: PBKDFTest.java    From token-core-android with Apache License 2.0 5 votes vote down vote up
@Test
public void derive() {
  for (String[] example : PBKDFExample) {
    PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
    gen.init(example[3].getBytes(), example[0].getBytes(), Integer.parseInt(example[1]));
    byte[] derivedKey = ((KeyParameter) gen.generateDerivedParameters(Integer.parseInt(example[2]) * 8)).getKey();
    Assert.assertEquals(NumericUtil.bytesToHex(derivedKey), example[4]);
  }
}
 
Example 3
Source File: Wallet.java    From gsc-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static byte[] generateAes128CtrDerivedKey(
        byte[] password, byte[] salt, int c, String prf) throws CipherException {

    if (!prf.equals("hmac-sha256")) {
        throw new CipherException("Unsupported prf:" + prf);
    }

    // Java 8 supports this, but you have to convert the password to a character array, see
    // http://stackoverflow.com/a/27928435/3211687

    PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
    gen.init(password, salt, c);
    return ((KeyParameter) gen.generateDerivedParameters(256)).getKey();
}
 
Example 4
Source File: KeyStore.java    From BlockchainWallet-Crypto with GNU General Public License v3.0 5 votes vote down vote up
private static byte[] generateAes128CtrDerivedKey(
        byte[] password, byte[] salt, int c, String prf) throws CipherException {

    if (!prf.equals("hmac-sha256")) {
        throw new CipherException("Unsupported prf:" + prf);
    }

    // Java 8 supports this, but you have to convert the password to a character array, see
    // http://stackoverflow.com/a/27928435/3211687
    PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
    gen.init(password, salt, c);
    return ((KeyParameter) gen.generateDerivedParameters(256)).getKey();
}
 
Example 5
Source File: LWallet.java    From dapp-wallet-demo with Apache License 2.0 5 votes vote down vote up
private static byte[] generateAes128CtrDerivedKey(
        byte[] password, byte[] salt, int c, String prf) throws CipherException {

    if (!prf.equals("hmac-sha256")) {
        throw new CipherException("Unsupported prf:" + prf);
    }

    // Java 8 supports this, but you have to convert the password to a character array, see
    // http://stackoverflow.com/a/27928435/3211687

    PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA256Digest());
    gen.init(password, salt, c);
    return ((KeyParameter) gen.generateDerivedParameters(256)).getKey();
}
 
Example 6
Source File: MnemonicUtils.java    From Android-Wallet-Token-ERC20 with Apache License 2.0 3 votes vote down vote up
/**
 * To create a binary seed from the mnemonic, we use the PBKDF2 function with a
 * mnemonic sentence (in UTF-8 NFKD) used as the password and the string "mnemonic"
 * + passphrase (again in UTF-8 NFKD) used as the salt. The iteration count is set
 * to 2048 and HMAC-SHA512 is used as the pseudo-random function. The length of the
 * derived key is 512 bits (= 64 bytes).
 *
 * @param mnemonic The input mnemonic which should be 128-160 bits in length containing
 *                 only valid words
 * @param passphrase The passphrase which will be used as part of salt for PBKDF2
 *                   function
 * @return Byte array representation of the generated seed
 */
public static byte[] generateSeed(String mnemonic, String passphrase) {
    validateMnemonic(mnemonic);
    passphrase = passphrase == null ? "" : passphrase;

    String salt = String.format("mnemonic%s", passphrase);
    PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA512Digest());
    gen.init(mnemonic.getBytes(Charset.forName("UTF-8")), salt.getBytes(Charset.forName("UTF-8")), SEED_ITERATIONS);

    return ((KeyParameter) gen.generateDerivedParameters(SEED_KEY_SIZE)).getKey();
}