bytesWrapDer function

Uint8List bytesWrapDer(
  1. Uint8List payload,
  2. Uint8List oid
)

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

payload is the payload to encode as the bit string. oid is the DER encoded (SEQUENCE wrapped) OID to tag the payload with.

Implementation

Uint8List bytesWrapDer(Uint8List payload, Uint8List oid) {
  // The 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;
  int 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));
  return buf;
}