Pin code: add some tests
This commit is contained in:
parent
8436806571
commit
d6d553e8e0
14 changed files with 371 additions and 45 deletions
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (c) 2023 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.cryptography.api
|
||||
|
||||
import android.security.keystore.KeyProperties
|
||||
|
||||
object AESEncryptionSpecs {
|
||||
const val BLOCK_MODE = KeyProperties.BLOCK_MODE_GCM
|
||||
const val PADDINGS = KeyProperties.ENCRYPTION_PADDING_NONE
|
||||
const val ALGORITHM = KeyProperties.KEY_ALGORITHM_AES
|
||||
const val KEY_SIZE = 128
|
||||
const val CIPHER_TRANSFORMATION = "$ALGORITHM/$BLOCK_MODE/$PADDINGS"
|
||||
}
|
||||
|
|
@ -20,10 +20,9 @@ import javax.crypto.Cipher
|
|||
import javax.crypto.SecretKey
|
||||
|
||||
/**
|
||||
* Simple service to provide cryptographic operations.
|
||||
* Simple service to provide encryption and decryption operations.
|
||||
*/
|
||||
interface CryptoService {
|
||||
fun getOrCreateSecretKey(alias: String): SecretKey
|
||||
interface EncryptionDecryptionService {
|
||||
fun createEncryptionCipher(key: SecretKey): Cipher
|
||||
fun createDecryptionCipher(key: SecretKey, initializationVector: ByteArray): Cipher
|
||||
fun encrypt(key: SecretKey, input: ByteArray): EncryptionResult
|
||||
|
|
@ -14,10 +14,13 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
@file:OptIn(ExperimentalEncodingApi::class)
|
||||
|
||||
package io.element.android.libraries.cryptography.api
|
||||
|
||||
import android.util.Base64
|
||||
import java.nio.ByteBuffer
|
||||
import kotlin.io.encoding.Base64
|
||||
import kotlin.io.encoding.ExperimentalEncodingApi
|
||||
|
||||
/**
|
||||
* Holds the result of an encryption operation.
|
||||
|
|
@ -34,7 +37,7 @@ class EncryptionResult(
|
|||
.put(initializationVector)
|
||||
.put(encryptedByteArray)
|
||||
.array()
|
||||
return Base64.encodeToString(cipherTextWithIv, Base64.NO_WRAP)
|
||||
return Base64.encode(cipherTextWithIv)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
|
@ -43,7 +46,7 @@ class EncryptionResult(
|
|||
* @return the [EncryptionResult] from the base64 representation.
|
||||
*/
|
||||
fun fromBase64(base64: String): EncryptionResult {
|
||||
val cipherTextWithIv = Base64.decode(base64, Base64.NO_WRAP)
|
||||
val cipherTextWithIv = Base64.decode(base64)
|
||||
val buffer = ByteBuffer.wrap(cipherTextWithIv)
|
||||
val initializationVectorSize = buffer.int
|
||||
val initializationVector = ByteArray(initializationVectorSize)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (c) 2023 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.cryptography.api
|
||||
|
||||
import javax.crypto.SecretKey
|
||||
|
||||
/**
|
||||
* Simple interface to get or create a secret key for a given alias.
|
||||
* Implementation should be able to store the generated key securely.
|
||||
*/
|
||||
interface SecretKeyProvider {
|
||||
fun getOrCreateKey(alias: String): SecretKey
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue