SHA1 Plain text? C#.NET

不羁岁月 提交于 2019-11-29 10:38:59

It's a hex string, so only 0-9 and A-F.

Actually it's just a byte array, but you use the string BitConverter.ToString(byte[]) method to turn it into a hex-string in pairs of two separated by a - (dash).

Hexadecimal only contains: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. 16 options, a pair of two of those will represent a single byte (16*16 = 2^8 = 256, is a single byte).

The hashing API itself returns a byte[] containing an arbitrary 20 byte sequence.

You need to convert it to string yourself. Using Hex or Base64 as encodings are popular choices.

BitConverter.ToString() converts the string to hex with bytes delimited by -. Since you then remove the -s you end up with a 40 character hex string (0-9, A-F), which is a subset of alphanumeric chars.

So your code will always return an alphanumeric string, even though SHA-1 doesn't.

SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
byte[] hash = sha1.ComputeHash(to_be_hash)
string delimitedHexHash = BitConverter.ToString(hash);
string hexHash = delimitedHexHash.Replace("-", "");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!