generateSerialNumberBigInt method

BigInt generateSerialNumberBigInt({
  1. int bytes = 16,
})

Generates a cryptographically strong random serial number.

bytes should be between 8 and 20 for production use (RFC 5280).

Implementation

BigInt generateSerialNumberBigInt({int bytes = 16}) {
  if (bytes < 8 || bytes > 20) {
    throw RangeError('bytes must be between 8 and 20 for production use');
  }

  final buffer = calloc<UnsignedChar>(bytes);
  try {
    while (true) {
      final ok = bindings.RAND_bytes(buffer, bytes);
      if (ok != 1) {
        throw OpenSslException('RAND_bytes failed');
      }

      final data = buffer.cast<Uint8>().asTypedList(bytes);
      var allZero = true;
      for (final b in data) {
        if (b != 0) {
          allZero = false;
          break;
        }
      }

      if (allZero) {
        continue;
      }

      return _bigIntFromBytes(data);
    }
  } finally {
    calloc.free(buffer);
  }
}