From 7c76ba8184430f52f8e06333dc8eaa1a8d8161e9 Mon Sep 17 00:00:00 2001 From: ganfra Date: Wed, 25 May 2022 14:45:16 +0200 Subject: [PATCH] Create BackupRecoveryKey wrapper class (to avoid directly expose uniffi generated classes) --- .../crypto/keysbackup/BackupRecoveryKey.kt | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/keysbackup/BackupRecoveryKey.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/keysbackup/BackupRecoveryKey.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/keysbackup/BackupRecoveryKey.kt new file mode 100644 index 0000000000..87a4c933b2 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/keysbackup/BackupRecoveryKey.kt @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2022 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 org.matrix.android.sdk.api.session.crypto.keysbackup + +import uniffi.olm.BackupRecoveryKey as InnerBackupRecoveryKey + +class BackupRecoveryKey internal constructor(internal val inner: InnerBackupRecoveryKey) { + + constructor() : this(InnerBackupRecoveryKey()) + + companion object { + + fun fromBase58(key: String): BackupRecoveryKey { + val inner = InnerBackupRecoveryKey.fromBase58(key) + return BackupRecoveryKey(inner) + } + + fun fromBase64(key: String): BackupRecoveryKey { + val inner = InnerBackupRecoveryKey.fromBase64(key) + return BackupRecoveryKey(inner) + } + + fun fromPassphrase(passphrase: String, salt: String, rounds: Int): BackupRecoveryKey { + val inner = InnerBackupRecoveryKey.fromPassphrase(passphrase, salt, rounds) + return BackupRecoveryKey(inner) + } + + fun newFromPassphrase(passphrase: String): BackupRecoveryKey { + val inner = InnerBackupRecoveryKey.newFromPassphrase(passphrase) + return BackupRecoveryKey(inner) + } + } + + override fun equals(other: Any?): Boolean { + if (other !is BackupRecoveryKey) return false + return this.toBase58() == other.toBase58() + } + + fun toBase58() = inner.toBase58() + + fun toBase64() = inner.toBase64() + + fun decryptV1(ephemeralKey: String, mac: String, ciphertext: String) = inner.decryptV1(ephemeralKey, mac, ciphertext) + + fun megolmV1PublicKey() = inner.megolmV1PublicKey() +}