fromConcatenation static method

SecretBox fromConcatenation(
  1. List<int> data, {
  2. required int nonceLength,
  3. required int macLength,
  4. bool copy = true,
})

Constructs a SecretBox from a concatenation of nonce, cipherText and mac.

If copy is true, the cipherText, nonce and mac are copied from the data. If copy is false and data is Uint8List, Uint8List.view will be used.

Implementation

static SecretBox fromConcatenation(
  List<int> data, {
  required int nonceLength,
  required int macLength,
  bool copy = true,
}) {
  if (nonceLength < 0) {
    throw ArgumentError.value(nonceLength, 'nonceLength');
  }
  if (macLength < 0) {
    throw ArgumentError.value(macLength, 'macLength');
  }
  if (data.length < nonceLength + macLength) {
    throw ArgumentError.value(
      data,
      'data',
      'Less than minimum length ($nonceLength + $macLength)',
    );
  }
  if (data is Uint8List) {
    final nonce = Uint8List.view(
      data.buffer,
      data.offsetInBytes,
      nonceLength,
    );
    final cipherText = Uint8List.view(
      data.buffer,
      data.offsetInBytes + nonceLength,
      data.length - nonceLength - macLength,
    );
    final macBytes = Uint8List.view(
      data.buffer,
      data.offsetInBytes + data.lengthInBytes - macLength,
      macLength,
    );
    if (copy) {
      return SecretBox(
        Uint8List.fromList(cipherText),
        nonce: Uint8List.fromList(nonce),
        mac: Mac(Uint8List.fromList(macBytes)),
      );
    }
    return SecretBox(
      cipherText,
      nonce: nonce,
      mac: Mac(macBytes),
    );
  } else {
    return fromConcatenation(
      Uint8List.fromList(data),
      nonceLength: nonceLength,
      macLength: macLength,
      copy: false,
    );
  }
}