Пример #1
0
        /// <summary>
        /// Перемешать порядок ответов.
        /// </summary>
        public static void Shuffle(QuestCase quest)
        {
            System.Security.Cryptography.RNGCryptoServiceProvider provider = new System.Security.Cryptography.RNGCryptoServiceProvider();
            KeyValuePair <string, bool>[] list = new KeyValuePair <string, bool> [quest.Answers.Count];

            int x = 0;

            foreach (KeyValuePair <string, bool> v in quest.Answers)
            {
                list[x++] = new KeyValuePair <string, bool>(v.Key, v.Value);
            }
            int n = list.Length - 1;

            while (n > 1)
            {
                byte[] box = new byte[1];
                do
                {
                    provider.GetBytes(box);
                }while (!(box[0] < n * (Byte.MaxValue / n)));
                int k = (box[0] % n);
                KeyValuePair <string, bool> value = new KeyValuePair <string, bool>(list[k].Key, list[k].Value);
                list[k] = list[n];
                list[n] = value;
                n--;
            }
            quest.Answers.Clear();
            foreach (var kvp in list)
            {
                quest.Answers.Add(kvp.Key, kvp.Value);
            }
            provider.Dispose();
        }
Пример #2
0
        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('/', '~');
        }
Пример #3
0
        public static byte[] RNG(int length)
        {
            byte[] random = new byte[length];
            var    rng    = new System.Security.Cryptography.RNGCryptoServiceProvider();

            rng.GetBytes(random);
            rng.Dispose();

            return(random);
        }
Пример #4
0
        public static string CreateHash(int length = 8)
        {
            byte[] random = new byte[256];
            var    rng    = new System.Security.Cryptography.RNGCryptoServiceProvider();

            rng.GetBytes(random);
            rng.Dispose();

            return(ComputeHash(random, length));
        }
Пример #5
0
        private bool disposedValue = false; // To detect redundant calls

        private void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    randomSource.Dispose();
                    randomSource = null;
                }

                disposedValue = true;
            }
        }
Пример #6
0
        /// <summary>
        /// Вернет случайное число от мин до мах
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static int RandomInt(int min, int max)
        {
            System.Security.Cryptography.RNGCryptoServiceProvider provider = new System.Security.Cryptography.RNGCryptoServiceProvider();
            byte[] random = new byte[1];

            //Заполнить массив случайными байтами
            provider.GetBytes(random);

            //случайное значение от 0 до 1
            double multypleer = (random[0] / 255d);
            double diff       = max - min;
            int    result     = (int)(min + Math.Floor(multypleer * diff));

            provider.Dispose();
            return(result);
        }
Пример #7
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (!disposing)
                {
                    return;
                }

                _internalRNGCSP.Dispose();
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
Пример #8
0
        protected virtual void Dispose(bool disposing)
        {
            if (!_is_disposed)
            {
                if (disposing)
                {
                    // マネージドリソースの解放
                    if (_engine != null)
                    {
                        _engine.Dispose();
                        _engine = null;
                    }
                }

                // アンマネージドリソースの解放

                _is_disposed = true;
            }
        }