public void WhenCalled_ThenReturnsRandomInt32() { using (var rng = new RNGCryptoServiceProvider()) { var result1 = rng.GetInt32(); var result2 = rng.GetInt32(); Assert.That(result1, Is.Not.EqualTo(result2)); } }
private ValueTuple <byte[], byte[]> Random() { var range = RNGCryptoServiceProvider.GetInt32(128, Length); byte[] r1 = new byte[range]; byte[] r2 = new byte[Length - range]; RNGCryptoServiceProvider.Fill(r1); RNGCryptoServiceProvider.Fill(r2); return(ValueTuple.Create(r1, r2)); }
public void GenerateOneMillionInt32_WithRngProvider_ShouldBeUnique() { const int count = 10000; var numbersHolder = new int[count]; using var rngProvider = new RNGCryptoServiceProvider(); for (var i = 0; i < count; i++) { numbersHolder[i] = rngProvider.GetInt32(9); } numbersHolder.Should().NotBeEmpty().And.NotContainNulls().And.HaveCount(count).And.OnlyHaveUniqueItems(); }
public static string GenerateRandomString(int size) { string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*()_+{}|:\"<>?`-=[]\\;',./"; int length = chars.Length; StringBuilder salt = new StringBuilder(); for (int i = 0; i < size; i++) { int choosen = RNGCryptoServiceProvider.GetInt32(length); salt.Append(chars[choosen]); } return(salt.ToString()); }
protected internal int GetRandomInt32(int min, int max) { if (min > max) { throw new ArgumentOutOfRangeException(nameof(min), "Min was greater than max."); } if (min == max) { return(min); } int generatedValue = Math.Abs(_rng.GetInt32()); int diff = max - min + 1; int mod = generatedValue % diff; return(min + mod); }
private string GenerateSalt() { int size = RNGCryptoServiceProvider.GetInt32(1, 21); return(Utils.Utils.GenerateRandomString(size)); }