Exemplo n.º 1
0
        public override void SetKey(byte[] data, int offset, int length)
        {
            var key = NetUtility.ComputeSHAHash(data, offset, length);

            NetException.Assert(key.Length >= 16);
            SetKey(key, 0, 16);
        }
Exemplo n.º 2
0
        public override void SetKey(ReadOnlySpan <byte> data, int offset, int length)
        {
            // ReSharper disable once SuggestVarOrType_Elsewhere
            Span <byte> key = stackalloc byte[32];

            NetUtility.ComputeSHAHash(data.Slice(offset, length), key);
            NetException.Assert(key.Length >= 16);
            SetKey(key, 0, 16);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Computer private key (x)
        /// </summary>
        public static byte[] ComputePrivateKey(string username, string password, byte[] salt)
        {
            byte[] tmp       = Encoding.UTF8.GetBytes(username + ":" + password);
            byte[] innerHash = NetUtility.ComputeSHAHash(tmp);

            byte[] total = new byte[innerHash.Length + salt.Length];
            Buffer.BlockCopy(salt, 0, total, 0, salt.Length);
            Buffer.BlockCopy(innerHash, 0, total, salt.Length, innerHash.Length);

            // x   ie. H(salt || H(username || ":" || password))
            return(new NetBigInteger(NetUtility.ToHexString(NetUtility.ComputeSHAHash(total)), 16).ToByteArrayUnsigned());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Compute multiplier (k)
        /// </summary>
        private static NetBigInteger ComputeMultiplier()
        {
            string one = NetUtility.ToHexString(N.ToByteArrayUnsigned());
            string two = NetUtility.ToHexString(g.ToByteArrayUnsigned());

            string ccstr = one + two.PadLeft(one.Length, '0');

            byte[] cc = NetUtility.ToByteArray(ccstr);

            var ccHashed = NetUtility.ComputeSHAHash(cc);

            return(new NetBigInteger(NetUtility.ToHexString(ccHashed), 16));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Compute intermediate value (u)
        /// </summary>
        public static byte[] ComputeU(byte[] clientPublicEphemeral, byte[] serverPublicEphemeral)
        {
            // u = SHA-1(A || B)
            string one = NetUtility.ToHexString(clientPublicEphemeral);
            string two = NetUtility.ToHexString(serverPublicEphemeral);

            int    len   = 66;        //  Math.Max(one.Length, two.Length);
            string ccstr = one.PadLeft(len, '0') + two.PadLeft(len, '0');

            byte[] cc = NetUtility.ToByteArray(ccstr);

            var ccHashed = NetUtility.ComputeSHAHash(cc);

            return(new NetBigInteger(NetUtility.ToHexString(ccHashed), 16).ToByteArrayUnsigned());
        }
        private void InitializeNetwork()
        {
            lock (m_initializeLock)
            {
                // make sure this is properly set up again, if required.

                // start network thread if not running
                NetPeerManager.StartNetworkThread();

                m_configuration.Lock();

                if (m_status == NetPeerStatus.Running)
                {
                    return;
                }

                if (m_configuration.m_enableUPnP)
                {
                    m_upnp = new NetUPnP(this);
                }

                InitializePools();

                m_releasedIncomingMessages.Clear();
                m_unsentUnconnectedMessages.Clear();

                _handshakeManager.Handshakes.Clear();

                // bind to socket
                BindSocket(false);

                m_receiveBuffer            = new byte[m_configuration.ReceiveBufferSize];
                m_sendBuffer               = new byte[m_configuration.SendBufferSize];
                m_readHelperMessage        = new NetIncomingMessage(NetIncomingMessageType.Error);
                m_readHelperMessage.m_data = m_receiveBuffer;

                byte[] macBytes = NetUtility.GetMacAddressBytes();

                var    boundEp  = m_socket.LocalEndPoint as NetEndPoint;
                byte[] epBytes  = BitConverter.GetBytes(boundEp.GetHashCode());
                byte[] combined = new byte[epBytes.Length + macBytes.Length];
                Array.Copy(epBytes, 0, combined, 0, epBytes.Length);
                Array.Copy(macBytes, 0, combined, epBytes.Length, macBytes.Length);
                m_uniqueIdentifier = BitConverter.ToInt64(NetUtility.ComputeSHAHash(combined), 0);

                m_status = NetPeerStatus.Running;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Create XTEA symmetrical encryption object from sessionValue
        /// </summary>
        public static NetXtea CreateEncryption(NetPeer peer, byte[] sessionValue)
        {
            var hash = NetUtility.ComputeSHAHash(sessionValue);

            var key = new byte[16];

            for (int i = 0; i < 16; i++)
            {
                key[i] = hash[i];
                for (int j = 1; j < hash.Length / 16; j++)
                {
                    key[i] ^= hash[i + (j * 16)];
                }
            }

            return(new NetXtea(peer, key));
        }
Exemplo n.º 8
0
 /// <summary>
 /// String to hash for key
 /// </summary>
 public NetXtea(NetPeer peer, string key)
     : this(peer, NetUtility.ComputeSHAHash(Encoding.UTF8.GetBytes(key)), 32)
 {
 }