veilidchat/lib/tools/secret_crypto.dart
Christien Rioux a93c711d52 proto cleanup
2023-09-26 18:46:02 -04:00

47 lines
1.4 KiB
Dart

import 'dart:typed_data';
import '../entities/local_account.dart';
import '../veilid_init.dart';
import '../veilid_support/veilid_support.dart';
Future<Uint8List> encryptSecretToBytes(
{required SecretKey secret,
required CryptoKind cryptoKind,
EncryptionKeyType encryptionKeyType = EncryptionKeyType.none,
String encryptionKey = ''}) async {
final veilid = await eventualVeilid.future;
late final Uint8List secretBytes;
switch (encryptionKeyType) {
case EncryptionKeyType.none:
secretBytes = secret.decode();
case EncryptionKeyType.pin:
case EncryptionKeyType.password:
final cs = await veilid.getCryptoSystem(cryptoKind);
secretBytes =
await cs.encryptAeadWithPassword(secret.decode(), encryptionKey);
}
return secretBytes;
}
Future<SecretKey> decryptSecretFromBytes(
{required Uint8List secretBytes,
required CryptoKind cryptoKind,
EncryptionKeyType encryptionKeyType = EncryptionKeyType.none,
String encryptionKey = ''}) async {
final veilid = await eventualVeilid.future;
late final SecretKey secret;
switch (encryptionKeyType) {
case EncryptionKeyType.none:
secret = SecretKey.fromBytes(secretBytes);
case EncryptionKeyType.pin:
case EncryptionKeyType.password:
final cs = await veilid.getCryptoSystem(cryptoKind);
secret = SecretKey.fromBytes(
await cs.decryptAeadWithPassword(secretBytes, encryptionKey));
}
return secret;
}