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

[Web] Issue with "Invalid argument(s): Invalid or corrupted pad block" in Flutter #1272

Open
BokDevTeam opened this issue Jan 30, 2024 · 3 comments

Comments

@BokDevTeam
Copy link

When using Hive in the web version, I encounter an issue in the following scenario.

The problem arises when the security key changes in the middle of using secureStorage, resulting in the error "Invalid argument(s): Invalid or corrupted pad block."

The issue occurs when calling Hive.deleteBoxFromDisk or any other Hive function during this situation. The code gets locked, and there is no further response.

I attempted to resolve the problem by using try-catch, intending to call Hive.deleteBoxFromDisk to handle the issue and then reload the webpage. However, I face an issue where the code freezes when attempting to call Hive.deleteBoxFromDisk, preventing the problem from being resolved. Refreshing the page resolves the issue, and it starts functioning normally again.

To reproduce the issue, follow these steps after running the code once. Uncomment either "test key 1" or "test key 2" to alternate between the security keys.

Please note that when running the code in Flutter, make sure to use the --web-port 3000 option to fix the port. This is essential to maintain the same repository and reproduce the problem.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  Log.initialize();

  await Hive.initFlutter();
  // await PersistentData().initialize();

  const secureStorage = FlutterSecureStorage(
    aOptions: AndroidOptions(
      encryptedSharedPreferences: true,
    ),
  );

  // if key not exists return null
  final encryptionKeyString = await secureStorage.read(key: 'secureKey');
  if (encryptionKeyString == null) {
    final key = Hive.generateSecureKey();
    await secureStorage.write(
      key: 'secureKey',
      value: base64UrlEncode(key),
    );
  }

  try {
    // test key 1
    // await secureStorage.write(
    //   key: 'secureKey',
    //   value: 'R9LRQSXERnYA6at85jsf4zpJCBE3HcKfGeoKbSCs-U0=',
    // );

    // test key 2
    // await secureStorage.write(
    //   key: 'secureKey',
    //   value: 'Pe-J9mcSNI-MHcCxt6MxqH8kAt8GD-UN0HKNIrpsnJs=',
    // );

    final key = await secureStorage.read(key: 'secureKey');
    print('Encryption key: $key');
    final encryptionKeyUint8List = base64Url.decode(key!);
    print('Encryption key Uint8List: $encryptionKeyUint8List');

    final encryptedBox = await Hive.openBox('vaultBox', encryptionCipher: HiveAesCipher(encryptionKeyUint8List));

    print(encryptedBox.get('secret'));
    encryptedBox.put('secret', 'Hive is cool');
    print(encryptedBox.get('secret'));
  } catch (e) {
    print(e);

    try {
      await Hive.deleteBoxFromDisk('vaultBox');
    } catch (e) {
      print(e);
    }
    print('--- delete vaultBox ---');
  }

  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }
}
@hpelitebook745G2
Copy link

any updates?

@bombo774
Copy link

Getting the same error

@valtbarbos
Copy link

Same error here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants