ecdsaDerToRaw static method

Uint8List ecdsaDerToRaw(
  1. Uint8List derSignature, {
  2. required String namedCurve,
})

Implementation

static Uint8List ecdsaDerToRaw(
  Uint8List derSignature, {
  required String namedCurve,
}) {
  final coordinateLength = curveCoordinateLength(namedCurve);
  if (derSignature.isEmpty || derSignature[0] != 0x30) {
    throw ArgumentError('ECDSA DER inválida');
  }
  final (seqLen, seqLenBytes) = _readDerLength(derSignature, 1);
  final seqStart = 1 + seqLenBytes;
  if (seqStart + seqLen > derSignature.length) {
    throw ArgumentError('ECDSA DER truncada');
  }
  var offset = seqStart;
  if (derSignature[offset] != 0x02) {
    throw ArgumentError('ECDSA DER inválida (R)');
  }
  final (rLen, rLenBytes) = _readDerLength(derSignature, offset + 1);
  final rStart = offset + 1 + rLenBytes;
  final r = derSignature.sublist(rStart, rStart + rLen);
  offset = rStart + rLen;
  if (derSignature[offset] != 0x02) {
    throw ArgumentError('ECDSA DER inválida (S)');
  }
  final (sLen, sLenBytes) = _readDerLength(derSignature, offset + 1);
  final sStart = offset + 1 + sLenBytes;
  final s = derSignature.sublist(sStart, sStart + sLen);

  final raw = Uint8List(coordinateLength * 2);
  _copyBigIntBytesToFixed(r, raw, 0, coordinateLength);
  _copyBigIntBytesToFixed(s, raw, coordinateLength, coordinateLength);
  return raw;
}