News:

Wondering if this will always be free?  See why free is better.

Main Menu

Try convert password to sha1 c#

Started by sYnced, May 01, 2012, 04:03:45 PM

Previous topic - Next topic

sYnced

public static string HashCode(string str)
        {
            string rethash = "";
            try
            {
                System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
                System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
                byte[] combined = encoder.GetBytes(str);
                hash.ComputeHash(combined);
                rethash = Convert.ToBase64String(hash.Hash);
            }
            catch (Exception ex)
            {
                string strerr = "Error in HashCode : " + ex.Message;
            }
            return rethash;
        }

well this is my code, but the code on phpadmin is not the same! anyone can help me ?

kat

As this is about a coding issue, sYnced, I took the liberty of moving it to the... er... Coding board. ;)

Hope you don't mind.


Suki

Welcome to SMF

If you are talking about SMF's password, then you need to include the username too, for php: sha1(strtolower($username) . $password);
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

sYnced

Quote from: Suki on May 01, 2012, 04:09:43 PM
Welcome to SMF

If you are talking about SMF's password, then you need to include the username too, for php: sha1(strtolower($username) . $password);

but sukki this is c# not php! and user i have get that, but dont know how to use on c# code! xD

sYnced

Well i have modified a a part !


private void Form1_Load(object sender, EventArgs e)
        {
            string go = "testuser" + "mypw";
            string allez = HashCode(go);
            hxxp:messagebox.show [nonactive](allez);
        }
        public static string HashCode(string str)
        {
            string rethash = "";
            try
            {
                System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
                System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
                byte[] combined = encoder.GetBytes(str);
                hash.ComputeHash(combined);
                rethash = Convert.ToBase64String(hash.Hash);
            }
            catch (Exception ex)
            {
                string strerr = "Error in HashCode : " + ex.Message;
            }
            return rethash;
        }


But in database the output is different

Oldiesmann

You're returning a base64-encoded version of the hash string. SMF doesn't store hashes in Base64-encoded strings.

Try this. It's a slightly modified version of the example at http://dotnetpulse.blogspot.com/2007/12/sha1-hash-calculation-in-c.html:

public static string HashCode(string str)
{
    System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
    byte[] buffer = encoder.GetBytes(text);
    SHA1CryptoServiceProvider cryptoTransformSHA1 =
    new SHA1CryptoServiceProvider();
    string hash = BitConverter.ToString(
        cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");

    return hash;
}
Michael Eshom
Christian Metal Fans

sYnced

Quote from: Oldiesmann on May 01, 2012, 05:02:47 PM
You're returning a base64-encoded version of the hash string. SMF doesn't store hashes in Base64-encoded strings.

Try this. It's a slightly modified version of the example at hxxp:dotnetpulse.blogspot.com/2007/12/sha1-hash-calculation-in-c.html: [nonactive]

public static string HashCode(string str)
{
    System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
    byte[] buffer = encoder.GetBytes(text);
    SHA1CryptoServiceProvider cryptoTransformSHA1 =
    new SHA1CryptoServiceProvider();
    string hash = BitConverter.ToString(
        cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");

    return hash;
}


thanks a lot brow!

Suki

Board SMF Coding Discussion porpuse is to deal with SMF coding, since this topic is/was here/there I assumed you were trying to deal with SMF passwords, I just showed that you need the username + password.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

Advertisement: