本文整理汇总了C#中System.Security.Cryptography.RNGCryptoServiceProvider类的典型用法代码示例。如果您正苦于以下问题:C# System.Security.Cryptography.RNGCryptoServiceProvider类的具体用法?C# System.Security.Cryptography.RNGCryptoServiceProvider怎么用?C# System.Security.Cryptography.RNGCryptoServiceProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
System.Security.Cryptography.RNGCryptoServiceProvider类属于命名空间,在下文中一共展示了System.Security.Cryptography.RNGCryptoServiceProvider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ZActor
/// <summary>
/// You are using ZContext.Current!
/// </summary>
public ZActor(ZAction0 action, params object[] args)
: this(default(string), action, args)
{
var rnd0 = new byte[8];
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) rng.GetNonZeroBytes(rnd0);
this.Endpoint = string.Format("inproc://{0}", ZContext.Encoding.GetString(rnd0));
}
示例2: GenerateRandomString
/// <remarks>
/// http://stackoverflow.com/questions/730268/unique-random-string-generation
/// </remarks>
string GenerateRandomString(int length,
string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
{
if (length < 0) throw new ArgumentOutOfRangeException(nameof(length), "length cannot be less than zero.");
if (string.IsNullOrEmpty(allowedChars)) throw new ArgumentException("allowedChars may not be empty.");
const int byteSize = 0x100;
var allowedCharSet = new HashSet<char>(allowedChars).ToArray();
if (byteSize < allowedCharSet.Length)
throw new ArgumentException($"allowedChars may contain no more than {byteSize} characters.");
// Guid.NewGuid and System.Random are not particularly random. By using a
// cryptographically-secure random number generator, the caller is always
// protected, regardless of use.
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
{
var result = new StringBuilder();
var buf = new byte[128];
while (result.Length < length)
{
rng.GetBytes(buf);
for (var i = 0; i < buf.Length && result.Length < length; ++i)
{
// Divide the byte into allowedCharSet-sized groups. If the
// random value falls into the last group and the last group is
// too small to choose from the entire allowedCharSet, ignore
// the value in order to avoid biasing the result.
var outOfRangeStart = byteSize - (byteSize%allowedCharSet.Length);
if (outOfRangeStart <= buf[i]) continue;
result.Append(allowedCharSet[buf[i]%allowedCharSet.Length]);
}
}
return result.ToString();
}
}
示例3: CreateSalt
/// <summary>
/// Create string salt
/// </summary>
/// <param name="grv">Create string salt</param>
public static string CreateSalt()
{
byte[] bytSalt = new byte[9];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytSalt);
return Convert.ToBase64String(bytSalt);
}
示例4: GetNonceAsHexDigitString
static string GetNonceAsHexDigitString(int lengthInBytes)
{
var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
var bytes = new byte[lengthInBytes];
rng.GetBytes(bytes);
return ToHexDigitString(bytes);
}
示例5: Espresso_Publisher
static void Espresso_Publisher(ZContext context)
{
// The publisher sends random messages starting with A-J:
using (var publisher = new ZSocket(context, ZSocketType.PUB))
{
publisher.Bind("tcp://*:6000");
ZError error;
while (true)
{
var bytes = new byte[5];
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
{
rng.GetBytes(bytes);
}
if (!publisher.SendBytes(bytes, 0, bytes.Length, ZSocketFlags.None, out error))
{
if (error == ZError.ETERM)
return; // Interrupted
throw new ZException(error);
}
Thread.Sleep(1);
}
}
}
示例6: TestOccasaionallyIgnored
public void TestOccasaionallyIgnored()
{
var rnd = new System.Security.Cryptography.RNGCryptoServiceProvider();
var buf = new byte[1];
rnd.GetBytes(buf);
if ( buf[0] > 100 ) Assert.Ignore("too big");
}
示例7: Generate
public static string Generate()
{
// Generate random
var rnd = new System.Security.Cryptography.RNGCryptoServiceProvider();
var entropy = new byte[bytes - 4];
try {
rnd.GetBytes(entropy);
} finally {
rnd.Dispose();
}
// Hash
var sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();
byte[] hash;
try {
hash = sha.ComputeHash(entropy);
} finally {
sha.Dispose();
}
// Compute output
var raw = new byte[bytes];
Array.Copy(entropy, 0, raw, 0, bytes - 4);
Array.Copy(hash, 0, raw, bytes - 4, 4);
// Convert to Base64
return Convert.ToBase64String(raw).Replace('+', '!').Replace('/', '~');
}
示例8: Ant
System.Security.Cryptography.RNGCryptoServiceProvider rnd; // generator liczb pseudolosowych
#endregion Fields
#region Constructors
public Ant()
{
mSize = 0;
mPath = new List<int>();
freeTasks = new List<int>();
rnd = new System.Security.Cryptography.RNGCryptoServiceProvider();
}
示例9: GetRandomSeed
static int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
示例10: GenerateNewSalt
public static string GenerateNewSalt()
{
byte[] saltInBytes = new byte[16];
System.Security.Cryptography.RNGCryptoServiceProvider saltGenerator = new System.Security.Cryptography.RNGCryptoServiceProvider();
saltGenerator.GetBytes(saltInBytes);
return Convert.ToBase64String(saltInBytes);
}
示例11: GetNext
public string GetNext(int length, char[] charSet)
{
if (length < 0)
{
throw new ArgumentOutOfRangeException("length", "Length cannot be less than zero.");
}
const int byteSize = 0x100;
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
{
var result = new StringBuilder();
var buf = new byte[128];
while (result.Length < length)
{
rng.GetBytes(buf);
for (var i = 0; i < buf.Length && result.Length < length; ++i)
{
var outOfRangeStart = byteSize - (byteSize % charSet.Length);
if (outOfRangeStart <= buf[i]) continue;
result.Append(charSet[buf[i] % charSet.Length]);
}
}
return result.ToString();
}
}
示例12: CreateSalt
public string CreateSalt()
{
int size = 10;//new Random().Next(10, 15);
var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
var buff = new byte[size];
rng.GetBytes(buff);
return Convert.ToBase64String(buff);
}
示例13: CreateSalt
protected string CreateSalt(int size)
{
var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
var buff = new byte[size];
rng.GetBytes(buff);
return Convert.ToBase64String(buff);
}
示例14: GetRandomNumber
public static int GetRandomNumber(int min, int max)
{
System.Security.Cryptography.RNGCryptoServiceProvider rng =
new System.Security.Cryptography.RNGCryptoServiceProvider();
byte[] buffer = new byte[4];
rng.GetBytes(buffer);
int result = BitConverter.ToInt32(buffer, 0);
return new System.Random(result).Next(min, max);
}
示例15: GetRandomBytes
public static byte[] GetRandomBytes(int length)
{
var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
var buffer = new byte[length];
rng.GetBytes(buffer);
return buffer;
}