本文整理汇总了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类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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));
}
开发者ID:shenxuejin,项目名称:clrzmq4,代码行数:10,代码来源:ZActor.cs
示例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();
}
}
开发者ID:carmbrester,项目名称:Charm.Core,代码行数:38,代码来源:Randomizer.cs
示例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);
}
开发者ID:thanhphung901,项目名称:ketoan.app,代码行数:11,代码来源:Security.cs
示例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);
}
开发者ID:pusp,项目名称:o2platform,代码行数:7,代码来源:HttpMultipartMimeForm.cs
示例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);
}
}
}
开发者ID:ray-zong,项目名称:zguide,代码行数:29,代码来源:espresso.cs
示例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");
}
开发者ID:inorton,项目名称:testvault,代码行数:7,代码来源:ExampleTests.cs
示例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('/', '~');
}
开发者ID:invertedtomato,项目名称:Amos2,代码行数:28,代码来源:Tokens.cs
示例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();
}
开发者ID:rAum,项目名称:AntMaster,代码行数:13,代码来源:Ant.cs
示例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);
}
开发者ID:Vlanta,项目名称:CspBase,代码行数:7,代码来源:Program.cs
示例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);
}
开发者ID:grrizzly,项目名称:3D-Repository,代码行数:7,代码来源:Utility.cs
示例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();
}
}
开发者ID:alekseysukharev,项目名称:bank,代码行数:25,代码来源:RandomStringGenerator.cs
示例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);
}
开发者ID:br3ach3r,项目名称:projectbase,代码行数:8,代码来源:Encrypt.cs
示例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);
}
开发者ID:MiroPakanec,项目名称:EFlats,代码行数:8,代码来源:CtrHash.cs
示例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);
}
开发者ID:xescrp,项目名称:breinstormin,代码行数:9,代码来源:RandomEngine.cs
示例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;
}
开发者ID:jusbuc2k,项目名称:cornshare,代码行数:9,代码来源:Random.cs
示例16: SecureDelete
/// <summary>
/// Initialize the class
/// </summary>
public SecureDelete()
{
// get random buffer
using (System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
rng.GetBytes(_rndBuffer);
// create reverse random buffer
Buffer.BlockCopy(_rndBuffer, 0, _revBuffer, 0, _revBuffer.Length);
Array.Reverse(_revBuffer);
}
开发者ID:modulexcite,项目名称:CEX,代码行数:13,代码来源:SecureDelete.cs
示例17: Random
/// <summary>
/// 获取线程级随机数
/// </summary>
/// <returns></returns>
public static Random Random()
{
var bytes = new byte[4];
var rng =
new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
var seed = BitConverter.ToInt32(bytes, 0);
var tick = DateTime.Now.Ticks + (seed);
return new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
}
开发者ID:shoy160,项目名称:Shoy.Common,代码行数:14,代码来源:RandomHelper.cs
示例18: Customer
public Customer(string name, int customerId, ModuleEnum[] modules)
{
Name = name;
CustomerId = customerId;
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
Random r = new Random(rng.GetHashCode());
SatisfactionLevel = r.NextDouble() * 10;
Modules = modules;
}
开发者ID:fauser,项目名称:MarketSimulator,代码行数:11,代码来源:Customer.cs
示例19: GetRandomString
public static System.String GetRandomString(System.Int32 length)
{
System.Byte[] seedBuffer = new System.Byte[4];
using (var rngCryptoServiceProvider = new System.Security.Cryptography.RNGCryptoServiceProvider())
{
rngCryptoServiceProvider.GetBytes(seedBuffer);
System.String chars = "-abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.Random random = new System.Random(System.BitConverter.ToInt32(seedBuffer, 0));
return new System.String(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
}
}
开发者ID:damy90,项目名称:Telerik-all,代码行数:11,代码来源:Program.cs
示例20: createRandomString
public static string createRandomString()
{
//create Random Number Generator
var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
//create random string
byte[] randomBytes = new byte[32];
rng.GetBytes (randomBytes);
return byteArrayToHexString (randomBytes);
}
开发者ID:crypto-ink,项目名称:CryptoInkLib,代码行数:11,代码来源:Helpers.cs
注:本文中的System.Security.Cryptography.RNGCryptoServiceProvider类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论