Skip to content

Commit 7a22f92

Browse files
authored
feat: add decrypt+verify and updated encrypt+sign (#70)
1 parent e2a7549 commit 7a22f92

16 files changed

+265
-75
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

example/integration_test/app_test.dart

+52
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,58 @@ void main() {
6666
}, timeout: Timeout(Duration(seconds: 60)));
6767
});
6868

69+
group('EncryptSign and DecryptVerify', () {
70+
final parent = find.byKey(ValueKey("encrypt-sign-decrypt-verify"));
71+
72+
testWidgets('Encrypt / Decrypt', (WidgetTester tester) async {
73+
final instance = app.MyApp();
74+
await tester.pumpWidget(instance);
75+
await tester.pumpAndSettle();
76+
77+
var container = find.descendant(
78+
of: parent,
79+
matching: find.byKey(ValueKey("encrypt")),
80+
);
81+
await tester.scrollUntilVisible(container, dyScroll, scrollable: list);
82+
await tester.pumpAndSettle();
83+
84+
await tester.enterText(
85+
find.descendant(
86+
of: container, matching: find.byKey(ValueKey("message"))),
87+
input);
88+
await tester.tap(
89+
find.descendant(
90+
of: container, matching: find.byKey(ValueKey("button"))),
91+
);
92+
await tester.pumpAndSettle(Duration(seconds: 3));
93+
var resultSelector = find.descendant(
94+
of: container, matching: find.byKey(ValueKey("result")));
95+
96+
await expectLater(resultSelector, findsWidgets);
97+
var result = resultSelector.evaluate().single.widget as Text;
98+
expect(result.data != "", equals(true));
99+
100+
container = find.descendant(
101+
of: parent,
102+
matching: find.byKey(ValueKey("decrypt")),
103+
);
104+
await tester.scrollUntilVisible(container, dyScroll, scrollable: list);
105+
await tester.pumpAndSettle();
106+
107+
await tester.tap(
108+
find.descendant(
109+
of: container, matching: find.byKey(ValueKey("button"))),
110+
);
111+
await tester.pumpAndSettle(Duration(seconds: 3));
112+
resultSelector = find.descendant(
113+
of: container, matching: find.byKey(ValueKey("result")));
114+
await expectLater(resultSelector, findsWidgets);
115+
116+
result = resultSelector.evaluate().single.widget as Text;
117+
expect(result.data, equals(input));
118+
}, timeout: Timeout(Duration(seconds: 60)));
119+
});
120+
69121
group('Encrypt and Decrypt Bytes', () {
70122
final parent = find.byKey(ValueKey("encrypt-decrypt-bytes"));
71123

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter/widgets.dart';
4+
5+
import 'package:openpgp/openpgp.dart';
6+
import 'package:openpgp_example/main.dart';
7+
import 'package:openpgp_example/shared/button_widget.dart';
8+
import 'package:openpgp_example/shared/input_widget.dart';
9+
import 'package:openpgp_example/shared/title_widget.dart';
10+
11+
class EncryptSignAndDecryptVerify extends StatefulWidget {
12+
const EncryptSignAndDecryptVerify({
13+
Key? key,
14+
required this.title,
15+
required KeyPair? keyPair,
16+
}) : keyPair = keyPair,
17+
super(key: key);
18+
19+
final KeyPair? keyPair;
20+
final String title;
21+
22+
@override
23+
_EncryptSignAndDecryptVerifyState createState() =>
24+
_EncryptSignAndDecryptVerifyState();
25+
}
26+
27+
class _EncryptSignAndDecryptVerifyState
28+
extends State<EncryptSignAndDecryptVerify> {
29+
String _encrypted = "";
30+
String _decrypted = "";
31+
32+
@override
33+
Widget build(BuildContext context) {
34+
return Container(
35+
padding: const EdgeInsets.all(10),
36+
child: Card(
37+
child: Column(
38+
children: [
39+
TitleWidget(widget.title),
40+
InputWidget(
41+
title: "EncryptSign",
42+
key: Key("encrypt"),
43+
result: _encrypted,
44+
onPressed: (controller) async {
45+
try {
46+
var entity = Entity();
47+
entity.privateKey = widget.keyPair!.privateKey;
48+
entity.passphrase = passphrase;
49+
var encrypted = await OpenPGP.encrypt(
50+
controller.text,
51+
widget.keyPair!.publicKey,
52+
signed: entity,
53+
);
54+
setState(() {
55+
_encrypted = encrypted;
56+
});
57+
} catch (e) {
58+
print(e.toString());
59+
}
60+
},
61+
),
62+
ButtonWidget(
63+
title: "DecryptVerify",
64+
key: Key("decrypt"),
65+
result: _decrypted,
66+
onPressed: () async {
67+
try {
68+
var entity = Entity();
69+
entity.publicKey = widget.keyPair!.publicKey;
70+
var decrypted = await OpenPGP.decrypt(
71+
_encrypted,
72+
widget.keyPair!.privateKey,
73+
passphrase,
74+
signed: entity,
75+
);
76+
setState(() {
77+
_decrypted = decrypted;
78+
});
79+
} catch (e) {
80+
print(e.toString());
81+
}
82+
},
83+
),
84+
],
85+
),
86+
),
87+
);
88+
}
89+
}

example/lib/main.dart

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
66
import 'package:flutter/widgets.dart';
77

88
import 'package:openpgp/openpgp.dart';
9+
import 'package:openpgp_example/encrypt_sign_decrypt_verify.dart';
910
import 'package:openpgp_example/encrypt_decrypt.dart';
1011
import 'package:openpgp_example/encrypt_decrypt_bytes.dart';
1112
import 'package:openpgp_example/encrypt_decrypt_file.dart';
@@ -125,6 +126,11 @@ class _MyAppState extends State<MyApp> {
125126
keyPair: _defaultKeyPair,
126127
key: Key("encrypt-decrypt"),
127128
),
129+
EncryptSignAndDecryptVerify(
130+
title: "EncryptSign And DecryptVerify",
131+
keyPair: _defaultKeyPair,
132+
key: Key("encrypt-sign-decrypt-verify"),
133+
),
128134
if (false)
129135
EncryptAndDecryptFile(
130136
title: "Encrypt And Decrypt file",

0 commit comments

Comments
 (0)