Ejemplo n.º 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);
 }
Ejemplo n.º 2
0
 public void InitaliseHandshakeHash(bool legacy)
 {
     if (legacy)
     {
         _VerifyHandshakeSHA1 = new Org.BouncyCastle.Crypto.Digests.Sha1Digest();
         _VerifyHandshakeMD5 = new Org.BouncyCastle.Crypto.Digests.MD5Digest();
     }
     else
         _VerifyHandshake = new Org.BouncyCastle.Crypto.Digests.Sha256Digest();
 }
Ejemplo n.º 3
0
 public void InitaliseHandshakeHash(bool legacy)
 {
     if (legacy)
     {
         _VerifyHandshakeSHA1 = new Org.BouncyCastle.Crypto.Digests.Sha1Digest();
         _VerifyHandshakeMD5  = new Org.BouncyCastle.Crypto.Digests.MD5Digest();
     }
     else
     {
         _VerifyHandshake = new Org.BouncyCastle.Crypto.Digests.Sha256Digest();
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a signature value given a signature base and the consumer secret and a known token secret.
        /// </summary>
        /// <a href="http://oauth.net/core/1.0#rfc.section.9.2"/>
        /// <param name="signatureMethod">The hashing method</param>
        /// <param name="signatureTreatment">The treatment to use on a signature value</param>
        /// <param name="signatureBase">The signature base</param>
        /// <param name="consumerSecret">The consumer secret</param>
        /// <param name="tokenSecret">The token secret</param>
        /// <returns></returns>
        public static string GetSignature(OAuthSignatureMethod signatureMethod,
                                          OAuthSignatureTreatment signatureTreatment,
                                          string signatureBase,
                                          string consumerSecret,
                                          string tokenSecret)
        {
            if (string.IsNullOrEmpty(tokenSecret))
            {
                tokenSecret = String.Empty;
            }
            consumerSecret = UrlEncodeRelaxed(consumerSecret);
            tokenSecret    = UrlEncodeRelaxed(tokenSecret);
            var    key = string.Format("{0}&{1}", consumerSecret, tokenSecret);
            string signature;

            switch (signatureMethod)
            {
            case OAuthSignatureMethod.HmacSha1:
            {
                var keyData = _encoding.GetBytes(key);
#if USE_BOUNCYCASTLE
                var digest = new Org.BouncyCastle.Crypto.Digests.Sha1Digest();
                var crypto = new Org.BouncyCastle.Crypto.Macs.HMac(digest);
                crypto.Init(new Org.BouncyCastle.Crypto.Parameters.KeyParameter(keyData));
                signature = signatureBase.HashWith(crypto);
#else
                using (var digest = new System.Security.Cryptography.HMACSHA1(keyData))
                    signature = signatureBase.HashWith(digest);
#endif
                break;
            }

            case OAuthSignatureMethod.PlainText:
            {
                signature = key;
                break;
            }

            default:
                throw new NotImplementedException("Only HMAC-SHA1 is currently supported.");
            }
            var result = signatureTreatment == OAuthSignatureTreatment.Escaped
                ? UrlEncodeRelaxed(signature)
                : signature;
            return(result);
        }
Ejemplo n.º 5
0
 public byte[] GetHash()
 {
     byte[] handshakeHash;
     if (_VerifyHandshake == null)
     {
         IDigest sha1 = new Org.BouncyCastle.Crypto.Digests.Sha1Digest(_VerifyHandshakeSHA1);
         IDigest md5  = new Org.BouncyCastle.Crypto.Digests.MD5Digest(_VerifyHandshakeMD5);
         handshakeHash = new byte[sha1.GetDigestSize() + md5.GetDigestSize()];
         md5.DoFinal(handshakeHash, 0);
         sha1.DoFinal(handshakeHash, md5.GetDigestSize());
     }
     else
     {
         IDigest hash = new Org.BouncyCastle.Crypto.Digests.Sha256Digest((Org.BouncyCastle.Crypto.Digests.Sha256Digest)_VerifyHandshake);
         handshakeHash = new byte[hash.GetDigestSize()];
         hash.DoFinal(handshakeHash, 0);
     }
     return(handshakeHash);
 }
Ejemplo n.º 6
0
 public byte[] GetHash()
 {
     byte[] handshakeHash;
     if (_VerifyHandshake == null)
     {
         IDigest sha1 = new Org.BouncyCastle.Crypto.Digests.Sha1Digest(_VerifyHandshakeSHA1);
         IDigest md5 = new Org.BouncyCastle.Crypto.Digests.MD5Digest(_VerifyHandshakeMD5);
         handshakeHash = new byte[sha1.GetDigestSize() + md5.GetDigestSize()];
         md5.DoFinal(handshakeHash, 0);
         sha1.DoFinal(handshakeHash, md5.GetDigestSize());
     }
     else
     {
         IDigest hash = new Org.BouncyCastle.Crypto.Digests.Sha256Digest((Org.BouncyCastle.Crypto.Digests.Sha256Digest)_VerifyHandshake);
         handshakeHash = new byte[hash.GetDigestSize()];
         hash.DoFinal(handshakeHash, 0);
     }
     return handshakeHash;
 }
Ejemplo n.º 7
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;
        }