string_encryption 2.0.5
string_encryption: ^2.0.5 copied to clipboard
Cross-platform string encryption using common best-practices.
import 'package:flutter/material.dart';
import 'package:string_encryption/string_encryption.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _randomKey = 'Unknown';
String _string = "Unknown";
String _encrypted = "Unknown";
@override
initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
initPlatformState() async {
final cryptor = StringEncryption();
final key = await cryptor.generateRandomKey();
// ignore: avoid_print
print("randomKey: $key");
const string = "here is the string, here is the string.";
final encrypted = await cryptor.encrypt(string, key!);
final decrypted = await cryptor.decrypt(encrypted!, key);
assert(decrypted == string);
const wrongKey = 'jIkj0VOLhFpOJSpI7SibjA==:RZ03+kGZ/9Di3PT0a3xUDibD6gmb2RIhTVF+mQfZqy0=';
try {
await cryptor.decrypt(encrypted, wrongKey);
} on MacMismatchException {
// ignore: avoid_print
print('wrongly decrypted');
}
const salt = "Ee/aHwc6EfEactQ00sm/0A=="; // await cryptor.generateSalt();
const password = "a_strong_password%./😋";
final generatedKey = await cryptor.generateKeyFromPassword(password, salt);
// ignore: avoid_print
print("salt: $salt, key: $generatedKey");
assert(generatedKey == wrongKey);
setState(() {
_randomKey = key;
_string = string;
_encrypted = encrypted;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text(
'Random key: $_randomKey\n\nString: $_string\n\nEncrypted: $_encrypted'),
),
),
);
}
}