-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_test.dart
117 lines (109 loc) · 4.02 KB
/
simple_test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import 'dart:typed_data';
import 'package:convert/convert.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:owlchat_crypto/owlchat_crypto.dart';
void main() {
test('should generate new keypair', () async {
final crypto = OwlchatCrypto();
final keypair = await crypto.regenerateKeyPair();
expect(keypair.publicKey, isA<PublicKey>());
expect(keypair.publicKey.expose().length, 32);
expect(keypair.secretKey.expose().length, 32);
expect(keypair.seed, isA<Seed>());
crypto.dispose();
});
test('should backup and restore the same key', () async {
final crypto = OwlchatCrypto();
final keypair = await crypto.regenerateKeyPair();
final paperKey = await crypto.backupKeyPair(seed: keypair.seed);
final isValidPaperkey = await crypto.validatePaperKey(paperKey);
expect(isValidPaperkey, isTrue);
final restoredKeypair = await crypto.restoreKeyPair(paperKey);
expect(restoredKeypair.publicKey.asBase64(), keypair.publicKey.asBase64());
expect(restoredKeypair.secretKey.asBase64(), keypair.secretKey.asBase64());
expect(restoredKeypair.seed?.asBase64(), keypair.seed?.asBase64());
crypto.dispose();
});
test('should encrypt and decrypt', () async {
final crypto = OwlchatCrypto();
await crypto.regenerateKeyPair();
const message = 'Hello World';
final encrypted = await crypto.encrypt(
Uint8List.fromList(message.codeUnits),
);
final decrypted = await crypto.decrypt(encrypted);
expect(String.fromCharCodes(decrypted), message);
crypto.dispose();
});
test('should sign and verify', () async {
final crypto = OwlchatCrypto();
final keypair = await crypto.regenerateKeyPair();
const message = 'Hello World';
final signature = await crypto.sign(
Uint8List.fromList(message.codeUnits),
);
final verified = await crypto.verify(
theirPublicKey: keypair.publicKey,
message: Uint8List.fromList(message.codeUnits),
signature: signature,
);
expect(verified, isTrue);
crypto.dispose();
});
test('conversation between two crypto', () async {
final aliceCrypto = OwlchatCrypto();
final bobCrypto = OwlchatCrypto();
final aliceKeypair = await aliceCrypto.currentKeyPair;
final bobKeypair = await bobCrypto.currentKeyPair;
// sanity check
expect(
aliceKeypair.publicKey.asBase64() == bobKeypair.publicKey.asBase64(),
isFalse,
);
// fist we do a handshake
final aliceSharedSecret = await aliceCrypto.diffieHellmanKeyExchange(
bobKeypair.publicKey,
);
// alice creates a message and encrypt it
const message = 'Hello Bob!';
final aliceEncrypted = await aliceCrypto.encrypt(
Uint8List.fromList(message.codeUnits),
sharedSecret: aliceSharedSecret,
);
// also we need to sign the message
final aliceSignature = await aliceCrypto.sign(aliceEncrypted);
// bob receives the message and the signature
// first we verify the signature
final bobVerified = await bobCrypto.verify(
theirPublicKey: aliceKeypair.publicKey,
message: aliceEncrypted,
signature: aliceSignature,
);
expect(bobVerified, isTrue);
// then we create our shared secret
final bobSharedSecret = await bobCrypto.diffieHellmanKeyExchange(
aliceKeypair.publicKey,
);
// both shared secret are the same
expect(aliceSharedSecret.asBase64(), bobSharedSecret.asBase64());
// bob decrypts the message
final bobDecrypted = await bobCrypto.decrypt(
aliceEncrypted,
sharedSecret: bobSharedSecret,
);
// we should get the same message
expect(String.fromCharCodes(bobDecrypted), message);
aliceCrypto.dispose();
bobCrypto.dispose();
});
test('should be able to hash simple string', () async {
final crypto = OwlchatCrypto();
final hash = await crypto.hash(Uint8List.fromList('Hello World'.codeUnits));
expect(hash.length, 32);
expect(
hex.encode(hash),
"a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e",
);
crypto.dispose();
});
}