wrapDER function

Uint8List wrapDER(
  1. ByteBuffer payload,
  2. Uint8List oid
)

Wraps the given payload in a DER encoding tagged with the given encoded oid like so: SEQUENCE(oid, BITSTRING(payload))

@param paylod The payload to encode as the bit string @param oid The DER encoded (and SEQUENCE wrapped!) OID to tag the payload with

Implementation

Uint8List wrapDER(ByteBuffer payload, Uint8List oid) {
  // The Bit String header needs to include the unused bit count byte in its length
  final bitStringHeaderLength = 2 + encodeLenBytes(payload.lengthInBytes + 1);
  final len = oid.lengthInBytes + bitStringHeaderLength + payload.lengthInBytes;
  var offset = 0;
  final buf = Uint8List(1 + encodeLenBytes(len) + len);
  // Sequence
  buf[offset++] = 0x30;
  // Sequence Length
  offset += encodeLen(buf, offset, len);

  // OID

  buf.setAll(offset, oid);
  offset += oid.lengthInBytes;

  // Bit String Header
  buf[offset++] = 0x03;
  offset += encodeLen(buf, offset, payload.lengthInBytes + 1);
  // 0 padding
  buf[offset++] = 0x00;
  buf.setAll(offset, Uint8List.fromList(payload.asUint8List()));

  return buf;
}