gt_secure 1.0.0 copy "gt_secure: ^1.0.0" to clipboard
gt_secure: ^1.0.0 copied to clipboard

A production-ready Flutter package for secure local storage with AES-256-CBC encryption, automatic key management, in-memory caching, and comprehensive error handling. Perfect for storing sensitive da [...]

GT Secure #

pub package License: MIT Flutter

A production-ready Flutter package for secure local storage with AES-256-CBC encryption. Perfect for storing sensitive data like authentication tokens, user credentials, and encrypted preferences.

✨ Features #

  • πŸ” AES-256-CBC Encryption - Military-grade encryption with unique IVs per encryption
  • πŸ”‘ Automatic Key Management - Secure key generation, storage, and rotation support
  • πŸ“¦ Type-Safe Storage - Store String, int, bool, double, Map, List, and custom objects
  • ⚑ In-Memory Caching - LRU cache for frequently accessed data
  • πŸ”’ Thread-Safe Operations - Lock mechanism for concurrent access protection
  • πŸ”„ App Reinstall Detection - Automatic cleanup of stale data after reinstall
  • πŸ›‘οΈ Comprehensive Error Handling - Custom exceptions with detailed error messages
  • πŸ“Š Batch Operations - Efficient bulk read/write for better performance
  • πŸ’Ύ Backup & Restore - Export and import encrypted data
  • πŸ” Key Rotation - Periodically rotate encryption keys for enhanced security

πŸ“± Platform Support #

Platform Support
Android βœ… (EncryptedSharedPreferences)
iOS βœ… (Keychain)
macOS βœ…
Linux βœ…
Windows βœ…
Web ⚠️ (Limited - uses localStorage)

πŸš€ Getting Started #

Installation #

Add to your pubspec.yaml:

dependencies:
  gt_secure: ^1.0.0

Then run:

flutter pub get

Basic Usage #

import 'package:gt_secure/gt_secure.dart';

// Initialize once at app startup
await secureStorage.init();

// Store values
await secureStorage.setString('username', 'john_doe');
await secureStorage.setInt('userId', 12345);
await secureStorage.setBool('isLoggedIn', true);
await secureStorage.setDouble('balance', 1234.56);

// Retrieve values
final username = await secureStorage.getString('username');
final userId = await secureStorage.getInt('userId');
final isLoggedIn = await secureStorage.getBool('isLoggedIn');
final balance = await secureStorage.getDouble('balance');

// Remove values
await secureStorage.remove('userId');

// Clear all data
await secureStorage.clearAll();

πŸ“– API Reference #

Initialization #

// Initialize storage (required before any operation)
await secureStorage.init();

Store & Retrieve Complex Objects #

// Store a Map
await secureStorage.setMap('userSettings', {
  'theme': 'dark',
  'notifications': true,
  'language': 'en',
});

// Retrieve a Map
final settings = await secureStorage.getMap('userSettings');

// Store a List
await secureStorage.setList('recentSearches', ['flutter', 'dart']);

// Retrieve a List
final searches = await secureStorage.getList('recentSearches');

// Store custom objects
await secureStorage.setObject('user', user.toJson());

// Retrieve custom objects
final user = await secureStorage.getObject<User>(
  'user',
  (json) => User.fromJson(json),
);

Batch Operations #

// Write multiple values at once
await secureStorage.batchWrite({
  'key1': 'value1',
  'key2': 123,
  'key3': true,
});

// Read multiple values at once
final values = await secureStorage.batchRead(['key1', 'key2', 'key3']);

Utility Methods #

// Check if key exists
final exists = await secureStorage.containsKey('authToken');

// Get all stored keys
final allKeys = await secureStorage.getAllKeys();

// Remove multiple keys
await secureStorage.removeAll(['key1', 'key2']);

// Get storage statistics
final stats = await secureStorage.getStorageStats();
// Returns: {totalKeys, totalSizeBytes, cacheSize, version, initialized}

Advanced Features #

// Export data for backup
final backup = await secureStorage.exportData();

// Import data from backup
await secureStorage.importData(backup);

// Rotate encryption key (for enhanced security)
await secureStorage.rotateEncryptionKey();

// Validate data integrity
final isValid = await secureStorage.validateKey('myKey');
final allValid = await secureStorage.validateAllData();

// Reset entire storage (including encryption key)
await secureStorage.resetStorage();

Error Handling #

try {
  await secureStorage.setString('key', 'value');
} on SecureStorageException catch (e) {
  print('Storage error: ${e.message}');
  if (e.originalError != null) {
    print('Caused by: ${e.originalError}');
  }
}

πŸ” Security Best Practices #

  1. Initialize Early: Call init() at app startup before any storage operations
  2. Handle Errors: Always wrap operations in try-catch blocks
  3. Key Rotation: Consider rotating encryption keys periodically for sensitive apps
  4. Don't Store Raw Passwords: Store hashed or tokenized credentials only
  5. Clear on Logout: Use clearAll() when user logs out

πŸ“‹ Example #

See the example folder for a complete working demo application.

import 'package:flutter/material.dart';
import 'package:gt_secure/gt_secure.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await secureStorage.init();
  runApp(const MyApp());
}

🀝 Contributing #

Contributions are welcome! Please read our contributing guidelines before submitting a pull request.

πŸ“„ License #

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments #

0
likes
150
points
144
downloads

Publisher

unverified uploader

Weekly Downloads

A production-ready Flutter package for secure local storage with AES-256-CBC encryption, automatic key management, in-memory caching, and comprehensive error handling. Perfect for storing sensitive data like authentication tokens, user credentials, and encrypted preferences.

Repository (GitHub)
View/report issues

Topics

#secure-storage #encryption #security #storage #flutter

Documentation

API reference

License

MIT (license)

Dependencies

encrypt, flutter, flutter_secure_storage, shared_preferences

More

Packages that depend on gt_secure