/// <summary>Fills an array of bytes with a cryptographically strong sequence of random nonzero values.</summary> /// <param name="data">The array to fill with a cryptographically strong sequence of random nonzero values. </param> /// <exception cref="T:System.Security.Cryptography.CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="data" /> is null.</exception> public override void GetNonZeroBytes(byte[] data) { if (data == null) { throw new ArgumentNullException("data"); } byte[] array = new byte[data.Length * 2]; int i = 0; while (i < data.Length) { this._handle = RNGCryptoServiceProvider.RngGetBytes(this._handle, array); this.Check(); for (int j = 0; j < array.Length; j++) { if (i == data.Length) { break; } if (array[j] != 0) { data[i++] = array[j]; } } } }
/// <summary>Fills an array of bytes with a cryptographically strong sequence of random values.</summary> /// <param name="data">The array to fill with a cryptographically strong sequence of random values. </param> /// <exception cref="T:System.Security.Cryptography.CryptographicException">The cryptographic service provider (CSP) cannot be acquired. </exception> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="data" /> is null.</exception> public override void GetBytes(byte[] data) { if (data == null) { throw new ArgumentNullException("data"); } if (RNGCryptoServiceProvider._lock == null) { this._handle = RNGCryptoServiceProvider.RngGetBytes(this._handle, data); } else { object @lock = RNGCryptoServiceProvider._lock; lock (@lock) { this._handle = RNGCryptoServiceProvider.RngGetBytes(this._handle, data); } } this.Check(); }