Exemplo n.º 1
0
 public static GuideV1 CreateGuide()
 {
     return(new GuideV1
     {
         Id = IdGenerator.NextLong(),
         Name = RandomText.Name(),
         Type = RandomGuideType(),
         App = RandomText.Phrase(1, 50),
         MinVer = RandomLong.NextLong(int.MaxValue),
         MaxVer = RandomLong.NextLong(int.MaxValue),
         Tags = new List <string>
         {
             RandomText.Stuff(), IdGenerator.NextLong(), RandomText.Color(), RandomText.Name(), RandomText.Phone(), RandomText.Adjective(), "tag1"
         },
         AllTags = new List <string>
         {
             RandomText.Stuff(), RandomText.Color(), RandomText.Name(), RandomText.Phone(), RandomText.Adjective()
         },
         Status = RandomText.Verb(),
         CreateTime = DateTime.UtcNow,
         Pages = new List <GuidePageV1>
         {
             new GuidePageV1
             {
                 Title = new Dictionary <string, string>
                 {
                     { IdGenerator.NextShort(), RandomText.Word() }, { IdGenerator.NextShort(), RandomText.Word() }
                 },
                 Content = new Dictionary <string, string>
                 {
                     { IdGenerator.NextShort(), RandomText.Word() }, { IdGenerator.NextShort(), RandomText.Word() }
                 },
                 Color = RandomText.Color(),
                 PicId = IdGenerator.NextShort(),
                 PicUri = IdGenerator.NextLong()
             },
             new GuidePageV1
             {
                 Title = new Dictionary <string, string>
                 {
                     { IdGenerator.NextShort(), RandomText.Word() }, { IdGenerator.NextShort(), RandomText.Word() }
                 },
                 Content = new Dictionary <string, string>
                 {
                     { IdGenerator.NextShort(), RandomText.Word() }, { IdGenerator.NextShort(), RandomText.Word() }
                 },
                 Color = RandomText.Color(),
                 PicId = IdGenerator.NextShort(),
                 PicUri = IdGenerator.NextLong()
             }
         }
     });
 }
Exemplo n.º 2
0
 public static BlobInfoV1 Blob()
 {
     return(new BlobInfoV1
     {
         Id = IdGenerator.NextLong(),
         Group = RandomText.Name(),
         Name = RandomText.Name(),
         Size = RandomLong.NextLong(100, 100000),
         ContentType = RandomArray.Pick(new string[] { "text/plain", "application/binary", "application/json" }),
         CreateTime = DateTime.UtcNow,
         ExpireTime = RandomDateTime.NextDateTime(DateTime.UtcNow, new DateTime(2010, 1, 1)),
         Completed = RandomBoolean.NextBoolean()
     });
 }
Exemplo n.º 3
0
 public PrimeGenerator(RandomLong randGenerator)
 {
     rand = new RandomLong(randGenerator);
 }
Exemplo n.º 4
0
        public void TestSubstraction3()
        {
            SLongIntB N = new SLongIntB("2945729752935981200000005151659293467923476293623");
            RandomLong rand = new RandomLong((ulong)DateTime.Now.Millisecond);

            for (int i = 0; i < 1000; ++i)
            {
                SLongIntB A = new SLongIntB(rand.Next(N), ConstructorMode.Assign);
                SLongIntB B = new SLongIntB(rand.Next(N), ConstructorMode.Assign);

                SLongIntB C = A - B;

                Assert.AreEqual(C + B, A);
                Assert.AreEqual(LongMath.Abs(C - A), B);

                Assert.AreEqual(C - 0, C);
                Assert.AreEqual(C - C, (SLongIntB)0);
            }
        }
Exemplo n.º 5
0
        public void TestSubstraction1()
        {
            SLongIntB N = new SLongIntB("2945729752935981200000005151659293467923476293623");
            RandomLong rand = new RandomLong((ulong)DateTime.Now.Millisecond);

            for (int i = 0; i < 1000; ++i)
            {
                SLongIntB A = new SLongIntB(rand.Next(N), ConstructorMode.Assign);
                SLongIntB B = new SLongIntB(rand.Next(N), ConstructorMode.Assign);

                SLongIntB C = A + B;

                Assert.AreEqual(C - B, A, C.ToString() + " " + B.ToString());
                Assert.AreEqual(C - A, B, C.ToString() + " " + A.ToString());

                Assert.AreEqual(C - 0, C);
                Assert.AreEqual(C - C, (SLongIntB)0);
            }
        }
Exemplo n.º 6
0
        public void TestSquare2()
        {
            SLongIntB N = new SLongIntB("2945729752935981200000005151659293467923476293623");
            RandomLong rand = new RandomLong((ulong)DateTime.Now.Millisecond);

            for (int i = 0; i < 1000; ++i)
            {
                SLongIntB temp = new SLongIntB(rand.Next(N), ConstructorMode.Assign);
                Assert.AreEqual(temp*temp, LongMath.Sqr(temp));
            }
        }
Exemplo n.º 7
0
        public void TestShiftRight()
        {
            SLongIntB N = new SLongIntB("2945729752935981200000005151659293467923476293623");
            RandomLong rand = new RandomLong((ulong)DateTime.Now.Millisecond);

            for (int i = 0; i < 1000; ++i)
            {
                SLongIntB temp1 = new SLongIntB(rand.Next(N), ConstructorMode.Assign);
                SLongIntB temp3 = new SLongIntB(temp1);

                temp1.Shr();

                SLongIntB temp2 = new SLongIntB(temp1);
                temp2.Shl();

                Assert.AreEqual(temp1 * 2, temp2);
                Assert.AreEqual(temp1, temp3 / 2);
            }
        }
Exemplo n.º 8
0
        public void TestPower()
        {
            SLongIntB N = new SLongIntB("2945");
            RandomLong rand = new RandomLong((ulong)DateTime.Now.Millisecond);
            Random rand32 = new Random(DateTime.Now.Millisecond);

            SLongIntB m = new SLongIntB("98696766574783");

            for (int i = 0; i < 100; ++i)
            {
                SLongIntB temp = new SLongIntB(rand.Next(N), ConstructorMode.Assign);
                int power = rand32.Next(ushort.MaxValue) % 10000;

                Assert.AreEqual(CryptoMath.ExpMod5((ULongIntB)temp, (ULongIntB)power, (ULongIntB)m), LongMath.Exp((ULongIntB)temp, (ulong)power) % (ULongIntB)m,
                                temp.ToString() + "^" + power.ToString());
            }
        }
Exemplo n.º 9
0
        public void TestMul1()
        {
            SLongIntB N = new SLongIntB("2945729752935981200000005151659293467923476293623");
            RandomLong rand = new RandomLong((ulong)DateTime.Now.Millisecond);

            for (int i = 0; i < 1000; ++i)
            {
                SLongIntB temp1 = new SLongIntB(rand.Next(N), ConstructorMode.Assign);
                SLongIntB temp2 = new SLongIntB(rand.Next(N), ConstructorMode.Assign);
                SLongIntB temp3 = new SLongIntB(rand.Next(N), ConstructorMode.Assign);

                Assert.AreEqual((temp1*temp2)*temp3, temp1*(temp2*temp3));
            }
        }
Exemplo n.º 10
0
        //[Test()]
        public void TestExtendedGCD()
        {
            SLongIntB N = new SLongIntB("2945729752935981200000005151659293467923476293623");
            RandomLong rand = new RandomLong((ulong)DateTime.Now.Millisecond);

            SLongIntB U = null, V = null;

            for (int i = 0; i < 1000; ++i)
            {
                SLongIntB temp1 = new SLongIntB(rand.Next(N), ConstructorMode.Assign);
                SLongIntB temp2 = new SLongIntB(rand.Next(N), ConstructorMode.Assign);

                SLongIntB gcd = CryptoMath.eXtendedGCD(temp1, temp2, out U, out V);
                SLongIntB gcdB = CryptoMath.GCD(temp1, temp2);

                Assert.AreEqual(gcd, temp1*U + temp2*V);
                Assert.AreEqual(gcd, gcdB, temp1.ToString() + " " + temp2.ToString() + " " + gcd.ToString() + " " + gcdB.ToString());
            }
        }
Exemplo n.º 11
0
        public void TestDivision2()
        {
            SLongIntB N = new SLongIntB("2945729752935981200000005151659293467923476293623");
            RandomLong rand = new RandomLong((ulong)DateTime.Now.Millisecond);

            for (int i = 0; i < 1000; ++i)
            {
                SLongIntB A = new SLongIntB(rand.Next(N), ConstructorMode.Assign);
                SLongIntB B = new SLongIntB(rand.Next(N), ConstructorMode.Assign);

                if (B == 0)
                    continue;

                if ((B * (A / B)) + (A % B) != A)
                {
                    Console.WriteLine(A);
                    Console.WriteLine(B);

                    Console.WriteLine((A/B).ToString());
                    Console.WriteLine((A%B).ToString());
                }

                Assert.AreEqual((B * (A / B)) + (A % B), A, string.Join(" ",
                        new string[]{ A.ToString(), B.ToString(), (A/B).ToString(), (A%B).ToString() }));
            }
        }
Exemplo n.º 12
0
        public void TestDivision1()
        {
            SLongIntB N = new SLongIntB("2945729752935981200000005151659293467923476293623");
            RandomLong rand = new RandomLong((ulong)DateTime.Now.Millisecond);

            for (int i = 0; i < 1000; ++i)
            {
                SLongIntB A = new SLongIntB(rand.Next(N), ConstructorMode.Assign);
                SLongIntB B = new SLongIntB(rand.Next(N), ConstructorMode.Assign);

                SLongIntB C = A*B;

                if (C == 0)
                    continue;

                Assert.AreEqual(C / B, A);
                Assert.AreEqual(C / A, B);
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Test if input number is prime
        /// </summary>
        /// <param name="n">Input number</param>
        /// <param name="rounds">Number of times to provide test</param>
        /// <returns>Exact answer if input number is composite, and 
        /// a probable answer if input number can be prime. Probability of mistake
        /// answer is (0.25)^(Rounds number)</returns>
        public static PrimeTestResult TestPrimeMillerRabin(ULongIntB n, RoundsNumber rounds)
        {
            int r = TestSmallPrimes(n, 2000);
            if (r != 0)
                return PrimeTestResult.Composite;

            ULongIntB Nm1 = new ULongIntB(n);
            --Nm1;

            int t = 0;
            ULongIntB q = null;
            TwoFact(Nm1, out q, out t);

            RandomLong rand = new RandomLong((ulong)DateTime.Now.Millisecond);

            for (int i = 0; i < (int)rounds; ++i)
            {
                ULongIntB a = new ULongIntB(rand.NextNotOne(Nm1), ConstructorMode.Assign);
                ULongIntB b = ExpMod5(a, q, n);

                if ((b == 1) || b == Nm1)
                    continue;

                bool next = false;

                for (int j = 0; j < t - 1; ++j)
                {
                    b = SquareMod(b, n);
                    if (b == 1)
                        return PrimeTestResult.Composite;

                    if (b == Nm1)
                    {
                        next = true;
                        break;
                    }
                }

                if (!next)
                    return PrimeTestResult.Composite;
            }

            return PrimeTestResult.DontKnow;
        }
Exemplo n.º 14
0
 public RSAKeyGenerator(ulong seed)
 {
     rand = new RandomLong(seed);
     prGen = new PrimeGenerator(rand);
 }