Skip to content

Instantly share code, notes, and snippets.

View yakivmospan's full-sized avatar

Yakiv Mospan yakivmospan

View GitHub Profile
companion object {
var TRANSFORMATION_ASYMMETRIC = "RSA/ECB/PKCS1Padding"
}
val cipher: Cipher = Cipher.getInstance(transformation)
fun getAndroidKeyStoreAsymmetricKeyPair(alias: String): KeyPair? {
val privateKey = keyStore.getKey(alias, null) as PrivateKey?
val publicKey = keyStore.getCertificate(alias)?.publicKey
return if (privateKey != null && publicKey != null) {
KeyPair(publicKey, privateKey)
} else {
null
}
}
fun createAndroidKeyStoreAsymmetricKey(alias: String): KeyPair {
val generator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore")
if (SystemServices.hasMarshmallow()) {
initGeneratorWithKeyGenParameterSpec(generator, alias)
} else {
initGeneratorWithKeyPairGeneratorSpec(generator, alias)
}
// Generates Key with given spec and saves it to the KeyStore
private fun initGeneratorWithKeyPairGeneratorSpec(generator: KeyPairGenerator, alias: String) {
val startDate = Calendar.getInstance()
val endDate = Calendar.getInstance()
endDate.add(Calendar.YEAR, 20)
val builder = KeyPairGeneratorSpec.Builder(context)
.setAlias(alias)
.setSerialNumber(BigInteger.ONE)
.setSubject(X500Principal("CN=${alias} CA Certificate"))
.setStartDate(startDate.time)
@TargetApi(Build.VERSION_CODES.M)
private fun initGeneratorWithKeyGenParameterSpec(generator: KeyPairGenerator, alias: String) {
val builder = KeyGenParameterSpec.Builder(alias, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_ECB)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
generator.initialize(builder.build())
}
private val keyStore: KeyStore = createAndroidKeyStore()
private fun createAndroidKeyStore(): KeyStore {
val keyStore = KeyStore.getInstance("AndroidKeyStore")
keyStore.load(null)
return keyStore
}
private var deviceSecurityAlert: AlertDialog? = null
override fun onStart() {
super.onStart()
if (!systemServices.isDeviceSecure()) {
deviceSecurityAlert = systemServices.showDeviceSecurityAlert()
}
}
private val keyguardManager: KeyguardManager
init {
keyguardManager = context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
}
fun isDeviceSecure(): Boolean = if (hasMarshmallow()) keyguardManager.isDeviceSecure else keyguardManager.isKeyguardSecure
// Used to block application if no lock screen is setup.
fun showDeviceSecurityAlert(): AlertDialog {
if (BuildConfig.DEBUG) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectCustomSlowCalls()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
StrictMode.VmPolicy.Builder vmPolicy = new StrictMode.VmPolicy.Builder()
@yakivmospan
yakivmospan / readme.md
Created January 17, 2017 23:54 — forked from coolaj86/how-to-publish-to-npm.md
How to publish packages to NPM

Getting Started with NPM (as a developer)

If you haven't already set your NPM author info, now you should:

npm set init.author.name "Your Name"
npm set init.author.email "you@example.com"
npm set init.author.url "http://yourblog.com"

npm adduser