toBytes method

List<int> toBytes(
  1. List<int> label,
  2. int outLen
)

Generates pseudo-random bytes based on the current transcript state.

This method produces pseudo-random bytes using the current state of the transcript. The generated bytes can be used for various cryptographic purposes. It takes a label and an output length as parameters and appends the label to the transcript.

Parameters:

  • label: A list of integers representing the label for the pseudo-random data.
  • outLen: The length of the pseudo-random data to generate, specified as an integer.

Returns: A List<int> containing the pseudo-random bytes of the specified length.

Usage:

MerlinTranscript transcript = MerlinTranscript("MyApp");
List<int> randomBytes = transcript.toBytes("nonce".codeUnits, 16);
// Generate 16 bytes of pseudo-random data with the label "nonce."

This method appends the label to the transcript, ensuring that it influences the generation of pseudo-random bytes. The generated data is suitable for cryptographic operations.

Implementation

List<int> toBytes(List<int> label, int outLen) {
  final len = List.filled(4, 0);
  writeUint32LE(outLen, len);
  final List<int> labelSize = [...label, ...len];
  strobe.additionalData(true, labelSize);

  final List<int> outBytes = strobe.pseudoRandomData(outLen);
  return BytesUtils.toBytes(outBytes);
}