在iOS上,Realm是默认加密的

use*_*242 3 encryption realm ios

在iOS上,我无法确定Realm是否默认加密.有人可以帮我理解吗?

mar*_*ius 9

从Realm Documentation部分关于使用Realm和后台应用程序刷新:

在iOS 8及更高版本中,只要设备被锁定,应用程序内的文件就会使用NSFileProtection自动加密.如果您的应用程序在设备被锁定时尝试执行涉及Realm的任何工作,并且NSFileProtection您的Realm文件的属性设置为加密它们(默认情况下就是这种情况),open() failed: Operation not permitted则会引发异常.

除此之外,Realm还提供自己的本机文件加密支持.

Realm支持在创建Realm时通过提供64字节加密密钥,使用AES-256 + SHA2加密磁盘上的数据库文件.

// Generate a random encryption key
let key = NSMutableData(length: 64)!
SecRandomCopyBytes(kSecRandomDefault, key.length,
    UnsafeMutablePointer<UInt8>(key.mutableBytes))

// 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).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)")
}
Run Code Online (Sandbox Code Playgroud)

这使得存储在磁盘上的所有数据都可以根据需要使用AES-256进行透明加密和解密,并使用SHA-2 HMAC进行验证.每次获得Realm实例时都必须提供相同的加密密钥.