Skip to content

Commit 2064367

Browse files
authored
Merge pull request #141 from harrowmykel/flutter-decrypt-sample-code
flutter decryption sample code
2 parents cfdd5b1 + 59c01d9 commit 2064367

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

cryptography_flutter/example/lib/main.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class _CipherPageState extends State<CipherPage> {
6868
final _cipherTextController = TextEditingController();
6969
final _macController = TextEditingController();
7070
Object? _error;
71+
String _decryptedText = '';
7172

7273
@override
7374
Widget build(BuildContext context) {
@@ -190,6 +191,14 @@ class _CipherPageState extends State<CipherPage> {
190191
const InputDecoration(labelText: 'Cleartext (text)'),
191192
),
192193
const SizedBox(height: 10),
194+
const Text('Decrypted Text'),
195+
const SizedBox(height: 5),
196+
Container(
197+
color: Colors.grey.shade500,
198+
padding: const EdgeInsets.all(4),
199+
child: Text(_decryptedText),
200+
),
201+
const SizedBox(height: 10),
193202
TextField(
194203
controller: _cipherTextController,
195204
minLines: 1,
@@ -229,6 +238,9 @@ class _CipherPageState extends State<CipherPage> {
229238
);
230239
_cipherTextController.text = _toHex(secretBox.cipherText);
231240
_macController.text = _toHex(secretBox.mac.bytes);
241+
242+
_decrypt();
243+
232244
setState(() {
233245
_error = null;
234246
});
@@ -241,4 +253,19 @@ class _CipherPageState extends State<CipherPage> {
241253
return;
242254
}
243255
}
256+
257+
Future<void> _decrypt() async {
258+
final cipher = _cipher;
259+
260+
_decryptedText = utf8.decode(await cipher.decrypt(
261+
SecretBox(
262+
_fromHex(_cipherTextController.text),
263+
nonce: _fromHex(_nonceController.text),
264+
mac: Mac(_fromHex(_macController.text)),
265+
),
266+
secretKey: SecretKeyData(
267+
_fromHex(_secretKeyController.text),
268+
),
269+
));
270+
}
244271
}

0 commit comments

Comments
 (0)