Example #1
0
        public static int GetInt32(this IRng rng, int minInclusive, int maxInclusive)
        {
            var range = Math.Abs(maxInclusive - minInclusive);
            var i     = rng.GetInt32(range);

            return(i + minInclusive);
        }
Example #2
0
 public virtual byte[] Encrypt(byte[] plain, IRng key)
 {
     //Well let's make something work, yes?
     byte[] keyGen = new byte[128];
     key.NextBytes(keyGen);
     return(Encrypt(plain, keyGen, ForwardOp));
 }
Example #3
0
        /// <summary>
        /// Genearate pseudo-random byte array of the specified length
        /// </summary>
        /// <param name="rng">pseudo-random number generator</param>
        /// <param name="length">length of the array to return</param>
        /// <returns>array of pseudo-random bytes</returns>
        public static byte[] NextBytes(this IRng rng, int length)
        {
            var ret = new byte[length];

            rng.NextBytes(ret);
            return(ret);
        }
 public FeatureInjectorPlugin(IContextImpl context)
     : base(context)
 {
     Rng = context.Plugin <IRngPlugin>().Get(NetfuserFactory.FeatureInjectorName);
     _injectableTypes = new List <InjectableType>();
     _ns = context.Plugin <INaming>();
 }
Example #5
0
        public static long GetInt64(this IRng rng, long minInclusive, long maxInclusive)
        {
            var range = Math.Abs(maxInclusive - minInclusive);
            var i     = rng.GetInt64(range);

            return(i + minInclusive);
        }
Example #6
0
 public void SetUpThreeDoors(IRng rng)
 {
     Doors = new List <Door> {
         new Door(), new Door(), new Door()
     };
     Doors[rng.Next(0, 3)].Prize = Prize.Car;
 }
Example #7
0
        public static void Decrypt(
            this ICypher self,
            Stream cypher,
            IRng key,
            Stream outPlain)
        {
            if (!cypher.CanRead)
            {
                throw new InvalidOperationException($"Input stream does not allow read");
            }
            if (!outPlain.CanWrite)
            {
                throw new InvalidOperationException($"Output stream does not allow write");
            }

            byte[] buff = new byte[1024];
            int    read;

            while ((read = cypher.Read(buff, 0, 1024)) != 0)
            {
                byte[] bytesRead  = buff.Take(read).ToArray();
                byte[] bytesWrite = self.Decrypt(bytesRead, key);
                outPlain.Write(bytesWrite, 0, bytesWrite.Length);
            }
        }
Example #8
0
        public override byte[] Encrypt(byte[] plain, byte[] key)
        {
            IRng          rng            = key.KeyToRand();
            Array2D <int> polybiusSquare = GetSeededPolybiusSquare(rng);

            return(Encrypt(plain, polybiusSquare));
        }
Example #9
0
        public override byte[] Decrypt(byte[] cypher, byte[] key)
        {
            IRng          rng            = key.KeyToRand();
            Array2D <int> polybiusSquare = GetSeededPolybiusSquare(rng);

            return(Decrypt(cypher, polybiusSquare));
        }
Example #10
0
        public static long NextInt(IRng rng, int bytes, bool signed)
        {
            long value;

            do
            {
                switch (bytes)
                {
                case 1:
                    value = signed ? (long)rng.NextInt8() : rng.NextUInt8();
                    break;

                case 2:
                    value = signed ? (long)rng.NextInt16() : rng.NextUInt16();
                    break;

                case 4:
                    value = signed ? (long)rng.NextInt32() : rng.NextUInt32();
                    break;

                case 8:
                    value = signed ? rng.NextInt64() : (long)rng.NextUInt64();
                    break;

                default: throw new NotSupportedException();
                }
            } while (EdgeCases.Contains(value));

            return(value);
        }
Example #11
0
        private TestResult TestRngDeterminism(Type rngType, out string message)
        {
            message = string.Empty;
            int[] seeds = { 0, 13, 23 };

            TestResult testRes = TestResult.Passed;

            foreach (int s in seeds)
            {
                IRng rng0 = (IRng)Activator.CreateInstance(rngType, s);

                byte[] bytes0 = new byte[256];
                rng0.NextBytes(bytes0);

                IRng rng1 = (IRng)Activator.CreateInstance(rngType, s);

                byte[] bytes1 = new byte[256];
                rng1.NextBytes(bytes1);

                if (!bytes0.SequenceEqual(bytes1))
                {
                    testRes &= TestResult.Failed;
                }
            }
            return(testRes);
        }
 public IntManglerPlugin(IContextImpl context)
     : base(context)
 {
     Rng         = context.Plugin <IRngPlugin>().Get(NetfuserFactory.IntManglerName);
     _finj       = context.FeatureInjector();
     _demanglers = new List <ValueDemangler>();
 }
Example #13
0
        private static void DfsMaze(
            Maze cells,
            int i,
            int j,
            IRng rng,
            List <Tuple <int, int> > visitOrder, bool
            print)
        {
            if (print)
            {
                Console.WriteLine(cells);
            }

            visitOrder.Add(new Tuple <int, int>(i, j));
            List <Tuple <int, int, Cell> > neighs = GetUnvistedNeighbours(cells, i, j);

            cells[i, j] |= Cell.Visited;
            while (neighs.Any())
            {
                var pickedNeigh = neighs[rng.Next(0, neighs.Count)];
                int x = pickedNeigh.Item1, y = pickedNeigh.Item2;
                cells[i, j] |= pickedNeigh.Item3;
                cells[x, y] |= GetOpposite(pickedNeigh.Item3);

                DfsMaze(cells, x, y, rng, visitOrder, print);
                neighs = GetUnvistedNeighbours(cells, i, j);
            }
        }
Example #14
0
 public (int, int) PlayAllGames(IRng doorToPlaceCar, IRng doorToChoose, double switchingChance)
 {
     Wins   = 0;
     Losses = 0;
     PlayGameAndGetScore(doorToPlaceCar, doorToChoose, switchingChance);
     return(Wins, Losses);
 }
Example #15
0
        public void RollDice(IEnumerable <Die> diceCup, IRng rng)
        {
            var enumerableDiceCup = diceCup as Die[] ?? diceCup.ToArray();

            if (RollsLeft > 0)
            {
                foreach (var die in enumerableDiceCup)
                {
                    if (die.IsHeld == false)
                    {
                        die.Roll(rng);
                    }
                }

                RollsLeft--;
            }
            else
            {
                throw new RoundOverException("You have run out of Rolls! Please choose a category");
            }

            foreach (var die in enumerableDiceCup)
            {
                die.IsHeld = false;
            }
        }
Example #16
0
        public static T SelectOne <T>(this IRng rng) where T : Enum
        {
            var values    = _enumValues.GetOrAdd(typeof(T), k => Enum.GetValues(k));
            var selection = rng.GetInt32(0, values.Length - 1);

            return((T)values.GetValue(selection));
        }
Example #17
0
        public override byte[] Encrypt(byte[] plain, byte[] key)
        {
            //Let's encrypt 8x8 or 16x16 at a time
            //Insert padding amount at the beginning of the cypher so we know how much to remove
            _matrixDimension = 8;
            //if (plain.Length >= 16 * 16)
            //    _matrixDimension = 16;
            _matrixSize = _matrixDimension * _matrixDimension;
            //fix this
            List <int> plainIxs = BytesToIndices(plain).ToList();

            //calculate fill
            int fill  = _matrixDimension - plain.Length % _matrixDimension;
            var noise = new SysRng();

            for (int i = 0; i < fill; ++i)
            {
                plainIxs.Add(noise.Next(0, Alphabet.Count));
            }

            List <int> cypherIxs = new List <int>(plainIxs.Count + 4);

            cypherIxs.AddRange(EncodeFill(fill, Alphabet.Count));

            IRng rng = key.KeyToRand();

            for (int i = 0; i < plainIxs.Count; i += _matrixDimension)
            {
                cypherIxs.AddRange(Encrypt(new ListSpan <int>(plainIxs, i, i + _matrixDimension), rng));
            }

            return(IndicesToBytes(cypherIxs));
        }
Example #18
0
        public override byte[] Decrypt(byte[] cypher, byte[] key)
        {
            int[] cypherIxs = BytesToIndices(cypher);
            int   fill      = DecodeFill(cypherIxs, Alphabet.Count);

            int originalPlainLength = cypher.Length - 4 - fill;

            //Let's encrypt 8x8 or 16x16 at a time
            //Insert padding amount at the beginning of the cypher so we know how much to remove
            _matrixDimension = 8;
            //if (plain.Length >= 16 * 16)
            //    _matrixDimension = 16;
            _matrixSize = _matrixDimension * _matrixDimension;

            var  plainIxs = new List <int>(cypherIxs.Length);
            IRng rng      = key.KeyToRand();

            for (int i = 4; i < cypherIxs.Length; i += _matrixDimension)
            {
                plainIxs.AddRange(Decrypt(new ListSpan <int>(cypherIxs, i, i + _matrixDimension), rng));
            }

            plainIxs.RemoveRange(plainIxs.Count - fill, fill);

            return(IndicesToBytes(plainIxs));
        }
Example #19
0
 /// <summary>
 /// Generate pseudo-random unsigned long within the given range (inclusive)
 /// </summary>
 /// <param name="rng">pseudo-random number generator</param>
 /// <returns>value from the [min, max] range</returns>
 /// <remarks>Note that both upper and lower boundaries of the range are inclusive</remarks>
 public static ulong NextUInt64(this IRng rng, ulong min, ulong max)
 {
     if (max <= min)
     {
         return(min);
     }
     return(min + rng.NextUInt64(max - min));
 }
Example #20
0
 /// <summary>
 /// Generate pseudo-random signed int within the given range (inclusive)
 /// </summary>
 /// <param name="rng">pseudo-random number generator</param>
 /// <returns>value from the [min, max] range</returns>
 /// <remarks>Note that both upper and lower boundaries of the range are inclusive</remarks>
 public static int NextInt32(this IRng rng, int min, int max)
 {
     if (max <= min)
     {
         return(min);
     }
     return((int)(rng.NextUInt32((uint)(min + Ovf32s), (uint)(max + Ovf32s)) - Ovf32s));
 }
Example #21
0
 /// <summary>
 /// Generate pseudo-random unsigned byte within the given range (inclusive)
 /// </summary>
 /// <param name="rng">pseudo-random number generator</param>
 /// <returns>value from the [min, max] range</returns>
 /// <remarks>Note that both upper and lower boundaries of the range are inclusive</remarks>
 public static byte NextUInt8(this IRng rng, byte min, byte max)
 {
     if (max <= min)
     {
         return(min);
     }
     return((byte)(min + rng.NextUInt8((byte)(max - min))));
 }
Example #22
0
        public static UInt64 LongRandom(this IRng rand)
        {
            byte[] buf = new byte[8];
            rand.NextBytes(buf);
            UInt64 longRand = BitConverter.ToUInt64(buf, 0);

            return(longRand);
        }
Example #23
0
        protected override byte[] Encrypt(byte[] plain, byte[] key, Op op)
        {
            IRng rand = key.KeyToRand();

            byte[] cypher = Encrypt(plain, rand, op);

            return(cypher);
        }
Example #24
0
 /// <summary>
 /// Generate pseudo-random nsigned byte within the given range (inclusive)
 /// </summary>
 /// <param name="rng">pseudo-random number generator</param>
 /// <returns>value from the [min, max] range</returns>
 /// <remarks>Note that both upper and lower boundaries of the range are inclusive</remarks>
 public static sbyte NextInt8(this IRng rng, sbyte min, sbyte max)
 {
     if (max <= min)
     {
         return(min);
     }
     return((sbyte)(rng.NextUInt8((byte)(min + Ovf8s), (byte)(max + Ovf8s)) - Ovf8s));
 }
Example #25
0
 public RefreshTokenService(IRefreshTokenRepository refreshTokenRepository,
                            IUserRepository userRepository, IJwtProvider jwtProvider, IRng rng)
 {
     _refreshTokenRepository = refreshTokenRepository;
     _userRepository         = userRepository;
     _jwtProvider            = jwtProvider;
     _rng = rng;
 }
Example #26
0
 /// <summary>
 /// Generate pseudo-random signed short within the given range (inclusive)
 /// </summary>
 /// <param name="rng">pseudo-random number generator</param>
 /// <returns>value from the [min, max] range</returns>
 /// <remarks>Note that both upper and lower boundaries of the range are inclusive</remarks>
 public static short NextInt16(this IRng rng, short min, short max)
 {
     if (max <= min)
     {
         return(min);
     }
     return((short)(rng.NextUInt16((ushort)(min + Ovf16s), (ushort)(max + Ovf16s)) - Ovf16s));
 }
Example #27
0
 public MetadataManglerPlugin(IContextImpl context, MetadataManglerOptions options)
     : base(context)
 {
     _options = options;
     _root    = new MetadataElement.Root();
     _rng     = context.Plugin <IRngPlugin>().Get(NetfuserFactory.MetadataManglerName);
     _ns      = context.Plugin <INaming>();
 }
Example #28
0
 /// <summary>
 /// Generate pseudo-random unsigned int within the given range (inclusive)
 /// </summary>
 /// <param name="rng">pseudo-random number generator</param>
 /// <returns>value from the [min, max] range</returns>
 /// <remarks>Note that both upper and lower boundaries of the range are inclusive</remarks>
 public static uint NextUInt32(this IRng rng, uint min, uint max)
 {
     if (max <= min)
     {
         return(min);
     }
     return(min + rng.NextUInt32(max - min));
 }
Example #29
0
    public KarhunenLoeve(ISde <double> stochasticEquation, int numSubdivisions, int nTruncated, IRng <double> randomGen)
        : base(stochasticEquation, numSubdivisions)
    {
        N   = nTruncated;
        rng = randomGen;

        tmp = Math.Sqrt(2.0 * sde.Expiry);
    }
Example #30
0
 /// <summary>
 /// Generate pseudo-random unsigned short within the given range (inclusive)
 /// </summary>
 /// <param name="rng">pseudo-random number generator</param>
 /// <returns>value from the [min, max] range</returns>
 /// <remarks>Note that both upper and lower boundaries of the range are inclusive</remarks>
 public static ushort NextUInt16(this IRng rng, ushort min, ushort max)
 {
     if (max <= min)
     {
         return(min);
     }
     return((ushort)(min + rng.NextUInt16((ushort)(max - min))));
 }