verify function
bool
verify(
- dynamic sig,
- dynamic data,
- dynamic publicKey
)
Implementation
bool verify(dynamic sig, dynamic data, dynamic publicKey) {
if (sig is! Uint8List && sig is! String) {
throw "'sig' must be a string or Uint8List";
}
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 (sig is String) {
if (isHex(sig)) {
sig = Uint8List.fromList(hexToUint8List(sig));
} else {
throw "'signature' must be an hexadecimal string";
}
}
if (data is String) {
if (isHex(data)) {
data = Uint8List.fromList(hexToUint8List(data));
} else {
data = Uint8List.fromList(utf8.encode(data));
}
}
if (publicKey is String) {
if (isHex(publicKey)) {
publicKey = Uint8List.fromList(hexToUint8List(publicKey));
} else {
throw "'publicKey' must be an hexadecimal string";
}
}
final Uint8List curveBuf = publicKey.sublist(0, 1);
final Uint8List pubBuf = publicKey.sublist(2, publicKey.length);
switch (curveBuf[0]) {
case 0:
final verifyKey = ed25519.VerifyKey(pubBuf);
return verifyKey.verify(signature: ed25519.Signature(sig), message: data);
case 1:
final sha256 = Digest('SHA-256');
final msgHash = sha256.process(data);
final ec = elliptic.getP256();
final publicKey = elliptic.PublicKey.fromHex(ec, uint8ListToHex(pubBuf));
final signature = ecdsa.Signature.fromASN1(sig);
return ecdsa.verify(publicKey, msgHash, signature);
case 2:
final sha256 = Digest('SHA-256');
final msgHash = sha256.process(data);
final ec = elliptic.getSecp256k1();
final publicKey = elliptic.PublicKey.fromHex(ec, uint8ListToHex(pubBuf));
final signature = ecdsa.Signature.fromASN1(sig);
return ecdsa.verify(publicKey, msgHash, signature);
default:
throw 'Curve not supported';
}
}