This file contains 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
/** | |
* Container for everything needed for decrypting the database. | |
* | |
* @param iv initialization vector | |
* @param key encrypted database key | |
* @param salt cryptographic salt | |
*/ | |
data class Storable(val iv: String, val key: String, val salt: String) |
This file contains 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 rawByteKey: ByteArray | |
private lateinit var dbCharKey: CharArray | |
/** | |
* Generates a new database key. | |
*/ | |
fun createNewKey() { | |
// This is the raw key that we'll be encrypting + storing | |
rawByteKey = generateRandomKey() | |
// This is the key that will be used by Room |
This file contains 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 val HEX_CHARS = "0123456789ABCDEF".toCharArray() | |
/** | |
* Extension function that converts a ByteArray to a hex encoded CharArray. | |
*/ | |
fun ByteArray.toHex(): CharArray { | |
val result = StringBuilder() | |
forEach { | |
val octet = it.toInt() | |
val firstIndex = (octet and 0xF0).ushr(4) |
This file contains 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
/** | |
* Generates a random 32 byte key. | |
* | |
* @return a byte array containing random values | |
*/ | |
fun generateRandomKey(): ByteArray = | |
ByteArray(32).apply { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
SecureRandom.getInstanceStrong().nextBytes(this) | |
} else { |
This file contains 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
/* | |
* Copyright (c) 2016 AirWatch, LLC. All rights reserved. | |
* This product is protected by copyright and intellectual property laws in | |
* the United States and other countries as well as by international treaties. | |
* AirWatch products may be covered by one or more patents listed at | |
* http://www.vmware.com/go/patents. | |
*/ | |
package com.boxer.sdk; | |
import android.content.Context; |
NewerOlder