/// <summary>
        /// Checks to see if a given packets data matches a password
        /// provided by the receiving client and returns the matching password if found.
        /// </summary>
        /// <param name="bytes">The packets data</param>
        /// <param name="passwordManager">The receiving client's list of passwords</param>
        /// <param name="passwordRequired">Indicates if the request must supply a valid password</param>
        /// <param name="password">Returns the matching password if found; null otherwise</param>
        /// <returns><c>true</c> if the password matches, <c>false</c> otherwise</returns>
        protected static bool IsPasswordValid(byte[] bytes, PasswordManager passwordManager, bool passwordRequired, out string password)
        {
            password = null;

            byte[] packetWithoutPassword = new byte[bytes.Length - 16];
            Array.Copy(bytes, 0, packetWithoutPassword, 0, packetWithoutPassword.Length);
            byte[] thatChecksum = new byte[16];
            Array.Copy(bytes, bytes.Length - 16, thatChecksum, 0, 16);
            string thatChecksumString = Encoding.UTF8.GetString(thatChecksum);

            ByteBuilder bpb = new ByteBuilder();
            bpb.Append(packetWithoutPassword);

            Queue<string> validPasswords = new Queue<string>();
            foreach (KeyValuePair<string, Password> item in passwordManager.Passwords)
            {
                validPasswords.Enqueue(item.Key);
            }
            // if the request does not require a password, then
            // we can also try no (blank) password
            if (!passwordRequired) validPasswords.Enqueue(String.Empty);

            while(validPasswords.Count > 0)
            {
                string p = validPasswords.Dequeue();
                ByteBuilder pb = new ByteBuilder();
                pb.Append(bpb.GetBytes());
                pb.Append(p);
                byte[] thisChecksum = Cryptography.ComputeHash(pb.GetBytes(), Cryptography.HashAlgorithmType.MD5);
                string thisChecksumString = Encoding.UTF8.GetString(thisChecksum);

                if (thisChecksumString == thatChecksumString)
                {
                    password = p;
                    return true;
                }
            }

            return false;
        }
Beispiel #2
0
        private static object getRealValue(IDataReader datareader, int ordinal, Type t)
        {

            if (t == typeof(Int16)) return !datareader.IsDBNull(ordinal) ? (object)datareader.GetInt16(ordinal) : null;
            
            if (t == typeof(Int32)) return !datareader.IsDBNull(ordinal) ? (object)datareader.GetInt32(ordinal) : null;
            
            if (t == typeof(Int64)) return !datareader.IsDBNull(ordinal) ? (object)datareader.GetInt32(ordinal) : null;
            
            if (t == typeof(Byte[]))
            {
                if (datareader.IsDBNull(ordinal)) return null;
                int bufferSize = 1024;
                int startIndex = 0;
                byte[] outbyte = new byte[bufferSize];
                ByteBuilder bb = new ByteBuilder();
                long retval = datareader.GetBytes(ordinal, startIndex, outbyte, 0, bufferSize);
                while (retval == bufferSize)
                {
                    bb.Append(outbyte);
                    startIndex += bufferSize;
                    retval = datareader.GetBytes(ordinal, startIndex, outbyte, 0, bufferSize);
                }
                if (retval > 0)
                    bb.Append(outbyte);
                return bb.GetBytes();
            }
            
            if (t == typeof(String)) return !datareader.IsDBNull(ordinal) ? datareader.GetString(ordinal) : null;
            
            if (t == typeof(DateTime)) return !datareader.IsDBNull(ordinal) ? (object)datareader.GetDateTime(ordinal) : null;
            
            if (t == typeof(Boolean)) return !datareader.IsDBNull(ordinal) ? (object)datareader.GetBoolean(ordinal) : null;

            if (t == typeof(Double)) return !datareader.IsDBNull(ordinal) ? (object)datareader.GetDouble(ordinal) : null;

            if (t == typeof(Single)) return !datareader.IsDBNull(ordinal) ? (object)datareader.GetFloat(ordinal) : null;

            if (t == typeof(Guid)) return !datareader.IsDBNull(ordinal) ? (object)datareader.GetGuid(ordinal) : null;

            if (t == typeof(Decimal)) return !datareader.IsDBNull(ordinal) ? (object)datareader.GetDecimal(ordinal) : null;

            if (t == typeof(Byte)) return !datareader.IsDBNull(ordinal) ? (object)datareader.GetByte(ordinal) : null;

            return null;
        }
        /// <summary>
        /// Converts the notification information into a packet of data to be sent 
        /// to the Growl receiving application.
        /// </summary>
        /// <returns>byte array</returns>
        private byte[] PrepareData()
        {
            int flags = ConvertPriorityToFlag(this.priority);
            if (this.sticky) flags = flags | 1;

            ByteBuilder bb = new ByteBuilder();
            bb.Append((byte)this.protocolVersion);
            bb.Append((byte)this.packetType);
            bb.Append((short)flags);
            bb.Append(ByteBuilder.GetStringLengthAsShort(this.notificationType.Name));
            bb.Append(ByteBuilder.GetStringLengthAsShort(this.title));
            bb.Append(ByteBuilder.GetStringLengthAsShort(this.description));
            bb.Append(ByteBuilder.GetStringLengthAsShort(this.applicationName));
            bb.Append(this.notificationType.Name);
            bb.Append(this.title);
            bb.Append(this.description);
            bb.Append(this.applicationName);

            // handle the password
            ByteBuilder pb = new ByteBuilder();
            pb.Append(this.password);
            ByteBuilder bpb = new ByteBuilder();
            bpb.Append(bb.GetBytes());
            bpb.Append(pb.GetBytes());

            byte[] checksum = Cryptography.ComputeHash(bpb.GetBytes(), Cryptography.HashAlgorithmType.MD5);
            ByteBuilder fb = new ByteBuilder();
            fb.Append(bb.GetBytes());
            fb.Append(checksum);
            byte[] data = fb.GetBytes();

            return data;
        }
Beispiel #4
0
        private static object getRealValue(IDataReader datareader, int ordinal, Type t)
        {
            if (t == typeof(Int16))
            {
                return(!datareader.IsDBNull(ordinal) ? (object)datareader.GetInt16(ordinal) : null);
            }

            if (t == typeof(Int32))
            {
                return(!datareader.IsDBNull(ordinal) ? (object)datareader.GetInt32(ordinal) : null);
            }

            if (t == typeof(Int64))
            {
                return(!datareader.IsDBNull(ordinal) ? (object)datareader.GetInt32(ordinal) : null);
            }

            if (t == typeof(Byte[]))
            {
                if (datareader.IsDBNull(ordinal))
                {
                    return(null);
                }
                int         bufferSize = 1024;
                int         startIndex = 0;
                byte[]      outbyte    = new byte[bufferSize];
                ByteBuilder bb         = new ByteBuilder();
                long        retval     = datareader.GetBytes(ordinal, startIndex, outbyte, 0, bufferSize);
                while (retval == bufferSize)
                {
                    bb.Append(outbyte);
                    startIndex += bufferSize;
                    retval      = datareader.GetBytes(ordinal, startIndex, outbyte, 0, bufferSize);
                }
                if (retval > 0)
                {
                    bb.Append(outbyte);
                }
                return(bb.GetBytes());
            }

            if (t == typeof(String))
            {
                return(!datareader.IsDBNull(ordinal) ? datareader.GetString(ordinal) : null);
            }

            if (t == typeof(DateTime))
            {
                return(!datareader.IsDBNull(ordinal) ? (object)datareader.GetDateTime(ordinal) : null);
            }

            if (t == typeof(Boolean))
            {
                return(!datareader.IsDBNull(ordinal) ? (object)datareader.GetBoolean(ordinal) : null);
            }

            if (t == typeof(Double))
            {
                return(!datareader.IsDBNull(ordinal) ? (object)datareader.GetDouble(ordinal) : null);
            }

            if (t == typeof(Single))
            {
                return(!datareader.IsDBNull(ordinal) ? (object)datareader.GetFloat(ordinal) : null);
            }

            if (t == typeof(Guid))
            {
                return(!datareader.IsDBNull(ordinal) ? (object)datareader.GetGuid(ordinal) : null);
            }

            if (t == typeof(Decimal))
            {
                return(!datareader.IsDBNull(ordinal) ? (object)datareader.GetDecimal(ordinal) : null);
            }

            if (t == typeof(Byte))
            {
                return(!datareader.IsDBNull(ordinal) ? (object)datareader.GetByte(ordinal) : null);
            }

            return(null);
        }
Beispiel #5
0
        /// <summary>
        /// Frames the response with the special WebSocket framing bytes before sending.
        /// </summary>
        /// <param name="bytes">The data to be sent.</param>
        public override void  BeforeResponse(ref byte[] bytes)
        {
            ByteBuilder bb = new ByteBuilder();

            // FIN and opcode
            byte bFrameControl = BYTE_FIN_FINAL + BYTE_OPCODE_TEXT;

            bb.Append(bFrameControl);

            // Mask and length
            long length = bytes.LongLength;

            if (length > short.MaxValue)
            {
                bb.Append(BYTE_LENGTH_64);
                bb.Append(length);
            }
            else if (length > (int)BYTE_LENGTH_7)
            {
                bb.Append(BYTE_LENGTH_16);
                bb.Append((short)length);
            }
            else
            {
                bb.Append((byte)length);
            }

            // actual GNTP bytes
            bb.Append(bytes);

            bytes = bb.GetBytes();


            /*
             * // wrap the data with the framing
             * byte[] wrappedArray = new byte[extraByteCount + bytes.Length];
             * wrappedArray[0] = bFrameControl;
             * Array.Copy(bDataControl, 0, wrappedArray, 1, bDataControl.Length);
             * Array.Copy(bytes, 0, wrappedArray, extraByteCount, bytes.Length);
             * bytes = wrappedArray;
             * */

            /* dont do this here - we might need to send callback data
             * // send a Close frame
             * byte[] bCloseBytes = new byte[2];
             * bCloseBytes[0] = 128 + 8;   // FIN and Close
             * bCloseBytes[1] = 0;
             *
             * wrappedArray = new byte[bCloseBytes.Length + bytes.Length];
             * Array.Copy(bytes, 0, wrappedArray, 0, bytes.Length);
             * Array.Copy(bCloseBytes, 0, wrappedArray, bytes.Length, bCloseBytes.Length);
             * bytes = wrappedArray;
             * */

            /* write out the bits
             * System.Collections.BitArray ba = new System.Collections.BitArray(bytes);
             * for (int i = 0; i < ba.Count; i++)
             * {
             *  if (i > 0 && i % 8 == 0) Console.WriteLine("");
             *  bool bit = ba.Get(i);
             *  Console.Write(bit ? 1 : 0);
             * }
             * Console.WriteLine();
             * */
        }
        /// <summary>
        /// Converts the notification information into a packet of data to be sent 
        /// to the Growl receiving application.
        /// </summary>
        /// <returns>byte array</returns>
        private byte[] PrepareData()
        {
            ByteBuilder nb = new ByteBuilder();
            ByteBuilder db = new ByteBuilder();
            int index = 0;
            int notificationTypeCount = 0;
            int notificationTypeEnabledCount = 0;
            foreach (NotificationType notificationType in this.notificationTypes)
            {
                nb.Append(notificationType.Name.Length);
                nb.Append(notificationType.Name);

                notificationTypeCount++;
                if (notificationType.Enabled)
                {
                    notificationTypeEnabledCount++;
                    db.Append((byte)index);
                }
                index++;
            }

            ByteBuilder bb = new ByteBuilder();
            bb.Append((byte)this.protocolVersion);
            bb.Append((byte)this.packetType);
            bb.Append(ByteBuilder.GetStringLengthAsShort(this.applicationName));
            bb.Append((byte)notificationTypeCount);
            bb.Append((byte)notificationTypeEnabledCount);
            bb.Append(this.applicationName);
            bb.Append(nb.GetBytes());
            bb.Append(db.GetBytes());

            // handle the password
            ByteBuilder pb = new ByteBuilder();
            pb.Append(this.password);
            ByteBuilder bpb = new ByteBuilder();
            bpb.Append(bb.GetBytes());
            bpb.Append(pb.GetBytes());

            byte[] checksum = Cryptography.ComputeHash(bpb.GetBytes(), Cryptography.HashAlgorithmType.MD5);
            ByteBuilder fb = new ByteBuilder();
            fb.Append(bb.GetBytes());
            fb.Append(checksum);
            byte[] data = fb.GetBytes();

            return data;
        }