Quick and Dirty Hashing in ASP.NET Core
One of our product imports data from other system via SFTP (you might confuse it with FTPS, but that is OK). Data coming from that system contains the whole dataset every time rather than just updates since last upload and does not have timestamps. So, we need to identify rows that should be updated.
Obvious solution would be using of hash. During import we calculate the hash of each row, compare it with saved one and update our DB only if hashes are different. The question is how to compute a hash in ASP.NET?
In good old days I used to use something like
However this time it raised an exception:
1
2
System.PlatformNotSupportedException: Operation is not supported on this platform.
at System.Security.Cryptography.HashAlgorithm.Create(String hashName)
Bummer. I tried to google it, found some huge discussion in dotnet repo, started to read it and… completely missed the workaround from the very first post :) But here is where the fun begins.
Further readings brought me to that:
Yes, I intentionally added argument names, otherwise argument values would look meaningless (they are anyway). It is supposed to be cryptographically secure and works, but seems to be too much for just computing a hash.
Blog post revealing this beast claims that it is used in ASP.NET Identity and I remember that hashing password is much easier. So the question: can we use famous [PasswordHasher
Here comes the code:
Well, it smells like a hack. But it is still oneliner, easy to use and working.
Also you can use hash verification:
It is less attractive because of messing with PasswordVerificationResult
, and one can ask “why not just compute the hash again and compare it with a stored one?”. The answer is that PasswordHasher<TUser>.HashPassword()
produces different hash for the same input every time, and the only way to validate the hash is the VerifyHashedPassword()
method.
What do you think? :)
PS Those who don’t like that approach still can use the very first snippet in this post, but change first line. You still would need to convert to and from Base64 and compare