Skip to content

Instantly share code, notes, and snippets.

@Hayk985
Hayk985 / CipherManager.kt
Last active April 16, 2024 11:49
Android KeyStore API Tutorial
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import android.util.Base64
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
@Hayk985
Hayk985 / PBESpec.kt
Last active February 28, 2024 15:42
Password based encryption
import android.security.keystore.KeyProperties
import java.security.SecureRandom
import java.security.spec.KeySpec
import javax.crypto.SecretKey
import javax.crypto.SecretKeyFactory
import javax.crypto.spec.PBEKeySpec
import javax.crypto.spec.SecretKeySpec
// Source - https://medium.com/@hayk.mkrtchyan8998/shedding-light-on-android-encryption-android-crypto-api-part-2-cipher-147ff4411e1d
@Hayk985
Hayk985 / KeyWrapper.kt
Last active February 28, 2024 15:42
Key Wrapping example
import android.security.keystore.KeyProperties
import android.util.Base64
import java.security.Key
import java.security.SecureRandom
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
import kotlin.jvm.Throws
// Source - https://medium.com/@hayk.mkrtchyan8998/shedding-light-on-android-encryption-android-crypto-api-part-2-cipher-147ff4411e1d
@Hayk985
Hayk985 / CryptoUtils.kt
Last active March 1, 2024 17:34
CryptoUtils - Provides utility functions for encryption/decryption
import android.security.keystore.KeyProperties
import android.util.Base64
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
@Hayk985
Hayk985 / MD5Generator.kt
Last active February 20, 2024 19:02
MD5 Generator
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
// Source - https://medium.com/@hayk.mkrtchyan8998/shedding-light-on-android-encryption-android-crypto-api-part-1-introduction-9623ca992dc2
// Our algorithm name. As there's no a predefined constant, we'll make our own :)
private const val MD5_ALGORITHM = "MD5"
// A String extension to generate a hash value
@Throws(NoSuchAlgorithmException::class, NullPointerException::class)
import android.content.Context
import android.graphics.Point
import android.os.Build
import android.view.WindowManager
import kotlin.math.pow
import kotlin.math.sqrt
/**
* An extension function to return current device real width and height
*/