derInteger static method

BuBytes derInteger(
  1. List<int> bytes
)

DER INTEGER encoding: minimal length, big-endian, with a leading 0x00 inserted whenever the high bit of the first byte is set (so the value is never mistaken for negative under two's-complement rules).

Implementation

static BuBytes derInteger(List<int> bytes) {
  List<int> b = bytes.asBytes;
  var i = 0;
  while (i < b.length - 1 && b[i] == 0 && (b[i + 1] & 0x80) == 0) {
    i++;
  }
  b = b.sublist(i);
  if (b.isEmpty) b = BuBytes([0]);
  if ((b[0] & 0x80) != 0) {
    b = BuBytes.concat([
      [0],
      b,
    ]);
  }
  return derTlv(DerTag.integer, b);
}