dvDecodeDerSignature function

({BigInt r, BigInt s})? dvDecodeDerSignature(
  1. Uint8List der
)

Reads a DER-encoded ECDSA signature. Null when it is not one.

Implementation

({BigInt r, BigInt s})? dvDecodeDerSignature(Uint8List der) {
  int at = 0;
  if (der.length < 8 || der[at++] != 0x30) return null;
  final int total = der[at++];
  if (total != der.length - 2) return null;

  BigInt? readInteger() {
    if (at >= der.length || der[at++] != 0x02) return null;
    if (at >= der.length) return null;
    final int length = der[at++];
    if (length <= 0 || at + length > der.length) return null;
    final Uint8List bytes = Uint8List.sublistView(der, at, at + length);
    at += length;
    BigInt value = BigInt.zero;
    for (final int byte in bytes) {
      value = (value << 8) | BigInt.from(byte);
    }
    return value;
  }

  final BigInt? r = readInteger();
  final BigInt? s = readInteger();
  if (r == null || s == null || at != der.length) return null;
  return (r: r, s: s);
}