-
Notifications
You must be signed in to change notification settings - Fork 0
/
p256.dart
77 lines (71 loc) · 2.21 KB
/
p256.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
part of 'utils.dart';
class SecureP256 {
const SecureP256._();
static Future<P256PublicKey> getPublicKey(String tag) async {
assert(tag.isNotEmpty);
final raw = await SecureP256Platform.instance.getPublicKey(tag);
// ECDSA starts with 0x04 and 65 length.
if (raw.lengthInBytes == 65) {
return P256PublicKey.fromRaw(raw);
} else {
return P256PublicKey.fromDer(raw);
}
}
static Future<Uint8List> getSharedSecret(
String tag,
P256PublicKey publicKey, [
Tuple<FutureOr<void> Function(String, Uint8List)?,
FutureOr<Uint8List>? Function(Uint8List)?>?
hooks,
]) async {
assert(tag.isNotEmpty);
Uint8List rawKey = publicKey.rawKey;
if (Platform.isAndroid && !isDerPublicKey(rawKey, oidP256)) {
rawKey = bytesWrapDer(rawKey, oidP256);
}
hooks?.item1?.call(tag, rawKey);
final sharedSecret =
await SecureP256Platform.instance.getSharedSecret(tag, rawKey);
return hooks?.item2?.call(sharedSecret) ?? sharedSecret;
}
static Future<bool> isKeyCreated(String tag) async {
assert(tag.isNotEmpty);
return SecureP256Platform.instance.isKeyCreated(tag);
}
static Future<Uint8List> sign(
String tag,
Uint8List payload, [
Tuple<FutureOr<void> Function(String, Uint8List)?,
FutureOr<Uint8List>? Function(Uint8List)?>?
hooks,
]) async {
assert(tag.isNotEmpty);
assert(payload.isNotEmpty);
hooks?.item1?.call(tag, payload);
var signature = await SecureP256Platform.instance.sign(tag, payload);
if (!isDerSignature(signature)) {
signature = bytesWrapDerSignature(signature);
}
return hooks?.item2?.call(signature) ?? signature;
}
static Future<bool> verify(
Uint8List payload,
P256PublicKey publicKey,
Uint8List signature,
) {
assert(payload.isNotEmpty);
assert(signature.isNotEmpty);
Uint8List rawKey = publicKey.rawKey;
if (Platform.isAndroid && !isDerPublicKey(rawKey, oidP256)) {
rawKey = bytesWrapDer(rawKey, oidP256);
}
if (!isDerSignature(signature)) {
signature = bytesWrapDerSignature(signature);
}
return SecureP256Platform.instance.verify(
payload,
rawKey,
signature,
);
}
}