isEqual method

bool isEqual(
  1. Object? other
)

Checks if the message digest equals to other.

Here, the other can be a one of the following:

This function will return True if all bytes in the other matches with the bytes of this object. If the length does not match, the type of other is not supported, or a String is not valid hexadecimal, it returns False immediately.

The content comparison is constant-time: it does not exit early on the first mismatching byte, making this method safe for comparing MACs and message digests.

Implementation

bool isEqual(Object? other) {
  if (identical(this, other)) {
    return true;
  } else if (other is ByteCollector) {
    return isEqual(other.bytes);
  } else if (other is ByteBuffer) {
    return isEqual(Uint8List.view(other));
  } else if (other is TypedData && other is! Uint8List) {
    return isEqual(
      Uint8List.view(other.buffer, other.offsetInBytes, other.lengthInBytes),
    );
  } else if (other is String) {
    // A string that is not valid hexadecimal cannot match these bytes.
    final decoded = tryFromHex(other);
    return decoded != null && isEqual(decoded);
  } else if (other is List<int>) {
    return constantTimeEquals(bytes, other);
  } else if (other is Iterable<int>) {
    return isEqual(List<int>.of(other));
  }
  return false;
}