4 changed files with 7 additions and 190 deletions
@ -1,144 +0,0 @@ |
|||||
package com.yunqiinnovation.deepsound |
|
||||
|
|
||||
import android.content.Context |
|
||||
import androidx.security.crypto.EncryptedSharedPreferences |
|
||||
import androidx.security.crypto.MasterKey |
|
||||
import com.yunqiinnovation.deepvoice_utils.FileLogger |
|
||||
|
|
||||
/** |
|
||||
* 通用工具类,包含安全存储等功能 |
|
||||
*/ |
|
||||
object Utils { |
|
||||
private const val TAG = "Utils" |
|
||||
private const val SECURE_PREFS_FILENAME = "deep_voice_secure_prefs" |
|
||||
private const val KEY_SESSION_ID = "session_id" |
|
||||
|
|
||||
/** |
|
||||
* 安全存储数据 |
|
||||
* @param context 上下文 |
|
||||
* @param dataMap 要存储的键值对字典 |
|
||||
* @return 是否成功存储 |
|
||||
*/ |
|
||||
fun saveSecureData(context: Context, dataMap: Map<String, String?>): Boolean { |
|
||||
return try { |
|
||||
// 创建或获取主密钥 |
|
||||
val masterKey = MasterKey.Builder(context) |
|
||||
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM) |
|
||||
.build() |
|
||||
|
|
||||
// 创建加密SharedPreferences |
|
||||
val sharedPreferences = EncryptedSharedPreferences.create( |
|
||||
context, |
|
||||
SECURE_PREFS_FILENAME, |
|
||||
masterKey, |
|
||||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, |
|
||||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM |
|
||||
) |
|
||||
|
|
||||
// 获取或生成会话ID |
|
||||
var sessionId = sharedPreferences.getString(KEY_SESSION_ID, "") ?: "" |
|
||||
if (sessionId.isEmpty()) { |
|
||||
sessionId = java.util.UUID.randomUUID().toString() |
|
||||
} |
|
||||
|
|
||||
// 开始编辑会话 |
|
||||
val editor = sharedPreferences.edit() |
|
||||
|
|
||||
// 保存会话ID |
|
||||
editor.putString(KEY_SESSION_ID, sessionId) |
|
||||
|
|
||||
// 保存所有键值对 |
|
||||
for ((key, value) in dataMap) { |
|
||||
if (value != null) { |
|
||||
editor.putString(key, value) |
|
||||
} else { |
|
||||
editor.remove(key) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// 提交更改 |
|
||||
editor.apply() |
|
||||
|
|
||||
FileLogger.d(TAG, "数据已安全保存到加密存储中") |
|
||||
true |
|
||||
} catch (e: Exception) { |
|
||||
FileLogger.e(TAG, "保存数据到加密存储失败", e) |
|
||||
false |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 从安全存储中读取所有数据 |
|
||||
* @param context 上下文 |
|
||||
* @return 包含所有数据的键值对字典 |
|
||||
*/ |
|
||||
fun loadAllSecureData(context: Context): Map<String, String> { |
|
||||
return try { |
|
||||
val resultMap = mutableMapOf<String, String>() |
|
||||
|
|
||||
// 创建或获取主密钥 |
|
||||
val masterKey = MasterKey.Builder(context) |
|
||||
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM) |
|
||||
.build() |
|
||||
|
|
||||
// 创建加密SharedPreferences |
|
||||
val sharedPreferences = EncryptedSharedPreferences.create( |
|
||||
context, |
|
||||
SECURE_PREFS_FILENAME, |
|
||||
masterKey, |
|
||||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, |
|
||||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM |
|
||||
) |
|
||||
|
|
||||
// 读取所有数据 |
|
||||
for ((key, value) in sharedPreferences.all) { |
|
||||
if (value is String) { |
|
||||
resultMap[key] = value |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
FileLogger.d(TAG, "已从加密存储加载所有数据") |
|
||||
resultMap |
|
||||
} catch (e: Exception) { |
|
||||
FileLogger.e(TAG, "从加密存储加载所有数据失败", e) |
|
||||
emptyMap() |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 清除所有安全存储的数据 |
|
||||
* @param context 上下文 |
|
||||
* @return 是否成功清除 |
|
||||
*/ |
|
||||
fun clearAllSecureData(context: Context): Boolean { |
|
||||
return try { |
|
||||
// 创建或获取主密钥 |
|
||||
val masterKey = MasterKey.Builder(context) |
|
||||
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM) |
|
||||
.build() |
|
||||
|
|
||||
// 创建加密SharedPreferences |
|
||||
val sharedPreferences = EncryptedSharedPreferences.create( |
|
||||
context, |
|
||||
SECURE_PREFS_FILENAME, |
|
||||
masterKey, |
|
||||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, |
|
||||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM |
|
||||
) |
|
||||
|
|
||||
// 生成新的会话ID |
|
||||
val sessionId = java.util.UUID.randomUUID().toString() |
|
||||
|
|
||||
// 清除所有数据 |
|
||||
sharedPreferences.edit().clear() |
|
||||
.putString(KEY_SESSION_ID, sessionId) |
|
||||
.apply() |
|
||||
|
|
||||
FileLogger.d(TAG, "已清除所有安全存储的数据") |
|
||||
true |
|
||||
} catch (e: Exception) { |
|
||||
FileLogger.e(TAG, "清除安全存储数据失败", e) |
|
||||
false |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue