public CASTCredential(Hmac hmac, HmacContent content)
 {
     this.Hmac = new HmacCredential(hmac, content);
 }
Пример #2
0
        public int SaveProgress(ProgressData progress)
        {
            //if connection is already invalidated
            if (!ValidityMap.CurrentInstance.Contains(Context.ConnectionId) ||
                !ValidityMap.CurrentInstance[Context.ConnectionId])
            {
                Debug.WriteLine("INVALID CONNECTION");
                return(-1);
            }


            string dataRoot = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
            //read cookie
            string c = AESCryptoStuff.CurrentInstance.AesDecrypt(HttpUtility.UrlDecode(Context.RequestCookies["userID"].Value));

            //current time in UTC
            long utcTime = (long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds;
            Hmac h       = Hmac.CurrentInstance;

            #region SaveTime limit to 3 / min
            //check database if it's too soon
            Database d = Database.CurrentInstance; long[] times = new long[3];
            //PRQ stands for Parameterized Reader Query, it returns a DataTable with all the rows
            //First argument is the query, every argument after that is the parameters
            //The @ parameters MUST START FROM 1 COUNTS UP FROM THERE
            //you can have any number of @ parameters and corresponding method arguments for the values
            DataTable dt = d.PRQ("SELECT * FROM savetime WHERE userID = @1", c);
            if (dt == null)
            {
                return(-2);            //if database not up
            }
            if (dt.Rows.Count > 0)
            {
                //if you want to loop
                //foreach(DataRow dr in dt.Rows)
                DataRow dr = dt.Rows[0];
                //Field method returns the value of the column specified in the type in the angle brackets
                times[0] = dr.Field <long>("time1");
                times[1] = dr.Field <long>("time2");
                times[2] = dr.Field <long>("time3");
            }
            else
            {
                //PNQ stands for Parameterized Non Query, it returns nothing
                d.PNQ("INSERT INTO savetime (userID, time1, time2, time3) VALUES (@1, @2, @3, @4)",
                      c, 0, 0, utcTime);
                times = new long[] { 0, 0, utcTime };
            }
            if (utcTime - times[0] < 60000) //4th save in a minute
            {
                return(0);
            }
            else
            {
                times[0] = utcTime;
                Array.Sort(times);
                d.PNQ("UPDATE savetime SET time1 = @1, time2 = @2, time3 = @3 WHERE userID = @4",
                      times[0], times[1], times[2], c);
            }
            #endregion

            //get previous save data
            SaveFile prevSave;
            try
            {
                string prevSaveText = System.IO.File.ReadAllText(
                    dataRoot + "\\Saves\\" + h.Encode(c) + ".tusav");
                prevSave = SaveFile.Parse(prevSaveText);
            } catch (FileNotFoundException e)        //no existing save
            {
                long defaultTime = utcTime - 100000; //100 seconds leeway
                prevSave = new SaveFile(defaultTime, 0,
                                        new Dictionary <int, int>(), new int[] { });
            }

            bool noCheats = ProgressVerifier.VerifyProgress(prevSave, progress, utcTime);
            Debug.WriteLine("IS CHEATING: " + !noCheats);
            //verify progress
            if (!noCheats)
            {
                //if caught cheating
                //insert cheat record into database
                d.PNQ("INSERT INTO cheatlog (userID, time) VALUES (@1, @2)", c, utcTime);
                ValidityMap.CurrentInstance[Context.ConnectionId] = false;
                return(-1);
            }

            //save + time on first line
            string s = "" + utcTime
                       + '\n' + progress.ToString();
            Debug.WriteLine("SAVING FOR " + h.Encode(c) + ":\n" + s);

            //write to file
            System.IO.File.WriteAllText(
                dataRoot + "\\Saves\\" + h.Encode(c) + ".tusav", s);

            return(1);
        }
Пример #3
0
        /**
         * Base constructor.
         *
         * @param digest digest to build the HMAC on.
         */

        public HmacDsaKCalculator(IHash digest)
        {
            this._hmac = new Hmac(digest);
            this._v    = new byte[_hmac.OutputSize];
            this._k    = new byte[_hmac.OutputSize];
        }
Пример #4
0
		public static string Get(byte[] data, Type type, byte[] key = null)
		{
			if (type == Type.QuickHash)
				return Coder.BytesToString(ComputeQuickHash(data), Coder.CodePage.Hex);

			var hashAlg = GetHashAlgorithm(type);
			if ((key != null) && (hashAlg is BlockHashAlgorithm))
				hashAlg = new Hmac(hashAlg as BlockHashAlgorithm, key);
			return Coder.BytesToString(hashAlg.ComputeHash(data), Coder.CodePage.Hex);
		}
Пример #5
0
        //gets a save file and sends it to the client
        public string RequestSave(string code)
        {
            Hmac h = Hmac.CurrentInstance;

            //read cookie
            Debug.WriteLine(Context.RequestCookies["userID"].Value);
            string c = HttpUtility.UrlDecode(AESCryptoStuff.CurrentInstance.AesDecrypt(Context.RequestCookies["userID"].Value));

            #region Check access code
            DataTable dt = Database.CurrentInstance.PRQ(
                "SELECT code FROM saveaccess WHERE userID = @1", c);
            if (dt.Rows.Count == 0)
            {
                return("invalid:No access code");
            }
            bool validCode = false; code = HttpUtility.HtmlDecode(code);
            foreach (DataRow r in dt.Rows)
            {
                if (r.Field <string>("code") == code)
                {
                    validCode = true;
                }
            }
            if (!validCode)
            {
                return("invalid:Wrong access code");
            }
            Database.CurrentInstance.PNQ(
                "DELETE FROM saveaccess WHERE userID = @1", c);
            ValidityMap.CurrentInstance.Add(Context.ConnectionId, true);
            #endregion

            if (c == null || c == "guest")
            {
                return("invalid:No username attached");
            }
            else
            {
                Debug.Write("GETTING SAVE FILE OF: " + c);
                string dataRoot = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
                Debug.WriteLine(dataRoot);
                string saveFileLocation = dataRoot + "\\Saves\\" + h.Encode(c) + ".tusav";
                if (File.Exists(saveFileLocation))
                {
                    //get savefile
                    string s = System.IO.File.ReadAllText(saveFileLocation);

                    //remove time from save
                    string[] saveParts = s.Split('\n');

                    //get current time
                    long utcTime = (long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds;

                    //send save to player
                    if (s[0] == '{') //convert old format to new format
                    {
                        System.IO.File.WriteAllText(
                            dataRoot + "\\Saves\\" + h.Encode(c) + ".tusav", "" + utcTime + "\n" + s.Replace("\n", ""));
                        return(s);
                    }
                    else
                    {
                        System.IO.File.WriteAllText(
                            dataRoot + "\\Saves\\" + h.Encode(c) + ".tusav", "" + utcTime + "\n" + saveParts[1]);
                        return(saveParts[1]);
                    }
                }
                else
                {
                    return(null);
                }
            }
        }
Пример #6
0
 protected override BitString PseudoRandomFunction(BitString ni, BitString nr, BitString gxy = null, BitString cky_i = null, BitString cky_r = null, BitString presharedKey = null)
 {
     var key = _sha.HashMessage(ni.ConcatenateBits(nr)).Digest;
     return Hmac.Generate(key, cky_i.ConcatenateBits(cky_r)).Mac;
 }
Пример #7
0
 /// <summary>
 /// Create an instance of Security
 /// </summary>
 public Security()
 {
     Aes  = new Aesgcm();
     RSA  = new RSA();
     Hmac = new Hmac();
 }
Пример #8
0
        public void HashSizeTest(BlockHashAlgorithm hasher)
        {
            Hmac hmac = new Hmac(hasher);

            Assert.AreEqual(hasher.HashSize, hmac.HashSize);
        }
Пример #9
0
 public void ConstructorNullHasherTest()
 {
     Hmac hmac = new Hmac(null);
 }
Пример #10
0
 public RequestAuth(Hmac hmac)
 {
     Hmac = hmac;
     Validate();
 }
Пример #11
0
        ///
        /// Single block with a single transaction, in an immutable chain.
        /// Standard SHA-256 Hashing
        ///
        static void Main(string[] args)
        {
            //BlockChain chain = new BlockChain();
            //IBlock block1 = new Block(0, "ABC123", 1000.00m, DateTime.Now, "QWE123", 10000, ClaimType.TotalLoss, null);
            //IBlock block2 = new Block(1, "VBG345", 2000.00m, DateTime.Now, "JKH567", 20000, ClaimType.TotalLoss, block1);
            //IBlock block3 = new Block(2, "XCF234", 3000.00m, DateTime.Now, "DH23ED", 30000, ClaimType.TotalLoss, block2);
            //IBlock block4 = new Block(3, "CBHD45", 4000.00m, DateTime.Now, "DH34K6", 40000, ClaimType.TotalLoss, block3);
            //IBlock block5 = new Block(4, "AJD345", 5000.00m, DateTime.Now, "28FNF4", 50000, ClaimType.TotalLoss, block4);
            //IBlock block6 = new Block(5, "QAX367", 6000.00m, DateTime.Now, "FJK676", 60000, ClaimType.TotalLoss, block5);
            //IBlock block7 = new Block(6, "CGO444", 7000.00m, DateTime.Now, "LKU234", 70000, ClaimType.TotalLoss, block6);
            //IBlock block8 = new Block(7, "PLO254", 8000.00m, DateTime.Now, "VBN456", 80000, ClaimType.TotalLoss, block7);

            //chain.AcceptBlock(block1);
            //chain.AcceptBlock(block2);
            //chain.AcceptBlock(block3);
            //chain.AcceptBlock(block4);
            //chain.AcceptBlock(block5);
            //chain.AcceptBlock(block6);
            //chain.AcceptBlock(block7);
            //chain.AcceptBlock(block8);

            //chain.VerifyChain();

            //Console.WriteLine("");
            //Console.WriteLine("");

            //block4.CreatedDate = new DateTime(2017, 09, 20);

            //chain.VerifyChain();

            //Console.WriteLine();

            // ITransaction txn1 = new Transaction("ABC123", 1000.00m, DateTime.Now, "QWE123", 10000, ClaimType.TotalLoss);
            // ITransaction txn2 = new Transaction("VBG345", 2000.00m, DateTime.Now, "JKH567", 20000, ClaimType.TotalLoss);
            // ITransaction txn3 = new Transaction("XCF234", 3000.00m, DateTime.Now, "DH23ED", 30000, ClaimType.TotalLoss);
            // ITransaction txn4 = new Transaction("CBHD45", 4000.00m, DateTime.Now, "DH34K6", 40000, ClaimType.TotalLoss);
            // ITransaction txn5 = new Transaction("AJD345", 5000.00m, DateTime.Now, "28FNF4", 50000, ClaimType.TotalLoss);
            // ITransaction txn6 = new Transaction("QAX367", 6000.00m, DateTime.Now, "FJK676", 60000, ClaimType.TotalLoss);
            // ITransaction txn7 = new Transaction("CGO444", 7000.00m, DateTime.Now, "LKU234", 70000, ClaimType.TotalLoss);
            // ITransaction txn8 = new Transaction("PLO254", 8000.00m, DateTime.Now, "VBN456", 80000, ClaimType.TotalLoss);
            // ITransaction txn9 = new Transaction("ABC123", 1000.00m, DateTime.Now, "QWE123", 10000, ClaimType.TotalLoss);
            // ITransaction txn10 = new Transaction("VBG345", 2000.00m, DateTime.Now, "JKH567", 20000, ClaimType.TotalLoss);
            // ITransaction txn11 = new Transaction("XCF234", 3000.00m, DateTime.Now, "DH23ED", 30000, ClaimType.TotalLoss);
            // ITransaction txn12 = new Transaction("CBHD45", 4000.00m, DateTime.Now, "DH34K6", 40000, ClaimType.TotalLoss);
            // ITransaction txn13 = new Transaction("AJD345", 5000.00m, DateTime.Now, "28FNF4", 50000, ClaimType.TotalLoss);
            // ITransaction txn14 = new Transaction("QAX367", 6000.00m, DateTime.Now, "FJK676", 60000, ClaimType.TotalLoss);
            // ITransaction txn15 = new Transaction("CGO444", 7000.00m, DateTime.Now, "LKU234", 70000, ClaimType.TotalLoss);
            // ITransaction txn16 = new Transaction("PLO254", 8000.00m, DateTime.Now, "VBN456", 80000, ClaimType.TotalLoss);


            // IBlock block1 = new BlockWithMultipleTransactions.Block(0);
            // IBlock block2 = new BlockWithMultipleTransactions.Block(1);
            // IBlock block3 = new BlockWithMultipleTransactions.Block(2);
            // IBlock block4 = new BlockWithMultipleTransactions.Block(3);

            // block1.AddTransaction(txn1);
            // block1.AddTransaction(txn2);
            // block1.AddTransaction(txn3);
            // block1.AddTransaction(txn4);

            // block2.AddTransaction(txn5);
            // block2.AddTransaction(txn6);
            // block2.AddTransaction(txn7);
            // block2.AddTransaction(txn8);

            // block3.AddTransaction(txn9);
            // block3.AddTransaction(txn10);
            // block3.AddTransaction(txn11);
            // block3.AddTransaction(txn12);

            // block4.AddTransaction(txn13);
            // block4.AddTransaction(txn14);
            // block4.AddTransaction(txn15);
            // block4.AddTransaction(txn16);

            // block1.SetBlockHash(null);
            // block2.SetBlockHash(block1);
            // block3.SetBlockHash(block2);
            // block4.SetBlockHash(block3);


            //BlockWithMultipleTransactions.BlockChain chain = new BlockWithMultipleTransactions.BlockChain();
            // chain.AcceptBlock(block1);
            // chain.AcceptBlock(block2);
            // chain.AcceptBlock(block3);
            // chain.AcceptBlock(block4);


            // chain.VerifyChain();

            // Console.WriteLine("");
            // Console.WriteLine("");

            // txn5.ClaimNumber = "weqwewe";
            // chain.VerifyChain();

            // Console.WriteLine();

            ITransaction txn5     = SetupTransactions();
            IKeyStore    keyStore = new KeyStore(Hmac.GenerateKey());

            IBlock block1 = new Block(0, keyStore);
            IBlock block2 = new Block(1, keyStore);
            IBlock block3 = new Block(2, keyStore);
            IBlock block4 = new Block(3, keyStore);

            AddTransactionsToBlocksAndCalculateHashes(block1, block2, block3, block4);

            BlockChain chain = new BlockChain();

            chain.AcceptBlock(block1);
            chain.AcceptBlock(block2);
            chain.AcceptBlock(block3);
            chain.AcceptBlock(block4);

            chain.VerifyChain();

            Console.WriteLine("");
            Console.WriteLine("");

            txn5.ClaimNumber = "weqwewe";
            chain.VerifyChain();

            Console.WriteLine();
        }
 public HmacCredential(Hmac hmac, HmacContent content)
 {
     this.Hmac = hmac;
     this.HmacContent  = content;
     this.Validate();
 }