A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/atlas/device-sdks/sdk/swift/realm-files/encrypt-a-realm/ below:

Encrypt a Realm - Swift SDK - Atlas Device SDKs

You can encrypt the realm file on disk with AES-256 + SHA-2 by supplying a 64-byte encryption key when opening a realm.

Realm transparently encrypts and decrypts data with standard AES-256 encryption using the first 256 bits of the given 512-bit encryption key. Realm uses the other 256 bits of the 512-bit encryption key to validate integrity using a hash-based message authentication code (HMAC).

Warning

Do not use cryptographically-weak hashes for realm encryption keys. For optimal security, we recommend generating random rather than derived encryption keys.

Note Cannot Encrypt an Existing Unencrypted Realm

You must encrypt a realm the first time you open it. If you try to open an existing unencrypted realm using a configuration that contains an encryption key, Realm throws an error.

The following are key impacts to consider when encrypting a realm.

You must pass the same encryption key every time you open the encrypted realm. If you don't provide a key or specify the wrong key for an encrypted realm, the Realm SDK throws an error.

Apps should store the encryption key in the Keychain so that other apps cannot read the key.

Reads and writes on encrypted realms can be up to 10% slower than unencrypted realms.

You can encrypt a synced realm.

Realm only encrypts the data on the device and stores the data unencrypted in your Atlas data source. Any users with authorized access to the Atlas data source can read the data, but the following still applies:

You can also enable Customer Key Management to encrypt stored Atlas data using your cloud provider's key (e.g. AWS KMS, Azure Key Vault, Google Cloud KMS).

If you need unique keys for each user of your application, you can use an OAuth provider or use one of the Realm authentication providers and an authentication trigger to create a 64-bit key and store that key in a user object.

Changed in version 10.38.0.

Starting with Realm Swift SDK version 10.38.0, Realm supports opening the same encrypted realm in multiple processes.

If your app uses Realm Swift SDK version 10.37.2 or earlier, attempting to open an encrypted realm from multiple processes throws this error: Encrypted interprocess sharing is currently unsupported.

Apps using earlier SDK versions have two options to work with realms in multiple processes:

One possible tool to encrypt and decrypt fields is Apple's CryptoKit framework . You can use Swift Crypto to simplify app development with CryptoKit.

The following code demonstrates how to generate an encryption key and open an encrypted realm:

NSMutableData *key = [NSMutableData dataWithLength:64];(void)SecRandomCopyBytes(kSecRandomDefault, key.length, (uint8_t *)key.mutableBytes);RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];config.encryptionKey = key;NSError *error = nil;RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:&error];if (!realm) {        NSLog(@"Error opening realm: %@", error);} else {    }
var key = Data(count: 64)_ = key.withUnsafeMutableBytes { (pointer: UnsafeMutableRawBufferPointer) in    SecRandomCopyBytes(kSecRandomDefault, 64, pointer.baseAddress!) }var config = Realm.Configuration(encryptionKey: key)do {        let realm = try Realm(configuration: config)    } catch let error as NSError {        fatalError("Error opening realm: \(error.localizedDescription)")}

The following Swift example demonstrates how to store and retrieve a generated key from the Keychain:

func getKey() -> Data {        let keychainIdentifier = "io.Realm.EncryptionExampleKey"    let keychainIdentifierData = keychainIdentifier.data(using: String.Encoding.utf8, allowLossyConversion: false)!        var query: [NSString: AnyObject] = [        kSecClass: kSecClassKey,        kSecAttrApplicationTag: keychainIdentifierData as AnyObject,        kSecAttrKeySizeInBits: 512 as AnyObject,        kSecReturnData: true as AnyObject    ]            var dataTypeRef: AnyObject?    var status = withUnsafeMutablePointer(to: &dataTypeRef) { SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0)) }    if status == errSecSuccess {                return dataTypeRef as! Data    }            var key = Data(count: 64)    key.withUnsafeMutableBytes({ (pointer: UnsafeMutableRawBufferPointer) in        let result = SecRandomCopyBytes(kSecRandomDefault, 64, pointer.baseAddress!)        assert(result == 0, "Failed to get random bytes")    })        query = [        kSecClass: kSecClassKey,        kSecAttrApplicationTag: keychainIdentifierData as AnyObject,        kSecAttrKeySizeInBits: 512 as AnyObject,        kSecValueData: key as AnyObject    ]    status = SecItemAdd(query as CFDictionary, nil)    assert(status == errSecSuccess, "Failed to insert the new key in the keychain")    return key}var config = Realm.Configuration(encryptionKey: getKey())do {        let realm = try Realm(configuration: config)    } catch let error as NSError {        fatalError("Error opening realm: \(error)")}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4