搜尋套件、儲存庫與 APP 分類

套件詳情v1.0.1

encrypt_shared_preferences

此套件為儲存在您應用程式中的敏感資訊(如使用者憑證、API 金鑰或其他機密資料)增加了額外的保護層。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1.8萬
喜歡數
49
Pub 分數
130

安裝套件

flutter pub add encrypt_shared_preferences

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

encrypt_shared_preferences 套件透過加密使用者憑證與 API 金鑰等敏感資訊,增強了儲存在 SharedPreferences 中資料的安全性。它能無縫整合至跨多個平台的 Flutter 應用程式中,提供額外的保護層以防止未經授權的存取。透過利用加密技術,即使裝置遭入侵,也能確保機密資料的安全。此套件非常適合處理私人使用者資料的應用程式,且設定簡單,容易實作。

適用情境

  • 保護使用者登入權杖
  • 安全儲存API金鑰
  • 保護個人用戶數據
  • 加密設定設定
  • 防止在root設備上發生資料洩漏
  • 增強應用安全合規性

主要特色

  • 端對端加密
  • 與 SharedPreferences 的輕鬆整合
  • 跨平台支援
  • 安全金鑰管理
  • 最小的樣板程式碼
  • 支援所有主要的Flutter平台

適合對象

  • 處理敏感用戶數據的應用程式
  • 金融或醫療應用
  • 需要資料隱私的應用程式
  • 開發人員優先考慮安全
  • 使用 SharedPreferences 的專案已存在
  • 遵循安全最佳實踐的團隊

使用建議

  • 需要適當的金鑰儲存策略
  • 可能略微影響效能
  • 需要仔細處理加密金鑰
  • 不是伺服器端安全的替代品
  • 依賴於底層 SharedPreferences 的可靠性
  • 調試加密資料的額外複雜性

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

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

所屬分類

依賴

依賴清單 3 項

原始碼儲存庫

使用此套件的專案