getEncryptionKey function

Future<Uint8List> getEncryptionKey()

Implementation

Future<Uint8List> getEncryptionKey() async {
  const secureStorage = FlutterSecureStorage();

  // Check if an encryption key already exists
  final existingKey = await secureStorage.read(key: 'hiveEncryptionKey');

  if (existingKey != null) {
    // Return the existing encryption key
    return base64Url.decode(existingKey);
  } else {
    // Generate a new encryption key and store it securely
    final key =
        sha256
            .convert(utf8.encode('my_super_secure_key'))
            .bytes; // Use a secure way to create the key
    await secureStorage.write(
      key: 'hiveEncryptionKey',
      value: base64Url.encode(key),
    );
    return Uint8List.fromList(key);
  }
}