Ejemplo n.º 1
0
        /// <summary>
        /// Adds a thumbnail
        /// </summary>
        /// <param name="img">The image to create the thumbnail from</param>
        /// <returns>The MD5 of the thumbnail</returns>
        public static string Add(Stream img, string origPath)
        {
            img.Seek(0, SeekOrigin.Begin);

            string md5Hash;

            using (var origIMG = Image.FromStream(img)) {
                double ratio = Math.Min((double)ThumbnailWidth / origIMG.Width, (double)ThumbnailHeight / origIMG.Height);

                using (var newBMP = new Bitmap((int)Math.Ceiling(origIMG.Width * ratio), (int)Math.Ceiling(origIMG.Height * ratio))) {
                    using (var graph = Graphics.FromImage(newBMP)) {
                        graph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                        graph.DrawImage(origIMG, 0, 0, (float)Math.Ceiling(origIMG.Width * ratio), (float)Math.Ceiling(origIMG.Height * ratio));
                        graph.Flush(System.Drawing.Drawing2D.FlushIntention.Sync);

                        using (var stream = new MemoryStream(1024 * 1024)) {
                            newBMP.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                            stream.Flush();
                            byte [] bytes = stream.ToArray();

                            using (var hasher = new System.Security.Cryptography.MD5Cng())
                                md5Hash = BitConverter.ToString(hasher.ComputeHash(bytes)).Replace("-", "");

                            File.WriteAllBytes(Path.Combine(Program.Options.ThumbsPath, md5Hash), bytes);
                            thumbs.SetEntry(md5Hash, origPath);

                            bytes = null;
                        }
                    }
                }
            }

            return(md5Hash);
        }
Ejemplo n.º 2
0
        //md5加密算法
        public static string strToMd5(string sequence)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5Cng();
            byte[] bytResult = md5.ComputeHash(System.Text.Encoding.GetEncoding("UTF-8").GetBytes(sequence));

            //转换成字符串,32位
            string strResult = BitConverter.ToString(bytResult).Replace("-", "");

            return(strResult);
        }
Ejemplo n.º 3
0
 internal static byte[] CalcMD5(string SpecFunc, string SpecName, string SpecDev, int RevMaj, int RevMin)
 {
     byte[] Md5Buffer = new byte[62];
     System.Security.Cryptography.MD5Cng Calc = new System.Security.Cryptography.MD5Cng();
     AddToBuffer(Md5Buffer, 0, 20, AllignTo20(SpecFunc));
     AddToBuffer(Md5Buffer, 20, 20, AllignTo20(SpecName));
     AddToBuffer(Md5Buffer, 40, 20, AllignTo20(SpecDev));
     Md5Buffer[60] = (byte)(RevMaj + 0x30);
     Md5Buffer[61] = (byte)(RevMin + 0x30);
     return Calc.ComputeHash(Md5Buffer,0, 62);
 }
Ejemplo n.º 4
0
        public void DrawButton(RectangleF area, Graphics g, bool selected, Guid value)
        {
            float size   = area.Height - 4;
            float imageX = area.Width - area.Height + 2;

            Bitmap[] images = new[] { JONAS, ALI, DANIEL, JUAN };
            System.Security.Cryptography.MD5Cng hash = new System.Security.Cryptography.MD5Cng();
            Bitmap image = images[hash.ComputeHash(value.ToByteArray())[0] & 3];

            g.DrawImage(image, RectangleF.FromLTRB(area.Left + imageX, area.Top + 2, area.Right - 2, area.Bottom - 2));
            TextRenderer.DrawText(g, m_parameter.GetName(value), Font, new Point((int)area.X + 2, (int)area.Y + ((int)area.Height - Font.Height) / 2), Color.White);
        }
Ejemplo n.º 5
0
        public static String Md5(String ch)
        {
            using (System.Security.Cryptography.MD5 sha1 = new System.Security.Cryptography.MD5Cng())
            {
                ASCIIEncoding asen = new ASCIIEncoding();
                var hash = sha1.ComputeHash(asen.GetBytes(ch));
                var s = new StringBuilder();
                foreach (byte b in hash)
                    s.Append(b.ToString("x2").ToLower());

                return s.ToString() ;
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Will either accept, challenge or return an unauthorize request context. Will use the HttpListener
        /// provided in constructor in order to interract directly with the client.
        /// </summary>
        /// <param name="context">The context of the request to authenticate</param>
        /// <returns></returns>
        public System.Net.HttpListenerContext digest(System.Net.HttpListenerContext context)
        {
            if (context.Request.Headers["Authorization"] == null)
            {
                string responseString = "<HTML><HEAD><TITLE>Error</TITLE><META HTTP-EQUIV=\"Content - Type\" CONTENT=\"text / html; charset = ISO - 8859 - 1\"></HEAD><BODY><H1>401 Unauthorized.</H1></BODY></HTML>";
                System.Net.HttpListenerResponse response = context.Response;
                // Construct a response.
                byte[] buffer = Encoding.UTF8.GetBytes(responseString);
                response.StatusCode = 401;
                string digestHeader = craftDigestHeader(System.Environment.MachineName, GenerateNonce());
                response.AddHeader("WWW-Authenticate", digestHeader);
                // Get a response stream and write the response to it.
                response.ContentLength64 = buffer.Length;
                System.IO.Stream output = response.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                // You must close the output stream.
                output.Close();

                // The challenge has been sent. This request was invalid.
                return(null);
            }

            else // An Authorization header was present, there is digest data to analyse.
            {
                Dictionary <string, string> requestParams = parseHeader(context.Request.Headers["Authorization"]);

                string username = requestParams["username"];

                Console.WriteLine(requestParams["realm"]);
                Console.WriteLine(m_users[username]);
                string clientHA1StringData;
                if (m_users[username].type == PasswordType.PlainText)
                {
                    clientHA1StringData = username + ":" + requestParams["realm"] + ":" + m_users[username];
                }
                else
                {
                    clientHA1StringData = m_users[username].content;
                }

                Console.WriteLine("HA1 = M(" + clientHA1StringData + ")");

                string clientHA2StringData = context.Request.HttpMethod.ToUpper() + ":" + requestParams["uri"];

                Console.WriteLine("HA2 = M(" + clientHA2StringData + ")");

                byte[] clientHA1 = m_MD5Encoder.ComputeHash(System.Text.Encoding.ASCII.GetBytes(clientHA1StringData));
                byte[] clientHA2 = m_MD5Encoder.ComputeHash(System.Text.Encoding.ASCII.GetBytes(clientHA2StringData));

                string clientHA1String = BitConverter.ToString(clientHA1);
                clientHA1String = clientHA1String.ToLower();
                clientHA1String = clientHA1String.Replace("-", String.Empty);

                string clientHA2String = BitConverter.ToString(clientHA2);
                clientHA2String = clientHA2String.ToLower();
                clientHA2String = clientHA2String.Replace("-", String.Empty);

                string clientResponseString = clientHA1String + ":" + requestParams["nonce"] + ":" + requestParams["nc"] + ":" + requestParams["cnonce"] + ":" + requestParams["qop"] + ":" + clientHA2String;

                Console.WriteLine("Final Hash = M(" + clientResponseString + ")");

                byte[] clientResponseHA       = m_MD5Encoder.ComputeHash(System.Text.Encoding.ASCII.GetBytes(clientResponseString));
                string clientResponseStringHA = BitConverter.ToString(clientResponseHA);

                clientResponseStringHA = clientResponseStringHA.ToLower();
                clientResponseStringHA = clientResponseStringHA.Replace("-", String.Empty);

                Console.WriteLine("[DIGEST] - Server Hash : " + clientResponseStringHA);
                Console.WriteLine("[DIGEST] - Client Hash : " + requestParams["response"]);

                // ... request was properly authorized
                if (clientResponseStringHA.Equals(requestParams["response"]))
                {
                    return(context);
                }

                else
                {
                    return(null);
                }
            }
        }