Skip to content

Instantly share code, notes, and snippets.

@aramezx
Last active March 6, 2019 03:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aramezx/c46c8855e3e73b4a0ec8ae7850826a31 to your computer and use it in GitHub Desktop.
Save aramezx/c46c8855e3e73b4a0ec8ae7850826a31 to your computer and use it in GitHub Desktop.
Generating a new PrivateKey using a KeyPairGenerator with KeyPairGeneratorSpec
/*
* Generate a new EC key pair entry in the Android Keystore by
* using the KeyPairGenerator API. The private key can only be
* used for signing or verification and only with SHA-256 or
* SHA-512 as the message digest.
*/
KeyPairGenerator kpg = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
kpg.initialize(new KeyGenParameterSpec.Builder(
alias,
KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY)
.setDigests(KeyProperties.DIGEST_SHA256,
KeyProperties.DIGEST_SHA512)
.build());
KeyPair kp = kpg.generateKeyPair();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment