ecEncrypt function
Uint8List
ecEncrypt(
- dynamic data,
- dynamic publicKey
)
Implementation
Uint8List ecEncrypt(data, publicKey) {
if (!(data is Uint8List) && !(data is String)) {
throw "'data' must be a string or Uint8List";
}
if (!(publicKey is Uint8List) && !(publicKey is String)) {
throw "'publicKey' must be a string or Uint8List";
}
if (data is String) {
if (isHex(data)) {
data = hexToUint8List(data);
} else {
data = Uint8List.fromList(utf8.encode(data));
}
}
if (publicKey is String) {
if (isHex(publicKey)) {
publicKey = hexToUint8List(publicKey);
} else {
throw "'publicKey' must be an hexadecimal string";
}
}
Uint8List curveBuf = publicKey.sublist(0, 1);
Uint8List pubBuf = publicKey.sublist(1, publicKey.length);
switch (curveBuf[0]) {
case 0:
try {
Uint8List curve25519Pub =
sodium.Sodium.cryptoSignEd25519PkToCurve25519(pubBuf);
return sodium.Sodium.cryptoBoxSeal(data, curve25519Pub);
} catch (e) {
print(e);
}
return Uint8List(0);
case 1:
case 2:
default:
throw "Curve not supported";
}
}