Fix example with pbkdf2 and exceptions
This commit is contained in:
parent
2a93752a79
commit
0d0b0da7dd
@ -4,8 +4,10 @@ import 'dart:developer';
|
|||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
import 'package:native_crypto/symmetric_crypto.dart';
|
import 'package:native_crypto/symmetric_crypto.dart';
|
||||||
|
import 'package:native_crypto/exceptions.dart';
|
||||||
|
|
||||||
void main() => runApp(MyApp());
|
void main() => runApp(MyApp());
|
||||||
|
|
||||||
@ -16,20 +18,46 @@ class MyApp extends StatefulWidget {
|
|||||||
|
|
||||||
class _MyAppState extends State<MyApp> {
|
class _MyAppState extends State<MyApp> {
|
||||||
final textController = TextEditingController();
|
final textController = TextEditingController();
|
||||||
|
final pwdController = TextEditingController();
|
||||||
|
|
||||||
String _output = 'none';
|
String _output = 'none';
|
||||||
String _bench;
|
String _bench;
|
||||||
|
|
||||||
AES aes = AES();
|
AES aes = AES();
|
||||||
List<Uint8List> encryptedPayload;
|
List<Uint8List> encryptedPayload;
|
||||||
Uint8List decryptedPayload;
|
Uint8List decryptedPayload;
|
||||||
|
Uint8List key;
|
||||||
|
|
||||||
void _generateKey() async {
|
void _generateKey() async {
|
||||||
// You can also generate key before creating aes object.
|
// You can also generate key before creating aes object.
|
||||||
// Uint8List aeskey = await KeyGenerator().secretKey(keySize: KeySize.bits256);
|
// Uint8List aeskey = await KeyGenerator().secretKey(keySize: KeySize.bits256);
|
||||||
// AES aes = AES(key: aeskey);
|
// AES aes = AES(key: aeskey);
|
||||||
|
var output;
|
||||||
|
try {
|
||||||
await aes.init(KeySize.bits256);
|
await aes.init(KeySize.bits256);
|
||||||
|
output = 'Key generated. Length: ${aes.key.length}';
|
||||||
|
} catch (e) {
|
||||||
|
// PlatformException or KeyException, both have message property.
|
||||||
|
output = e.message;
|
||||||
|
}
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_output = 'Key generated. Length: ${aes.key.length}';
|
_output = output;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _pbkdf2() async {
|
||||||
|
final password = pwdController.text.trim();
|
||||||
|
|
||||||
|
var output;
|
||||||
|
if (password.isEmpty) {
|
||||||
|
output = 'Password is empty';
|
||||||
|
} else {
|
||||||
|
key = await KeyGenerator().pbkdf2(password, 'salt');
|
||||||
|
output = 'Key successfully derived.';
|
||||||
|
}
|
||||||
|
setState(() {
|
||||||
|
_output = output;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +71,7 @@ class _MyAppState extends State<MyApp> {
|
|||||||
var stringToBytes = TypeHelper().stringToBytes(plainText);
|
var stringToBytes = TypeHelper().stringToBytes(plainText);
|
||||||
// You can also pass a specific key.
|
// You can also pass a specific key.
|
||||||
// encryptedPayload = await AES().encrypt(stringToBytes, key: aeskey);
|
// encryptedPayload = await AES().encrypt(stringToBytes, key: aeskey);
|
||||||
encryptedPayload = await aes.encrypt(stringToBytes);
|
encryptedPayload = await aes.encrypt(stringToBytes, key: key?? null);
|
||||||
output = 'String successfully encrypted.';
|
output = 'String successfully encrypted.';
|
||||||
}
|
}
|
||||||
setState(() {
|
setState(() {
|
||||||
@ -51,6 +79,20 @@ class _MyAppState extends State<MyApp> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _alter() async {
|
||||||
|
var output;
|
||||||
|
if (encryptedPayload == null || encryptedPayload[0].isEmpty) {
|
||||||
|
output = 'Encrypt before altering payload!';
|
||||||
|
} else {
|
||||||
|
// Shuffle payload.
|
||||||
|
encryptedPayload[0].shuffle();
|
||||||
|
output = 'Payload altered.';
|
||||||
|
}
|
||||||
|
setState(() {
|
||||||
|
_output = output;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void _decrypt() async {
|
void _decrypt() async {
|
||||||
var output;
|
var output;
|
||||||
if (encryptedPayload == null || encryptedPayload[0].isEmpty) {
|
if (encryptedPayload == null || encryptedPayload[0].isEmpty) {
|
||||||
@ -58,9 +100,13 @@ class _MyAppState extends State<MyApp> {
|
|||||||
} else {
|
} else {
|
||||||
// You can also pass a specific key.
|
// You can also pass a specific key.
|
||||||
// decryptedPayload = await AES().decrypt(encryptedPayload, key: aeskey);
|
// decryptedPayload = await AES().decrypt(encryptedPayload, key: aeskey);
|
||||||
decryptedPayload = await aes.decrypt(encryptedPayload);
|
try {
|
||||||
|
decryptedPayload = await aes.decrypt(encryptedPayload, key: key?? null);
|
||||||
var bytesToString = TypeHelper().bytesToString(decryptedPayload);
|
var bytesToString = TypeHelper().bytesToString(decryptedPayload);
|
||||||
output = 'String successfully decrypted:\n\n$bytesToString';
|
output = 'String successfully decrypted:\n\n$bytesToString';
|
||||||
|
} on DecryptionException catch (e) {
|
||||||
|
output = e.message;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
setState(() {
|
setState(() {
|
||||||
_output = output;
|
_output = output;
|
||||||
@ -120,6 +166,7 @@ class _MyAppState extends State<MyApp> {
|
|||||||
void dispose() {
|
void dispose() {
|
||||||
// Clean up the controller when the widget is disposed.
|
// Clean up the controller when the widget is disposed.
|
||||||
textController.dispose();
|
textController.dispose();
|
||||||
|
pwdController.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,6 +184,21 @@ class _MyAppState extends State<MyApp> {
|
|||||||
child: Center(
|
child: Center(
|
||||||
child: Column(
|
child: Column(
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
|
TextField(
|
||||||
|
controller: pwdController,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
hintText: 'Test password',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 20),
|
||||||
|
FlatButton(
|
||||||
|
onPressed: _pbkdf2,
|
||||||
|
color: Colors.blue,
|
||||||
|
child: Text(
|
||||||
|
'Pbkdf2',
|
||||||
|
style: TextStyle(color: Colors.white),
|
||||||
|
)),
|
||||||
|
SizedBox(height: 30),
|
||||||
TextField(
|
TextField(
|
||||||
controller: textController,
|
controller: textController,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
@ -151,6 +213,13 @@ class _MyAppState extends State<MyApp> {
|
|||||||
'Encrypt String',
|
'Encrypt String',
|
||||||
style: TextStyle(color: Colors.white),
|
style: TextStyle(color: Colors.white),
|
||||||
)),
|
)),
|
||||||
|
FlatButton(
|
||||||
|
onPressed: _alter,
|
||||||
|
color: Colors.blue,
|
||||||
|
child: Text(
|
||||||
|
'Alter encrypted payload',
|
||||||
|
style: TextStyle(color: Colors.white),
|
||||||
|
)),
|
||||||
FlatButton(
|
FlatButton(
|
||||||
onPressed: _decrypt,
|
onPressed: _decrypt,
|
||||||
color: Colors.blue,
|
color: Colors.blue,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user