Blake2Config.blake2b constructor

Blake2Config.blake2b({
  1. List<int>? key,
  2. List<int>? salt,
  3. List<int>? personalization,
  4. int? fanout,
  5. int? maxDepth,
  6. int? leafSize,
  7. int? nodeOffset,
  8. int? nodeDepth,
  9. int? innerDigestLength,
  10. bool? lastNode,
})

Implementation

factory Blake2Config.blake2b({
  List<int>? key,
  List<int>? salt,
  List<int>? personalization,
  int? fanout,
  int? maxDepth,
  int? leafSize,
  int? nodeOffset,
  int? nodeDepth,
  int? innerDigestLength,
  bool? lastNode,
}) {
  fanout ??= 1;
  maxDepth ??= 1;
  leafSize ??= 0;
  nodeOffset ??= 0;
  innerDigestLength ??= 0;
  lastNode ??= false;
  nodeDepth ??= 0;

  const keyLength = 64;
  const int saltLength = 16;
  const int personalizationLength = 16;
  const int maxFanout = 255;
  const int maxMaxDepth = 255;
  const int maxLeafSize = 4294967295;
  const int maxNodeOffset = 281474976710655;
  const digestLength = 64;
  if (key != null && key.length > keyLength) {
    throw CoreErrors.invalidOperationArguments(
      "Blake2Config",
      name: "key",
      reason: "Incorrect key length.",
    );
  }
  if (salt != null) {
    if (salt.length > saltLength) {
      throw CoreErrors.invalidOperationArguments(
        "Blake2Config",
        name: "salt",
        reason: "Incorrect salt length.",
      );
    }
    if (salt.length != saltLength) {
      salt = List.filled(saltLength, 0)..setAll(0, salt);
    }
  }
  if (personalization != null) {
    if (personalization.length > personalizationLength) {
      throw CoreErrors.invalidOperationArguments(
        "Blake2Config",
        name: "personalization",
        reason: "Incorrect personalization length.",
      );
    }
    if (personalization.length != personalizationLength) {
      personalization = List.filled(personalizationLength, 0)
        ..setAll(0, personalization);
    }
  }
  if (fanout < 0 || fanout > maxFanout) {
    throw CoreErrors.invalidOperationArguments(
      "Blake2Config",
      name: "fanout",
      reason: "Incorrect fanout.",
    );
  }
  if (maxDepth < 0 || maxDepth > maxMaxDepth) {
    throw CoreErrors.invalidOperationArguments(
      "Blake2Config",
      name: "depth",
      reason: "Incorrect depth.",
    );
  }
  if (leafSize < 0 || leafSize > maxLeafSize) {
    throw CoreErrors.invalidOperationArguments(
      "Blake2Config",
      name: "leafSize",
      reason: "Incorrect leaf size.",
    );
  }
  if (innerDigestLength < 0 || innerDigestLength > digestLength) {
    throw CoreErrors.invalidOperationArguments(
      "Blake2Config",
      name: "innerDigestLength",
      reason: "Incorrect inner digest length",
    );
  }
  if (nodeOffset < 0 || nodeOffset > maxNodeOffset) {
    throw CoreErrors.invalidOperationArguments(
      "Blake2Config",
      name: "nodeOffset",
      reason: "Incorrect node offset.",
    );
  }
  return Blake2Config._(
    personalization: personalization,
    salt: salt,
    fanout: fanout,
    innerDigestLength: innerDigestLength,
    key: key,
    lastNode: lastNode,
    leafSize: leafSize,
    maxDepth: maxDepth,
    nodeDepth: nodeDepth,
    nodeOffset: nodeOffset,
  );
}