0

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?

Gurpreet Kailey
  • 337
  • 2
  • 13
  • Salting Your Password: Best Practices? https://stackoverflow.com/questions/674904/salting-your-password-best-practices – Roman Ryzhiy Sep 21 '20 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. – Gurpreet Kailey Sep 21 '20 at 12:29
  • 4
    `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?](https://andrewlock.net/why-is-string-gethashcode-different-each-time-i-run-my-program-in-net-core/) – Pavel Anikhouski Sep 21 '20 at 12:33
  • Can confirm that running it on .NET Framework results in always the same ([source](https://dotnetfiddle.net/dukMPO)), but running it on .NET Core returns different results ([source](https://dotnetfiddle.net/WNjqnT)) each run. – MindSwipe Sep 21 '20 at 12:35
  • 5
    [Remarks section of `GetHashCode`](https://docs.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=netcore-3.1#remarks) tells you do not persist hash value and do not use the hash code instead of a value returned by a cryptographic hashing function – Pavel Anikhouski Sep 21 '20 at 12:37
  • 3
    Also, from [`String.GetHashCode`](https://docs.microsoft.com/en-us/dotnet/api/system.string.gethashcode?view=netcore-3.1) _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._ – Pavel Anikhouski Sep 21 '20 at 12:40
  • A lot of things broke in Core. Looks like the dispose method may be causing the issue. See : https://docs.microsoft.com/en-us/dotnet/core/compatibility/2.2-3.1#cryptostreamdispose-transforms-final-block-only-when-writing – jdweng Sep 21 '20 at 12:48

1 Answers1

0

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.

Gurpreet Kailey
  • 337
  • 2
  • 13