verify method

  1. @override
bool verify(
  1. Uint8List bytes,
  2. Encrypted signature
)
override

Verify encrypted signature.

Implementation

@override
bool verify(Uint8List bytes, Encrypted signature) {
  if (publicKey == null) {
    throw StateError('Can\'t verify without a public key, null given.');
  }

  final hash = Uint8List(_digestCipher.digestSize);

  _digestCipher
    ..reset()
    ..update(bytes, 0, bytes.length)
    ..doFinal(hash, 0);

  _cipher
    ..reset()
    ..init(false, _publicKeyParams!);

  var _signature = Uint8List(_cipher.outputBlockSize);

  try {
    final length = _cipher.processBlock(
        signature.bytes, 0, signature.bytes.length, _signature, 0);
    _signature = _signature.sublist(0, length);
  } on ArgumentError {
    return false;
  }

  final expected = _encode(hash);

  if (_signature.length == expected.length) {
    for (var i = 0; i < _signature.length; i++) {
      if (_signature[i] != expected[i]) {
        return false;
      }
    }

    return true;
  } else if (_signature.length == expected.length - 2) {
    var sigOffset = _signature.length - hash.length - 2;
    var expectedOffset = expected.length - hash.length - 2;

    expected[1] -= 2;
    expected[3] -= 2;

    var nonEqual = 0;

    for (var i = 0; i < hash.length; i++) {
      nonEqual |= (_signature[sigOffset + i] ^ expected[expectedOffset + i]);
    }

    for (int i = 0; i < sigOffset; i++) {
      nonEqual |= (_signature[i] ^ expected[i]);
    }

    return nonEqual == 0;
  } else {
    return false;
  }
}