Skip to content

Latest commit

 

History

History
90 lines (57 loc) · 2.3 KB

README.md

File metadata and controls

90 lines (57 loc) · 2.3 KB

HypersignWalletCoreKotlin

This repository will dictate the best practises in terms of security for implementing Hypersign Mobile Wallet

Using android Keystore

Importing keystore

import java.security.KeyStore
import javax.crypto.Cipher
import javax.crypto.KeyGenerator

Create Instance of keystore

val KeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,"AndroidKeyStore")

val KeyGenParameterSpec = KeyGenParameterSpec.Builder("HypersignKeyAllias",
        KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
        .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
        .build()

KeyGenerator.init(KeyGenParameterSpec)
KeyGenerator.generateKey()

GetKey string using keystore instance

fun getKey(): SecretKey {
        val keystore = KeyStore.getInstance("AndroidKeyStore")
        keystore.load(null)

        val secretKeyEntry = keystore.getEntry("HypersignKeyAllias", null) as KeyStore.SecretKeyEntry
        return secretKeyEntry.secretKey
}

Encrypt string using keystore instance

fun encryptData(data: String): Pair<ByteArray, ByteArray> {
    val cipher = Cipher.getInstance("AES/CBC/NoPadding")

    var temp = data
    while (temp.toByteArray().size % 16 != 0)
        temp += "\u0020"

    cipher.init(Cipher.ENCRYPT_MODE, getKey())

    val ivBytes = cipher.iv
    val encryptedBytes = cipher.doFinal(temp.toByteArray(Charsets.UTF_8))

    return Pair(ivBytes, encryptedBytes)
}

DeCrypt string using keystore instance

fun decryptData(ivBytes: ByteArray, data: ByteArray): String{
        val cipher = Cipher.getInstance("AES/CBC/NoPadding")
        val spec = IvParameterSpec(ivBytes)

        cipher.init(Cipher.DECRYPT_MODE, getKey(), spec)
        return cipher.doFinal(data).toString(Charsets.UTF_8).trim()
    }

Reference