Пример #1
0
        /// <summary>
        /// Emil's adaptation of md5crypt() from:
        /// $OpenBSD: md5crypt.c,v 1.13 2003/08/07 00:30:21 deraadt Exp $
        /// $FreeBSD: crypt.c,v 1.5 1996/10/14 08:34:02 phk Exp $
        /// Original license:
        /// ----------------------------------------------------------------------------
        /// "THE BEER-WARE LICENSE" (Revision 42):
        /// <phk @login.dknet.dk> wrote this file.As long as you retain this notice you
        /// can do whatever you want with this stuff.If we meet some day, and you think
        /// this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
        /// ----------------------------------------------------------------------------
        ///
        /// The JavaScript adaptation is copyright (c) 2004 Emil Mikulic
        /// </summary>
        private static string MD5HashPassword(EncodedString password, EncodedString salt)
        {
            EncodedString ctx  = password + "$1$" + salt;
            EncodedString ctx1 = GetMD5(password + salt + password);

            /* "Just as many characters of ctx1" (as there are in the password) */
            for (var pl = password.Length; pl > 0; pl -= 16)
            {
                ctx += ctx1.Subdata(0, (pl > 16) ? 16 : pl);
            }

            /* "Then something really weird" */
            for (int i = password.Length; i != 0; i >>= 1)
            {
                if ((i & 1) == 1)
                {
                    ctx += 0;
                }
                else
                {
                    ctx += (byte)password[0];
                }
            }
            ctx = GetMD5(ctx);

            /* "Just to make sure things don't run too fast" */
            for (int i = 0; i < 1000; i++)
            {
                ctx1 = "";
                if ((i & 1) == 1)
                {
                    ctx1 += password;
                }
                else
                {
                    ctx1 += ctx;
                }

                if ((i % 3) != 0)
                {
                    ctx1 += salt;
                }

                if ((i % 7) != 0)
                {
                    ctx1 += password;
                }

                if ((i & 1) != 0)
                {
                    ctx1 += ctx;
                }
                else
                {
                    ctx1 += password;
                }

                ctx = GetMD5(ctx1);
            }

            return(new string(("$1$" + salt + "$" +
                               To64Triplet(ctx, 0, 6, 12) +
                               To64Triplet(ctx, 1, 7, 13) +
                               To64Triplet(ctx, 2, 8, 14) +
                               To64Triplet(ctx, 3, 9, 15) +
                               To64Triplet(ctx, 4, 10, 5) +
                               To64Single(ctx, 11)
                               ).ToCharArray()));
        }