verify method

CheckResult verify(
  1. dynamic alg,
  2. Uint8Buffer data,
  3. Uint8Buffer sig
)

Implementation

CheckResult verify(
    dynamic alg, typed.Uint8Buffer data, typed.Uint8Buffer sig) {
  bool Function(Uint8List data, x509.Signature signature) verify =
      (Uint8List data, x509.Signature signature) {
    return false;
  };

  if (pk is x509.EcPublicKey) {
    var mapping = {
      -7: x509.algorithms.signing.ecdsa.sha256,
      -35: x509.algorithms.signing.ecdsa.sha384,
      -36: x509.algorithms.signing.ecdsa.sha512,
    };
    if (!mapping.containsKey(alg)) {
      return CheckResult.unknownAlgorithm;
    } else {
      verify = pk.createVerifier(mapping[alg]!).verify;
    }
  } else if (pk is x509.RsaPublicKey) {
    var mapping = {
      -7: x509.algorithms.signing.rsa.sha256,
      -35: x509.algorithms.signing.rsa.sha384,
      -36: x509.algorithms.signing.rsa.sha512,
    };

    if (alg == -37) {
      ninja.RsaSsaPssVerifier ninv = ninja.RsaSsaPssVerifier(
          hasher: sha256, mgf: Mgf1(hasher: sha256), saltLength: 32);

      var npk = ninja.RSAPublicKey((pk as x509.RsaPublicKey).modulus,
          (pk as x509.RsaPublicKey).exponent);
      verify = (Uint8List d, x509.Signature s) {
        return ninv.verify(npk, s.data, d);
      };
    } else if (!mapping.containsKey(alg)) {
      return CheckResult.unknownAlgorithm;
    } else {
      verify = pk.createVerifier(mapping[alg]!).verify;
    }
  } else {
    return CheckResult.unknownAlgorithm;
  }

  var valid = verify(
      data.buffer.asUint8List(), x509.Signature(sig.buffer.asUint8List()));
  return valid ? CheckResult.valid : CheckResult.invalid;
}