Generate Cryptographically Secure OTP Codes

ebeeraheem

View Profile
7 views
Nov 28, 2025
   /// <summary>
   /// Generates a secure 8-digit code using cryptographically secure random number generator
   /// </summary>
   /// <returns>Secure 8-digit order acceptance code</returns>
   private static long GenerateSecureCode()
   {
       using var rng = RandomNumberGenerator.Create();
       var bytes = new byte[4];
       rng.GetBytes(bytes);
       // Convert to 8-digit number (10000000-99999999)
       var number = Math.Abs(BitConverter.ToInt32(bytes, 0)) % 90000000 + 10000000;
       return number;
   }