Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SHA1 Plain text? C#.NET

Tags:

c#

sha1

I am using C# and calculating SHA1 for a string. My question is that will this always produce plain text 0-1 and A-Z ? Or it will produce has with special characters too ? I mean is ComputeHash function here will return always plain text ?

SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
string receivedValue = BitConverter.ToString(sha1.ComputeHash(to_be_hash)).Replace("-", "");

Not sure but I think it should generate special character only if its converted to Base 64 .

like image 854
Pit Digger Avatar asked Mar 17 '11 14:03

Pit Digger


People also ask

Is SHA-1 broken?

SHA-1 has been phased out of use in most applications and none of the major browsers will accept certificates signed with SHA-1, and NIST deprecated it in 2011.

Can SHA-1 be decrypted?

Since SHA-1 maps several byte sequences to one, you can't "decrypt" a hash, but in theory you can find collisions: strings that have the same hash. It seems that breaking a single hash would cost about 2.7 million dollars worth of computer time currently, so your efforts are probably better spent somewhere else.

Is SHA-1 still secure?

Security researchers have achieved the first real-world collision attack against the SHA-1 hash function, producing two different PDF files with the same SHA-1 signature. This shows that the algorithm's use for security-sensitive functions should be discontinued as soon as possible.

Does SHA-1 require key?

SHA1 doesn't have a concept of keys at all. It's just a hash function.


2 Answers

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("-", "");
like image 117
CodesInChaos Avatar answered Oct 10 '22 22:10

CodesInChaos


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).

like image 27
Aidiakapi Avatar answered Oct 11 '22 00:10

Aidiakapi