locked
Simulating MySql's PASSWORD() encryption using .NET in Windows 8 RRS feed

  • Question

  • PASSWORD() according to MySQL documentation is a double SHA1 algorithm.

    In Win32 I was using this method:

    public string GenerateMySQLHash(string key)
    {
         byte[] keyArray = Encoding.UTF8.GetBytes(key);
            SHA1Managed enc = new SHA1Managed();
            byte[] encodedKey = enc.ComputeHash(enc.ComputeHash(keyArray));
        StringBuilder myBuilder = new StringBuilder(encodedKey.Length);
    
        foreach (byte b in encodedKey)
            myBuilder.Append(b.ToString("X2"));
    
        return "*" + myBuilder.ToString();
    }

    SHA1Managed object is not available in the Metro .net framework because the security stuff is now in Windows.Security.Cryptography and not in System.Security.Cryptography.

    In the documentation, I see this example to obtain the SHA1 from a string:

     public String HashMsg(String strMsg)
        {
            // Convert the message string to binary data.
            IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(strMsg, BinaryStringEncoding.Utf8);
    
            // Create a HashAlgorithmProvider object.
            HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
    
            // Hash the message.
            IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);
    
            // Verify that the hash length equals the length specified for the algorithm.
            if (buffHash.Length != objAlgProv.HashLength)
            {
                throw new Exception("There was an error creating the hash");
            }
    
            // Convert the hash to a string (for display).
            return CryptographicBuffer.EncodeToBase64String(buffHash);
        }

    But I need a double SHA1 algorithm. Any way to do this easy as in win32?

    Wednesday, July 24, 2013 11:12 AM

Answers

  • I finally found the solution :), I hope it help you:

            /// <summary>
            /// Reverse a string
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public static string ReverseString(string s)
            {
                char[] arr = s.ToCharArray();
                Array.Reverse(arr);
                return new string(arr);
            }
    
            /// <summary>
            /// MySQL PASSWORD encryption
            /// </summary>
            /// <param name="strMsg"></param>
            /// <returns></returns>
            public String HashMsg(String strMsg)
            {
                // Convert the message string to binary data.
                IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(strMsg, BinaryStringEncoding.Utf8);
    
                // Create a HashAlgorithmProvider object.
                HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
    
                // Hash the message.
                IBuffer buffHash = objAlgProv.HashData(objAlgProv.HashData(buffUtf8Msg));
    
                // Verify that the hash length equals the length specified for the algorithm.
                if (buffHash.Length != objAlgProv.HashLength)
                {
                    throw new Exception("There was an error creating the hash");
                }
    
                byte[] arrByteNew;
                CryptographicBuffer.CopyToByteArray(buffHash, out arrByteNew);
                StringBuilder myBuilder = new StringBuilder(arrByteNew.Length);
    
                foreach (var b in arrByteNew)
                    myBuilder.Append(b.ToString("X2"));
    
                // Concat with the STRING REVERSED
                String stringReversed = "*" + myBuilder.ToString() + ReverseString(strMsg);
    
                buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(s3, BinaryStringEncoding.Utf8);
                buffHash = objAlgProv.HashData(objAlgProv.HashData(buffUtf8Msg));
    
                if (buffHash.Length != objAlgProv.HashLength)
                {
                    throw new Exception("There was an error creating the hash");
                }
    
                CryptographicBuffer.CopyToByteArray(buffHash, out arrByteNew);
                myBuilder = new StringBuilder(arrByteNew.Length);
    
                foreach (var b in arrByteNew)
                {
                    myBuilder.Append(b.ToString("X2"));
                }
    
                stringReversed = "*" + myBuilder.ToString();
    
                return stringReversed;
            }
    • Marked as answer by rincha Thursday, July 25, 2013 9:23 AM
    Thursday, July 25, 2013 9:22 AM

All replies

  • Hi,rincha

    you can code like this:

    IBuffer buffHash 
    =objAlgProv.HashData(objAlgProv.HashData(buffUtf8Msg));

    you can refer to this link:
    http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.hashalgorithmprovider.hashdata.aspx/css

    Thanks,
    Anne Jing


    • Edited by Anne Jing Thursday, July 25, 2013 4:52 AM form
    Thursday, July 25, 2013 3:42 AM
  • I finally found the solution :), I hope it help you:

            /// <summary>
            /// Reverse a string
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public static string ReverseString(string s)
            {
                char[] arr = s.ToCharArray();
                Array.Reverse(arr);
                return new string(arr);
            }
    
            /// <summary>
            /// MySQL PASSWORD encryption
            /// </summary>
            /// <param name="strMsg"></param>
            /// <returns></returns>
            public String HashMsg(String strMsg)
            {
                // Convert the message string to binary data.
                IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(strMsg, BinaryStringEncoding.Utf8);
    
                // Create a HashAlgorithmProvider object.
                HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
    
                // Hash the message.
                IBuffer buffHash = objAlgProv.HashData(objAlgProv.HashData(buffUtf8Msg));
    
                // Verify that the hash length equals the length specified for the algorithm.
                if (buffHash.Length != objAlgProv.HashLength)
                {
                    throw new Exception("There was an error creating the hash");
                }
    
                byte[] arrByteNew;
                CryptographicBuffer.CopyToByteArray(buffHash, out arrByteNew);
                StringBuilder myBuilder = new StringBuilder(arrByteNew.Length);
    
                foreach (var b in arrByteNew)
                    myBuilder.Append(b.ToString("X2"));
    
                // Concat with the STRING REVERSED
                String stringReversed = "*" + myBuilder.ToString() + ReverseString(strMsg);
    
                buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(s3, BinaryStringEncoding.Utf8);
                buffHash = objAlgProv.HashData(objAlgProv.HashData(buffUtf8Msg));
    
                if (buffHash.Length != objAlgProv.HashLength)
                {
                    throw new Exception("There was an error creating the hash");
                }
    
                CryptographicBuffer.CopyToByteArray(buffHash, out arrByteNew);
                myBuilder = new StringBuilder(arrByteNew.Length);
    
                foreach (var b in arrByteNew)
                {
                    myBuilder.Append(b.ToString("X2"));
                }
    
                stringReversed = "*" + myBuilder.ToString();
    
                return stringReversed;
            }
    • Marked as answer by rincha Thursday, July 25, 2013 9:23 AM
    Thursday, July 25, 2013 9:22 AM