randomBigInt static method

BigInt randomBigInt(
  1. int bitLength
)

Creates a random Big Integer with roughly bitLength amount of bits using a secure random generator. If you need EXACTLY bigLength amount of bits, you might need to pad the resulting BigInt

Implementation

static BigInt randomBigInt(int bitLength) {
  final random = Random.secure();
  final hexDigitCount =
      (bitLength / 4).ceil(); // Each hex digit represents 4 bits
  final buffer = StringBuffer();

  while (buffer.length < hexDigitCount) {
    buffer.write(random.nextInt(16).toRadixString(16));
  }

  // Add leading zeros if necessary to reach the desired bit length
  final hexString = buffer.toString().padLeft(hexDigitCount, '0');
  return BigInt.parse(hexString, radix: 16);
}