mirror of
https://gitlab.com/veilid/veilid.git
synced 2025-12-14 23:45:53 -05:00
Switch to crypto to typed keys everywhere
This commit is contained in:
parent
88f69ce237
commit
848da0ae4e
180 changed files with 8532 additions and 8488 deletions
|
|
@ -3,93 +3,102 @@ import 'dart:convert';
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:veilid/veilid.dart';
|
||||
|
||||
Future<void> testBestCryptoSystem() async {
|
||||
final cs = await Veilid.instance.bestCryptoSystem();
|
||||
expect(await cs.defaultSaltLength(), equals(16));
|
||||
Future<void> testListCryptoSystems() async {
|
||||
final cryptoKinds = Veilid.instance.validCryptoKinds();
|
||||
expect(cryptoKinds, isNotEmpty);
|
||||
}
|
||||
|
||||
Future<void> testGetCryptoSystem() async {
|
||||
Future<void> testGetCryptoSystems() async {
|
||||
final cs = await Veilid.instance.getCryptoSystem(cryptoKindVLD0);
|
||||
expect(await cs.defaultSaltLength(), equals(16));
|
||||
}
|
||||
|
||||
const CryptoKind invalidCryptoKind = cryptoKindNONE + 1;
|
||||
|
||||
Future<void> testGetCryptoSystemInvalid() async {
|
||||
await expectLater(() async => Veilid.instance.getCryptoSystem(cryptoKindNONE),
|
||||
await expectLater(
|
||||
() async => Veilid.instance.getCryptoSystem(invalidCryptoKind),
|
||||
throwsA(isA<VeilidAPIException>()));
|
||||
}
|
||||
|
||||
Future<void> testHashAndVerifyPassword() async {
|
||||
final cs = await Veilid.instance.bestCryptoSystem();
|
||||
final nonce = await cs.randomNonce();
|
||||
final salt = nonce.decode();
|
||||
for (final kind in Veilid.instance.validCryptoKinds()) {
|
||||
final cs = await Veilid.instance.getCryptoSystem(kind);
|
||||
final nonce = await cs.randomNonce();
|
||||
final salt = nonce.decode();
|
||||
|
||||
// Password match
|
||||
final phash = await cs.hashPassword(utf8.encode('abc123'), salt);
|
||||
expect(await cs.verifyPassword(utf8.encode('abc123'), phash), isTrue);
|
||||
// Password match
|
||||
final phash = await cs.hashPassword(utf8.encode('abc123'), salt);
|
||||
expect(await cs.verifyPassword(utf8.encode('abc123'), phash), isTrue);
|
||||
|
||||
// Password mismatch
|
||||
await cs.hashPassword(utf8.encode('abc1234'), salt);
|
||||
expect(await cs.verifyPassword(utf8.encode('abc1235'), phash), isFalse);
|
||||
// Password mismatch
|
||||
await cs.hashPassword(utf8.encode('abc1234'), salt);
|
||||
expect(await cs.verifyPassword(utf8.encode('abc1235'), phash), isFalse);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> testSignAndVerifySignature() async {
|
||||
final cs = await Veilid.instance.bestCryptoSystem();
|
||||
final kp1 = await cs.generateKeyPair();
|
||||
final kp2 = await cs.generateKeyPair();
|
||||
for (final kind in Veilid.instance.validCryptoKinds()) {
|
||||
final cs = await Veilid.instance.getCryptoSystem(kind);
|
||||
final kp1 = await cs.generateKeyPair();
|
||||
final kp2 = await cs.generateKeyPair();
|
||||
|
||||
// BareSignature match
|
||||
final sig = await cs.sign(kp1.key, kp1.secret, utf8.encode('abc123'));
|
||||
expect(await cs.verify(kp1.key, utf8.encode('abc123'), sig), isTrue);
|
||||
// Signature match
|
||||
final sig = await cs.sign(kp1.key, kp1.secret, utf8.encode('abc123'));
|
||||
expect(await cs.verify(kp1.key, utf8.encode('abc123'), sig), isTrue);
|
||||
|
||||
// BareSignature mismatch
|
||||
final sig2 = await cs.sign(kp1.key, kp1.secret, utf8.encode('abc1234'));
|
||||
expect(await cs.verify(kp1.key, utf8.encode('abc1234'), sig2), isTrue);
|
||||
expect(await cs.verify(kp1.key, utf8.encode('abc12345'), sig2), isFalse);
|
||||
expect(await cs.verify(kp2.key, utf8.encode('abc1234'), sig2), isFalse);
|
||||
// Signature mismatch
|
||||
final sig2 = await cs.sign(kp1.key, kp1.secret, utf8.encode('abc1234'));
|
||||
expect(await cs.verify(kp1.key, utf8.encode('abc1234'), sig2), isTrue);
|
||||
expect(await cs.verify(kp1.key, utf8.encode('abc12345'), sig2), isFalse);
|
||||
expect(await cs.verify(kp2.key, utf8.encode('abc1234'), sig2), isFalse);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> testSignAndVerifySignatures() async {
|
||||
final cs = await Veilid.instance.bestCryptoSystem();
|
||||
final kind = cs.kind();
|
||||
final kp1 = await cs.generateKeyPair();
|
||||
final kps = <KeyPair>[];
|
||||
for (final kind in Veilid.instance.validCryptoKinds()) {
|
||||
final cs = await Veilid.instance.getCryptoSystem(kind);
|
||||
final kp = await cs.generateKeyPair();
|
||||
kps.add(kp);
|
||||
}
|
||||
|
||||
// BareSignature match
|
||||
final sigs = await Veilid.instance.generateSignatures(
|
||||
utf8.encode('abc123'), [KeyPair.fromBareKeyPair(kind, kp1)]);
|
||||
// Signature match
|
||||
final sigs =
|
||||
await Veilid.instance.generateSignatures(utf8.encode('abc123'), kps);
|
||||
final pks = kps.map((kp) => kp.key).toList();
|
||||
expect(
|
||||
await Veilid.instance.verifySignatures(
|
||||
[PublicKey(kind: kind, value: kp1.key)], utf8.encode('abc123'), sigs),
|
||||
equals([PublicKey(kind: kind, value: kp1.key)]));
|
||||
// BareSignature mismatch
|
||||
await Veilid.instance.verifySignatures(pks, utf8.encode('abc123'), sigs),
|
||||
equals(pks));
|
||||
// Signature mismatch
|
||||
expect(
|
||||
await Veilid.instance.verifySignatures(
|
||||
[PublicKey(kind: kind, value: kp1.key)],
|
||||
utf8.encode('abc1234'),
|
||||
sigs),
|
||||
await Veilid.instance.verifySignatures(pks, utf8.encode('abc1234'), sigs),
|
||||
isNull);
|
||||
}
|
||||
|
||||
Future<void> testGenerateSharedSecret() async {
|
||||
final cs = await Veilid.instance.bestCryptoSystem();
|
||||
for (final kind in Veilid.instance.validCryptoKinds()) {
|
||||
final cs = await Veilid.instance.getCryptoSystem(kind);
|
||||
|
||||
final kp1 = await cs.generateKeyPair();
|
||||
final kp2 = await cs.generateKeyPair();
|
||||
final kp3 = await cs.generateKeyPair();
|
||||
final kp1 = await cs.generateKeyPair();
|
||||
final kp2 = await cs.generateKeyPair();
|
||||
final kp3 = await cs.generateKeyPair();
|
||||
|
||||
final ssA =
|
||||
await cs.generateSharedSecret(kp1.key, kp2.secret, utf8.encode('abc123'));
|
||||
final ssB =
|
||||
await cs.generateSharedSecret(kp2.key, kp1.secret, utf8.encode('abc123'));
|
||||
final ssA = await cs.generateSharedSecret(
|
||||
kp1.key, kp2.secret, utf8.encode('abc123'));
|
||||
final ssB = await cs.generateSharedSecret(
|
||||
kp2.key, kp1.secret, utf8.encode('abc123'));
|
||||
|
||||
expect(ssA, equals(ssB));
|
||||
expect(ssA, equals(ssB));
|
||||
|
||||
final ssC = await cs.generateSharedSecret(
|
||||
kp2.key, kp1.secret, utf8.encode('abc1234'));
|
||||
final ssC = await cs.generateSharedSecret(
|
||||
kp2.key, kp1.secret, utf8.encode('abc1234'));
|
||||
|
||||
expect(ssA, isNot(equals(ssC)));
|
||||
expect(ssA, isNot(equals(ssC)));
|
||||
|
||||
final ssD =
|
||||
await cs.generateSharedSecret(kp3.key, kp1.secret, utf8.encode('abc123'));
|
||||
final ssD = await cs.generateSharedSecret(
|
||||
kp3.key, kp1.secret, utf8.encode('abc123'));
|
||||
|
||||
expect(ssA, isNot(equals(ssD)));
|
||||
expect(ssA, isNot(equals(ssD)));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue