c# - SHA computes hashes with random length -
recently, noticed sha algorithm computes hashes random length.
hashalgorithm provider; provider = hashalgorithm.create("system.security.cryptography.sha256"); while(!stackoverflow) { console.writeline(encoding.utf8.getstring( provider.computehash(encoding.utf8.getbytes( (new random()).next().tostring()))) .count().tostring()); } outputs:
29 29 30 29 29 30 29 31 29 29 32 29 30 28 ... is possible set maximum hash length? (could make hash useless..) or doing wrong in computing hashes? encoding?
edit:
the snippet above example. need in end method takes string, computes hash of string , returns it. hashalgorithm.computehash takes bytes , returns bytes, used utf8.getbytes() /utf8.getstring() converting seems huge mistake.
sha1-256 hashes 32 bytes long. doing here trying interpret these bytes utf-8 encoded text, plain wrong because there absolutely no guarantee hash bytes valid utf8-encoded sequence.
even if there such guarantee, utf-8 variable-length encoding: when converting raw bytes unicode characters "use up" variable number of bytes (1 4) per character output code theoretically anywhere between 8 , 32.
in general, example not make sense. please clarify intent is.
Comments
Post a Comment