我正在使用Realm作为本地数据库来开发移动应用程序,我试图加密文件中的数据,但是要处理很多。
我想知道我是否可以加密Realm数据库文件?如果可能的话,怎么办?。
谢谢。
答案 0 :(得分:0)
尝试一下:
// Generate a random encryption key
var key = Data(count: 64)
_ = key.withUnsafeMutableBytes { bytes in
SecRandomCopyBytes(kSecRandomDefault, 64, bytes)
}
// Open the encrypted Realm file
let config = Realm.Configuration(encryptionKey: key)
do {
let realm = try Realm(configuration: config)
// Use the Realm as normal
let dogs = realm.objects(Dog.self).filter("name contains 'Fido'")
} catch let error as NSError {
// If the encryption key is wrong, `error` will say that it's an invalid database
fatalError("Error opening realm: \(error)")
}