SHARE
TWEET

Untitled

a guest Jul 21st, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
  2. fun generateAsymmetricKey(keyAlias: String, context: Context): KeyStore.Entry {
  3.  
  4.     val keyStore = KeyStore.getInstance("AndroidKeyStore")
  5.     keyStore.load(null)
  6.  
  7.     if (!keyStore.containsAlias(keyAlias)) {
  8.         val startDate = Calendar.getInstance()
  9.         val endDate = Calendar.getInstance()
  10.         endDate.add(Calendar.YEAR, 25)
  11.         val keyPairGeneratorSpec = KeyPairGeneratorSpec.Builder(context.applicationContext)
  12.             .setAlias(keyAlias)
  13.             .setSubject(X500Principal("CN=$keyAlias"))
  14.             .setSerialNumber(BigInteger.valueOf(123456))
  15.             .setStartDate(startDate.time)
  16.             .setEndDate(endDate.time)
  17.             .build()
  18.         val keyPairGenerator = KeyPairGenerator.getInstance(
  19.             "RSA",
  20.             "AndroidKeyStore"
  21.         )
  22.         keyPairGenerator.initialize(keyPairGeneratorSpec)
  23.         keyPairGenerator.generateKeyPair()
  24.     }
  25.     return keyStore.getEntry(keyAlias, null)
  26. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top