当前位置: 首页>>代码示例>>C#>>正文


C# Cryptography.RNGCryptoServiceProvider类代码示例

本文整理汇总了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));
		}
开发者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


注:本文中的System.Security.Cryptography.RNGCryptoServiceProvider类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。