I'm having some problems to access an encrypted Realm DB. This is my flow: - iOS team create the encrypted DB - they created the key - then I receive the key in a API response to avoid store the key locally.
When receive the key I convert it to SHA-512 with the following method:
fun String.getSHA512Pass(): String {
return MessageDigest
.getInstance("SHA-512")
.digest(this.toByteArray())
.fold("", { str, it -> str + "%02x".format(it) })}
And then I just Apply it to my RealmConfiguration:
class AppDatabase {
companion object {
fun init(context: Context) {
Realm.init(context)
val hasKey = "xxxxx-key_from_server-xxxxxx".getSHA512Pass()
try {
val config = RealmConfiguration.Builder()
.assetFile("org.realm")
.encryptionKey(hasKey.toByteArray())
.schemaVersion(1)
.readOnly()
.build()
Realm.setDefaultConfiguration(config)
} catch (exception: Exception) {
Timber.e(exception)
}
}
}}
It crash with this exception:
java.lang.IllegalArgumentException: The provided key must be 64 bytes. Yours was: 128
I tried implementing Base64,BigInteger(hasKey, 16).toByteArray(), and some other approaches but they didn't give me the required length. and I don't know if that will work.
Any suggestion will be appreciated.
Found the solution for the problem here: https://github.com/realm/realm-java/issues/2835
I just apply this to my Hex and gives me the require length:
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.