PkcePair.generate constructor Null safety

PkcePair.generate(
  1. {int length = 32}
)

Generates a PkcePair.

length is the length used to generate the codeVerifier. It must be between 32 and 96, inclusive, which corresponds to a codeVerifier of length between 43 and 128, inclusive. The spec recommends a length of 32.

Implementation

factory PkcePair.generate({int length = 32}) {
  if (length < 32 || length > 96) {
    throw ArgumentError.value(
        length, 'length', 'The length must be between 32 and 96, inclusive.');
  }

  final random = Random.secure();
  final verifier =
      base64UrlEncode(List.generate(length, (_) => random.nextInt(256)))
          .split('=')[0];
  final challenge =
      base64UrlEncode(sha256.convert(ascii.encode(verifier)).bytes)
          .split('=')[0];

  return PkcePair._(verifier, challenge);
}