Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support encryption key in configuration #920

Merged
merged 16 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/src/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ abstract class Configuration implements Finalizable {
this.fifoFilesFallbackPath,
this.encryptionKey,
}) {
if (encryptionKey != null && encryptionKey!.length != realmCore.encryptionKeySize) {
throw RealmException("EncryptionKey must be 64 bytes");
if (encryptionKey != null && encryptionKey!.isNotEmpty && encryptionKey?.length != realmCore.encryptionKeySize) {
throw RealmException("Wrong encryption key size (must be 0 or ${realmCore.encryptionKeySize})");
desistefanova marked this conversation as resolved.
Show resolved Hide resolved
}

this.path = path ?? _path.join(_path.dirname(_defaultPath), _path.basename(defaultRealmName));
}

Expand Down
9 changes: 7 additions & 2 deletions test/configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,12 @@ Future<void> main([List<String>? args]) async {

test('Configuration set short encryption key', () {
desistefanova marked this conversation as resolved.
Show resolved Hide resolved
List<int> key = [1, 2, 3];
expect(() => Configuration.local([Car.schema], encryptionKey: key), throws<RealmException>("EncryptionKey must be 64 bytes"));
expect(() => Configuration.local([Car.schema], encryptionKey: key), throws<RealmException>("Wrong encryption key size (must be 0 or $encryptionKeySize)"));
});

test('Configuration set byte exceeding encryption key', () {
List<int> byteExceedingKey = List<int>.generate(encryptionKeySize, (i) => random.nextInt(4294967296));
Configuration.local([Car.schema], encryptionKey: byteExceedingKey);
});

test('Configuration set a correct encryption key', () {
Expand All @@ -549,7 +554,7 @@ Future<void> main([List<String>? args]) async {
List<int> key = List<int>.generate(encryptionKeySize + 10, (i) => random.nextInt(256));
expect(
() => Configuration.flexibleSync(user, [Task.schema], encryptionKey: key),
throws<RealmException>("EncryptionKey must be 64 bytes"),
throws<RealmException>("Wrong encryption key size (must be 0 or $encryptionKeySize)"),
);
});
}
6 changes: 6 additions & 0 deletions test/realm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,11 @@ Future<void> main([List<String>? args]) async {
openEncryptedRealm(generateValidKey(), generateValidKey(), afterEncrypt: (realm) => realm.close());
});

test('Realm - open local realm with key with values exceeding byte size 255', () {
List<int> byteExceedingKey = List<int>.generate(encryptionKeySize, (i) => random.nextInt(4294967296));
openEncryptedRealm(byteExceedingKey, byteExceedingKey);
});

baasTest('Realm - open remote encrypted realm with encryption key', (appConfiguration) async {
final app = App(appConfiguration);
final credentials = Credentials.anonymous();
Expand Down Expand Up @@ -880,6 +885,7 @@ void openEncryptedRealm(List<int>? encryptionKey, List<int>? decryptionKey, {voi
}
if (encryptionKey == decryptionKey) {
final decriptedRealm = getRealm(config2);
expect(decriptedRealm.isClosed, false);
} else {
expect(
() => getRealm(config2),
Expand Down