How to obtain one-way hash for given string in .NET?

岁酱吖の 提交于 2019-12-23 13:13:40

问题


Is there a build in library in .NET that can compute secure one-way hash ? I mean a library that implements SHA-2 cryptographic hash function or something similar.

If is there is no SHA-2 implementation some weaker hash funcion would be sufficient. If there are more options I prefer the most secure one.

Please provide a use example e.g. provide the code that returns one-way hash for string mySampleString.


EDIT: Please provide the example and the hash algorithm used.


回答1:


You can use the SHA256Mananged class to generate a hash of a string. The ComputeHash documentation provides a complete example.

If you are targeting Vista+ only, you can use the faster SHA256Cng class instead. Just realize that it will throw on XP.




回答2:


Have a look in the System.Security.Cryptography namespace.
There you have a bunch of SHA-algorithms, MD5 etc.




回答3:


HashAlgorithm.ComputeHash Method (Byte[])



来源:https://stackoverflow.com/questions/5264644/how-to-obtain-one-way-hash-for-given-string-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!