readEncryptionCipher method

Future<HiveCipher> readEncryptionCipher()

Reads the encryption cipher from secure storage and returns a HiveCipher object.

The HiveCipher object is used to encrypt and decrypt data in the database.

This method reads the encryption key from secure storage, decodes it from base64Url, and returns a HiveAesCipher object initialized with the encryption key.

If the secure key is null, a DatabaseServiceException is thrown set to 'Secure key is null'.

If an error occurs while reading the secure key a DatabaseServiceException is thrown.

Implementation

Future<HiveCipher> readEncryptionCipher() async {
  try {
    final secureStorageKey = await _secureStorage.read(
      key: _secureKey,
      iOptions: const IOSOptions(
        accessibility: KeychainAccessibility.first_unlock,
      ),
      mOptions: const MacOsOptions(
        accessibility: KeychainAccessibility.first_unlock,
      ),
    );

    if (secureStorageKey != null) {
      final encryptionKey = base64Url.decode(secureStorageKey);
      return HiveAesCipher(encryptionKey);
    } else {
      throw const DatabaseBridgeException(error: 'Secure key is null');
    }
  } catch (e) {
    throw DatabaseBridgeException(error: e.toString());
  }
}