3

I have following code written in application.

  var salt = "0115ee62e6e5e8ea68db9e2d7e72cec85e810ad9566212ffc2c4b76a17ca0b00";
  var hashCode = StringComparer.OrdinalIgnoreCase.GetHashCode(salt ?? string.Empty);

It has different behaviour in 'asp.net core' and 'standard' framework.

Framework 4.7: If I will use this code in website/console application created in standard framework of .net, it alwasy give me same result. The value of hashcode variable is always "1066337973".

Asp.net Core: If I will use this code in asp.net core website, then every time I stop the application and start again, the hashcode variable have differnt value.

Becasue of this problem, my logic is getting failed in asp.net core application.
How this problem can be solved?

7
  • Salting Your Password: Best Practices? stackoverflow.com/questions/674904/… Commented Sep 21, 2020 at 12:24
  • @RomanRyzhiy, I have a big logic written for encryption and decryption and that is working fine in other .net frameworks. when I tried to use the same logic for .net core application then it is not working for me. Commented Sep 21, 2020 at 12:29
  • 6
    GetHashCode implementation was changed in .NET Core, it's an expected behavior, have a look at this article Why is string.GetHashCode() different each time I run my program in .NET Core? Commented Sep 21, 2020 at 12:33
  • 5
    Remarks section of GetHashCode tells you do not persist hash value and do not use the hash code instead of a value returned by a cryptographic hashing function Commented Sep 21, 2020 at 12:37
  • 3
    Also, from String.GetHashCode Hash codes for identical strings can differ across .NET implementations, across .NET versions, and across .NET platforms (such as 32-bit and 64-bit) for a single version of .NET. In some cases, they can even differ by application domain. This implies that two subsequent runs of the same program may return different hash codes. Commented Sep 21, 2020 at 12:40

1 Answer 1

6

As per @Pavel Anikhouski

GetHashCode implementation was changed in .NET Core, it's expected behavior, have a look at this article Why is string.GetHashCode() different each time I run my program in .NET Core?

String.GetHashCode Hash codes for identical strings can differ across .NET implementations, across .NET versions, and across .NET platforms (such as 32-bit and 64-bit) for a single version of .NET. In some cases, they can even differ by application domain. This implies that two subsequent runs of the same program may return different hash codes.

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

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.