GenerateHash() public static method

public static GenerateHash ( byte data ) : byte[]
data byte
return byte[]
Example #1
0
        void DisptachPacket(byte[] packet)
        {
            MemoryStream stream = new MemoryStream(packet);
            BinaryReader br     = new BinaryReader(stream);

            uint   magic    = br.ReadUInt32();
            string packetID = Program.ReadString(br, 12);
            uint   length   = br.ReadUInt32();
            uint   checksum = br.ReadUInt32();

            if (magic == cMainNetwork)
            {
                byte[] payload = br.ReadBytes((int)length);

                byte[] hash = Program.GenerateHash(payload);
                if (CompareChecksum(checksum, hash))
                {
                    // valid packet, get the handler
                    if (mPacketProcessors.ContainsKey(packetID))
                    {
                        PacketProcessor p = mPacketProcessors[packetID];
                        p(payload);
                    }
                    else
                    {
                        Console.WriteLine("Unhandled packet: " + packetID);
                    }
                }
            }

            br.Close();
        }
        public void LoadTransaction(byte[] data)
        {
            byte[] hash    = Program.GenerateHash(data);
            string hashStr = Program.HashToString(hash);

            if (mTransactions.ContainsKey(hashStr))
            {
                Transaction t = mTransactions[hashStr];
                t.Load(data);
            }
            else
            {
                Console.WriteLine("Unknown transaction");
            }
        }
Example #3
0
        void SendPacket(string packetID, byte[] payload)
        {
            byte[] hash = Program.GenerateHash(payload);

            MemoryStream stream = new MemoryStream();
            BinaryWriter w      = new BinaryWriter(stream);

            w.Write(cMainNetwork);

            for (int i = 0; i < 12; i++)
            {
                if (i < packetID.Length)
                {
                    w.Write((byte)packetID[i]);
                }
                else
                {
                    w.Write((byte)0);
                }
            }

            // Payload len
            w.Write(payload.Length);

            // Checksum
            w.Write(hash[0]);
            w.Write(hash[1]);
            w.Write(hash[2]);
            w.Write(hash[3]);

            w.Write(payload);

            byte[] packetData = stream.ToArray();

            /*
             * Console.WriteLine("Send Packet - " + packetID);
             * for (int i = 0; i < packetData.Length; i++)
             * {
             *  Console.Write("{0:X2} ", packetData[i]);
             *  if (i > 0 && ((i + 1) % 16) == 0)
             *      Console.Write("\n");
             * }
             * Console.Write("\n");
             */
            mSocket.Send(packetData);
            w.Close();
        }