搜索插件、代码仓与 APP 分类

GitHub 代码仓xaldarof/encrypted-shared-preferences

加密共享偏好

非常适合注重安全性的开发者,加密共享偏好设置可轻松集成到您的项目中,让您安心并提升数据隐私。使用这一必备的加密解决方案来保护您应用的数据

加密共享偏好 项目封面
星标
22
复刻
11
最近推送(UTC)
2026年3月12日
项目状态
未归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

Shared preferences are a simple key-value storage mechanism that can be used to store small amounts of data. However, since shared preferences are stored in plain text, it is not suitable for storing sensitive information. To address this issue, you can use AES encryption to encrypt the data before saving it to shared preferences.

To use shared preferences with AES encryption support, you can add this package to your project as dependency.

Open pub.dev

Legacy EncryptedSharedPreferences

void main() async {
  final key = "";
  await EncryptedSharedPreferences.initialize(key: key);
  var sharedPref = EncryptedSharedPreferences.getInstance();

  await sharedPref.setString(
      'user_token', 'xxxxxxxxxxxx', notify: true); ////notify = true by default

  sharedPref.getString('user_token'); //xxxxxxxxxxxx

  await sharedPref.setInt('age', 99, notify: true); //notify = true by default

  sharedPref.getInt('age', defaultValue: 1001); //99

  await sharedPref.setDouble('pi', 3.14, notify: true); //notify = true by default

  sharedPref.getDouble('pi'); //3.14

  await sharedPref.setBool('isPremium', true, notify: true); //notify = true by default

  sharedPref.getBool('isPremium'); //true

  await sharedPref.setStringList("stringList", ["apple", "orange", "boom"]);

  sharedPref.getStringList("stringList");

  await sharedPref.remove('user_token', notify: true); //notify = true by default

  await sharedPref.clear(notify: true); //notify = true by default

  await sharedPref.reload();

  final badKeys = {
    "key1"
        "key2"
  };
  await sharedPref.removeWhere((key, value) => badKeys.contains(key));

  sharedPref.observe(key: 'token').listen((event) {
    // event = key
    print(event);
  });

  sharedPref.observeSet(keys: {'key1', 'key2', 'keyN'}).listen((event) {
    // event = key
    print(event);
  });
}

EncryptedSharedPreferencesAsync

void main() async {
  final key = "";
  await EncryptedSharedPreferencesAsync.initialize(key: key);
  var sharedPref = EncryptedSharedPreferencesAsync.getInstance();

  await sharedPref.setString(
      'user_token', 'xxxxxxxxxxxx', notify: true); ////notify = true by default

  await sharedPref.getString('user_token'); //xxxxxxxxxxxx

  await sharedPref.setInt('age', 99, notify: true); //notify = true by default

  await sharedPref.getInt('age', defaultValue: 1001); //99

  await sharedPref.setDouble('pi', 3.14, notify: true); //notify = true by default

  await sharedPref.getDouble('pi'); //3.14

  await sharedPref.setBool('isPremium', true, notify: true); //notify = true by default

  await sharedPref.getBool('isPremium'); //true

  await sharedPref.setStringList("stringList", ["apple", "orange", "boom"]);

  await sharedPref.getStringList("stringList");

  await sharedPref.remove('user_token', notify: true); //notify = true by default

  await sharedPref.clear(notify: true); //notify = true by default

  await sharedPref.reload();

  final badKeys = {
    "key1"
        "key2"
  };
  await sharedPref.removeWhere((key, value) =>
      badKeys.contains(key));

  sharedPref.observe(key: 'token').listen((event) {
    // event = key
    print(event);
  });

  sharedPref.observeSet(keys: {'key1', 'key2', 'keyN'}).listen((event) {
    // event = key
    print(event);
  });
}

[Shared builder] Here is example of how to use SharedBuilder widget

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SharedBuilder(
          listenKeys: const {"key1", "key2"}, //Optional
          builder: (EncryptedSharedPreferences encryptedSharedPreferences, String? updatedKey) {
            return Text("value : ${encryptedSharedPreferences.getString("key1")}");
          },
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            EncryptedSharedPreferences.getInstance()
                .setString('key1', 'dataValue');
            Future.delayed(const Duration(seconds: 3), () {
              EncryptedSharedPreferences.getInstance()
                  .setString('key2', 'dataValue');
            });
          },
        ),
      ),
    );
  }
}

[Shared builder async] Here is example of how to use SharedBuilder widget

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SharedBuilderAsync(
          listenKeys: const {"key1", "key2"}, //Optional
          builder: (EncryptedSharedPreferencesAsync encryptedSharedPreferences,
              String? updatedKey) {
            return Text("value : $updatedKey");
          },
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            EncryptedSharedPreferencesAsync.getInstance()
                .setString('key1', 'dataValue');
            Future.delayed(const Duration(seconds: 3), () {
              EncryptedSharedPreferencesAsync.getInstance()
                  .setString('key2', 'dataValue');
            });
          },
        ),
      ),
    );
  }
}

Also you can add custom external encryptor

class CustomEncryptor extends IEncryptor {
  @override
  String decrypt(String key, String encryptedData) {
    //decryption logic
  }

  @override
  String encrypt(String key, String plainText) {
    //encryption logic
  }
}

void main() {
  final key = "";
  await EncryptedSharedPreferences.initialize(key: key, encryptor: CustomEncryptor());
  var sharedPref = EncryptedSharedPreferences.getInstance();
}

DevTools extension for inspecting values

alt text

xaldarof GitHub avatar
GitHub 作者

xaldarof ↗

bedroom
语言DartC++CMakeHTMLRubySwiftCKotlinShellObjective-C

分类

此仓库发布的插件

使用的依赖

依赖清单 6 项

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗