Last active
October 26, 2020 19:30
-
-
Save kikoso/5164985782477b7d60fdd1ee44c546ae to your computer and use it in GitHub Desktop.
KeyPair.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private lateinit var keyPair: KeyPair | |
private fun generateKey() { | |
val startDate = GregorianCalendar() | |
val endDate = GregorianCalendar() | |
endDate.add(Calendar.YEAR, 1) | |
val keyPairGenerator: KeyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, ANDROID_KEYSTORE) | |
val parameterSpec: KeyGenParameterSpec = KeyGenParameterSpec.Builder(KEY_ALIAS, | |
KeyProperties.PURPOSE_SIGN or KeyProperties.PURPOSE_VERIFY).run { | |
setCertificateSerialNumber(BigInteger.valueOf(777)) //Serial number used for the self-signed certificate of the generated key pair, default is 1 | |
setCertificateSubject(X500Principal("CN=$KEY_ALIAS")) //Subject used for the self-signed certificate of the generated key pair, default is CN=fake | |
setDigests(KeyProperties.DIGEST_SHA256) //Set of digests algorithms with which the key can be used | |
setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PKCS1) //Set of padding schemes with which the key can be used when signing/verifying | |
setCertificateNotBefore(startDate.time) //Start of the validity period for the self-signed certificate of the generated, default Jan 1 1970 | |
setCertificateNotAfter(endDate.time) //End of the validity period for the self-signed certificate of the generated key, default Jan 1 2048 | |
setUserAuthenticationRequired(true) //Sets whether this key is authorized to be used only if the user has been authenticated, default false | |
setUserAuthenticationValidityDurationSeconds(30) //Duration(seconds) for which this key is authorized to be used after the user is successfully authenticated | |
build() | |
} | |
//Initialization of key generator with the parameters we have specified above | |
keyPairGenerator.initialize(parameterSpec) | |
//Generates the key pair | |
keyPair = keyPairGenerator.genKeyPair() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment