Beispiel #1
0
 public static byte[] Sha1Digest(this byte[] bytes)
 {
     Org.BouncyCastle.Crypto.Digests.Sha1Digest myHash = new Org.BouncyCastle.Crypto.Digests.Sha1Digest();
     myHash.BlockUpdate(bytes, 0, bytes.Length);
     byte[] compArr = new byte[myHash.GetDigestSize()];
     myHash.DoFinal(compArr, 0);
     return(compArr);
 }
 public void UpdateHandshakeHash(byte[] data)
 {
     if (_VerifyHandshake == null)
     {
         if (_VerifyHandshakeSHA1 != null)
         {
             _VerifyHandshakeSHA1.BlockUpdate(data, 0, data.Length);
             _VerifyHandshakeMD5.BlockUpdate(data, 0, data.Length);
         }
     }
     else
     {
         _VerifyHandshake.BlockUpdate(data, 0, data.Length);
     }
 }
Beispiel #3
0
        IEnumerator _Connect(Uri uri, Dictionary <string, string> optionalHeaders = null)
        {
            isDone    = false;
            connected = false;
            exception = null;

            receivedClosingHandshake = false;
            closing = false;

            var req = new Request("GET", uri.ToString());

            req.headers.Set("Upgrade", "websocket");
            req.headers.Set("Connection", "Upgrade");
            var key = WebSocketKey();

            req.headers.Set("Sec-WebSocket-Key", key);
            req.headers.Add("Sec-WebSocket-Protocol", "chat, superchat");
            req.headers.Set("Sec-WebSocket-Version", "13");
            req.headers.Set("Origin", "null");


            if (optionalHeaders != null)
            {
                foreach (string headerName in optionalHeaders.Keys)
                {
                    req.headers.Set(headerName, optionalHeaders [headerName]);
                }
            }


            req.acceptGzip = false;
            yield return(req.Send());

            if (req.exception != null)
            {
                exception = req.exception;
            }
            else
            {
                if (req.response.headers.Get("Upgrade").ToLower() == "websocket" && req.response.headers.Get("Connection").ToLower() == "upgrade")
                {
                    var receivedKey = req.response.headers.Get("Sec-Websocket-Accept").ToLower();
                    #if UNITY_WP8
                    var keyBytes = enc.GetBytes(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
                    var sha      = new Org.BouncyCastle.Crypto.Digests.Sha1Digest();
                    sha.BlockUpdate(keyBytes, 0, keyBytes.Length);
                    var hashBytes = new byte[20];
                    sha.DoFinal(hashBytes, 0);
                    var computedKey = System.Convert.ToBase64String(hashBytes).ToLower();
                    #else
                    var sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();
                    sha.ComputeHash(enc.GetBytes(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
                    var computedKey = System.Convert.ToBase64String(sha.Hash).ToLower();
                    #endif
                    if (computedKey == receivedKey)
                    {
                        //good to go
                        connected            = true;
                        connection           = req.upgradedConnection;
                        outgoingWorkerThread = new Thread(OutgoingWorker);
                        outgoingWorkerThread.Start();
                        incomingWorkerThread = new Thread(IncomingWorker);
                        incomingWorkerThread.Start();
                        UniWeb.Instance.StartCoroutine(Dispatcher());
                        UniWeb.Instance.OnQuit(() => {
                            Close(CloseEventCode.CloseEventCodeGoingAway, "Quit");
                            req.upgradedConnection.Dispose();
                        });
                        if (OnConnect != null)
                        {
                            OnConnect();
                        }
                    }
                    else
                    {
                        //invalid
                        connected = false;
                    }
                }
            }
            isDone = true;
        }