1

I am converting .net project to PHP . But unable to convert the following code:

public static string HashString(string value)
    {
        SHA1 hasher = new SHA1CryptoServiceProvider();

        UTF8Encoding enc = new UTF8Encoding();

        byte[] hashInBytes = hasher.ComputeHash(enc.GetBytes(value));

        return Convert.ToBase64String(hashInBytes);
    }

So far I have done this but result is not same:

function HashString($str) {
  return base64_encode(sha1($str));
}

Please help, thanks.

1

2 Answers 2

1

The reason behind the difference is, PHP uses ASCII encoding for hash calculations.

In C#, you can replace UTF8Encoding with ASCIIEncoding in order to have same results.

Sign up to request clarification or add additional context in comments.

3 Comments

I have a database filled with the encrypted password field,, and I want to use the same database, . Iam creating the project in PHP so its not useful for me to convert .net code.
Is there a way I can use utf8 encoding sha1 in php
password uses Unicode characters? that's weird. It's suggested to change the mechanism in C# instead.
0

Finally I found the solution:

This is the final code which is equivalent to .net code:

function HashString($str) {
  return base64_encode(sha1($str,true));
}

I have added "true" with sha1 function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.