Exemplo n.º 1
0
 /// <summary>
 /// Using the given lookup character array, reorders it using <see cref="DigestBasedRandomGenerator"/> with given parameters.
 /// </summary>
 /// <param name="chars">The lookup character array.</param>
 /// <param name="seed">The seed material to <see cref="DigestBasedRandomGenerator"/>, as <see cref="Byte"/> array.</param>
 /// <param name="algorithm">The <see cref="DigestAlgorithm"/> to use. If <c>null</c>, then <see cref="SHA512"/> will be used.</param>
 /// <param name="seedCycleCount">How often the seed will be re-digested.</param>
 /// <seealso cref="StringConversions.EncodeBinary(global::System.Byte[], global::System.Char[])"/>
 /// <seealso cref="UtilPackExtensions.Shuffle{T}(T[], Random)"/>
 public static void ShuffleBinaryEncodingCharactersFromSeed(
     Char[] chars,
     Byte[] seed,
     DigestAlgorithm algorithm = null,
     Int32 seedCycleCount      = 10
     )
 {
     using (var rng = new DigestBasedRandomGenerator(algorithm ?? new SHA512(), seedCycleCount: seedCycleCount, skipDisposeAlgorithm: false))
     {
         rng.AddSeedMaterial(seed);
         using (var secRandom = new SecureRandom(rng))
         {
             chars.Shuffle(secRandom);
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// This is convenience method to create new <see cref="DigestBasedRandomGenerator"/> and seed it with default, secure logic.
        /// </summary>
        /// <param name="algorithm">The algorithm that returned <see cref="DigestBasedRandomGenerator"/> should use.</param>
        /// <param name="seedCycleCount">How often to re-seed the state of returned <see cref="DigestBasedRandomGenerator"/>.</param>
        /// <param name="skipDisposeAlgorithm">Optional parameter controlling whether the <see cref="DigestBasedRandomGenerator"/> will, when disposed, dispose also the given <paramref name="algorithm"/>.</param>
        /// <returns>A new instance of <see cref="DigestBasedRandomGenerator"/> with given parameters.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="algorithm"/> is <c>null</c>.</exception>
        public static DigestBasedRandomGenerator CreateAndSeedWithDefaultLogic(
            BlockDigestAlgorithm algorithm,
            Int32 seedCycleCount         = DEFAULT_SEED_CYCLE_COUNT,
            Boolean skipDisposeAlgorithm = false
            )
        {
            var retVal = new DigestBasedRandomGenerator(algorithm, seedCycleCount, skipDisposeAlgorithm);

            // Use Guid as random source (should be version 4)
            retVal.AddSeedMaterial(Guid.NewGuid().ToByteArray());

            // Use current ticks
            retVal.AddSeedMaterial(DateTime.Now.Ticks);

            // Use Guid again
            retVal.AddSeedMaterial(Guid.NewGuid().ToByteArray());
            return(retVal);
        }