Skip to content

Commit e8fa778

Browse files
committed
♻️ Make methods static
1 parent 35a2d79 commit e8fa778

4 files changed

Lines changed: 49 additions & 54 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 0.1.0-dev.4
4+
5+
* Make methods static.
6+
37
## 0.1.0-dev.3
48

59
* Support Encryption, Decryption, and Shared Secrets using ECDH.

example/lib/main.dart

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ class MyApp extends StatefulWidget {
1919
}
2020

2121
class _MyAppState extends State<MyApp> {
22-
final _p256Plugin = SecureP256();
23-
2422
String _publicKey = 'Unknown';
2523
String _signed = 'Unknown';
2624
bool? _verified;
@@ -71,76 +69,66 @@ class _MyAppState extends State<MyApp> {
7169
),
7270
ElevatedButton(
7371
onPressed: () {
74-
_p256Plugin.getPublicKey(alias).then(
75-
(r) => setState(() => _publicKey = hex.encode(r.rawKey)),
76-
);
72+
SecureP256.getPublicKey(alias).then(
73+
(r) => setState(() => _publicKey = hex.encode(r.rawKey)),
74+
);
7775
},
7876
child: const Text('getPublicKey'),
7977
),
8078
ElevatedButton(
8179
onPressed: () {
82-
_p256Plugin
83-
.sign(
84-
alias,
85-
Uint8List.fromList(utf8.encode(_verifyPayload)),
86-
)
87-
.then((r) => setState(() => _signed = hex.encode(r)));
80+
SecureP256.sign(
81+
alias,
82+
Uint8List.fromList(utf8.encode(_verifyPayload)),
83+
).then((r) => setState(() => _signed = hex.encode(r)));
8884
},
8985
child: const Text('sign'),
9086
),
9187
ElevatedButton(
9288
onPressed: () {
93-
_p256Plugin
94-
.verify(
95-
Uint8List.fromList(utf8.encode(_verifyPayload)),
96-
P256PublicKey.fromRaw(
97-
Uint8List.fromList(hex.decode(_publicKey)),
98-
),
99-
Uint8List.fromList(hex.decode(_signed)),
100-
)
101-
.then((r) => setState(() => _verified = r));
89+
SecureP256.verify(
90+
Uint8List.fromList(utf8.encode(_verifyPayload)),
91+
P256PublicKey.fromRaw(
92+
Uint8List.fromList(hex.decode(_publicKey)),
93+
),
94+
Uint8List.fromList(hex.decode(_signed)),
95+
).then((r) => setState(() => _verified = r));
10296
},
10397
child: const Text('verify'),
10498
),
10599
ElevatedButton(
106100
onPressed: () {
107-
_p256Plugin
108-
.getSharedSecret(
109-
alias,
110-
P256PublicKey.fromRaw(
111-
Uint8List.fromList(
112-
hex.decode(_othersPublicKeyTEC.text),
113-
),
114-
),
115-
)
116-
.then((r) => setState(() => _sharedSecret = hex.encode(r)));
101+
SecureP256.getSharedSecret(
102+
alias,
103+
P256PublicKey.fromRaw(
104+
Uint8List.fromList(
105+
hex.decode(_othersPublicKeyTEC.text),
106+
),
107+
),
108+
).then((r) => setState(() => _sharedSecret = hex.encode(r)));
117109
},
118110
child: const Text('getSharedSecret'),
119111
),
120112
ElevatedButton(
121113
onPressed: () {
122-
_p256Plugin
123-
.encrypt(
124-
sharedSecret: Uint8List.fromList(
125-
hex.decode(_sharedSecret!),
126-
),
127-
message: Uint8List.fromList(utf8.encode('Hello AstroX')),
128-
)
129-
.then((r) => setState(() => _encrypted = r));
114+
SecureP256.encrypt(
115+
sharedSecret: Uint8List.fromList(
116+
hex.decode(_sharedSecret!),
117+
),
118+
message: Uint8List.fromList(utf8.encode('Hello AstroX')),
119+
).then((r) => setState(() => _encrypted = r));
130120
},
131121
child: const Text('Encrypt (FFI)'),
132122
),
133123
ElevatedButton(
134124
onPressed: () {
135-
_p256Plugin
136-
.decrypt(
137-
sharedSecret: Uint8List.fromList(
138-
hex.decode(_sharedSecret!),
139-
),
140-
iv: _encrypted!.item1,
141-
cipher: _encrypted!.item2,
142-
)
143-
.then((r) => setState(() => _decrypted = utf8.decode(r)));
125+
SecureP256.decrypt(
126+
sharedSecret: Uint8List.fromList(
127+
hex.decode(_sharedSecret!),
128+
),
129+
iv: _encrypted!.item1,
130+
cipher: _encrypted!.item2,
131+
).then((r) => setState(() => _decrypted = utf8.decode(r)));
144132
},
145133
child: const Text('Decrypt (FFI)'),
146134
),

lib/secp256r1.dart

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import 'package:tuple/tuple.dart';
1010
import 'p256_platform_interface.dart';
1111

1212
class SecureP256 {
13-
Future<P256PublicKey> getPublicKey(String tag) async {
13+
const SecureP256._();
14+
15+
static Future<P256PublicKey> getPublicKey(String tag) async {
1416
assert(tag.isNotEmpty);
1517
final raw = await SecureP256Platform.instance.getPublicKey(tag);
1618
// ECDSA starts with 0x04 and 65 length.
@@ -21,7 +23,7 @@ class SecureP256 {
2123
}
2224
}
2325

24-
Future<Uint8List> sign(String tag, Uint8List payload) async {
26+
static Future<Uint8List> sign(String tag, Uint8List payload) async {
2527
assert(tag.isNotEmpty);
2628
assert(payload.isNotEmpty);
2729
final signature = await SecureP256Platform.instance.sign(tag, payload);
@@ -32,7 +34,7 @@ class SecureP256 {
3234
}
3335
}
3436

35-
Future<bool> verify(
37+
static Future<bool> verify(
3638
Uint8List payload,
3739
P256PublicKey publicKey,
3840
Uint8List signature,
@@ -53,7 +55,8 @@ class SecureP256 {
5355
);
5456
}
5557

56-
Future<Uint8List> getSharedSecret(String tag, P256PublicKey publicKey) {
58+
static Future<Uint8List> getSharedSecret(
59+
String tag, P256PublicKey publicKey) {
5760
assert(tag.isNotEmpty);
5861
Uint8List rawKey = publicKey.rawKey;
5962
if (Platform.isAndroid && !isDerPublicKey(rawKey, oidP256)) {
@@ -63,7 +66,7 @@ class SecureP256 {
6366
}
6467

6568
/// Return [iv, cipher].
66-
Future<Tuple2<Uint8List, Uint8List>> encrypt({
69+
static Future<Tuple2<Uint8List, Uint8List>> encrypt({
6770
required Uint8List sharedSecret,
6871
required Uint8List message,
6972
}) async {
@@ -81,7 +84,7 @@ class SecureP256 {
8184
return Tuple2(iv, cipher);
8285
}
8386

84-
Future<Uint8List> decrypt({
87+
static Future<Uint8List> decrypt({
8588
required Uint8List sharedSecret,
8689
required Uint8List iv,
8790
required Uint8List cipher,

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: secp256r1
22
description: >
33
A Flutter plugin that support secp256r1 by Keystore on Android and Secure Enclave on iOS.
4-
version: 0.1.0-dev.3
4+
version: 0.1.0-dev.4
55
repository: https://github.com/AstroxNetwork/flutter_secp256r1
66

77
environment:

0 commit comments

Comments
 (0)