parse static method

ASN1Integer parse(
  1. MutableIterable bytes
)
override

Implementation

static ASN1Integer parse(final MutableIterable bytes) {
  if (bytes.length < 3) {
    throw Exception('Invalid data!');
  }

  int tag = bytes.first;
  if (tag != ASN1Type.integerTag) {
    throw Exception('Invalid tag!');
  }
  bytes.mutate = bytes.skip(1);

  final lengthBigInt = ASN1Object.decodeLength(bytes);
  int length = lengthBigInt.toInt();
  if (length == 0) {
    throw Exception('Invalid data');
  }

  if (bytes.length < length) {
    throw Exception('Invalid data');
  }

  final contentBytes = bytes.take(length);
  bytes.mutate = bytes.skip(length);

  BigInt value = bytesToBigInt(contentBytes);
  if ((contentBytes.first & 0x80) != 0) {
    value = value.toSigned(value.bitLength);
  }

  return ASN1Integer(value);
}