Featured post
c# - Truncating a byte array vs. substringing the Encoded string coming out of SHA-256 -
i not familiar hashing algorithms , risks associated when using them , therefore have question on answer below received on previous question . . .
based on comment hash value must, when encoded ascii, fit within 16 asci characters, solution first, choose cryptographic hash function (the sha-2 family includes sha-256, sha-384, , sha-512) then, truncate output of chosen hash function 96 bits (12 bytes) - is, keep first 12 bytes of hash function output , discard remaining bytes then, base-64-encode truncated output 16 ascii characters (128 bits) yielding 96-bit-strong cryptographic hash.
if substring base-64-encoded string 16 characters fundamentally different keeping first 12 bytes of hash function , base-64-encoding them? if so, please explain (provide example code) truncating byte array?
i tested substring of full hash value against 36,000+ distinct values , had no collisions. code below current implementation.
thanks (and clarity) can provide.
public static byte[] createsha256hash(string data) { byte[] datatohash = (new unicodeencoding()).getbytes(data); sha256 sham = new sha256managed(); byte[] hasheddata = sham.computehash(datatohash); return hasheddata; } public override void inputbuffer_processinputrow(inputbufferbuffer row) { byte[] hasheddata = createsha256hash(row.hashstring); string s = convert.tobase64string(hasheddata, base64formattingoptions.none); row.hashvalue = s.substring(0, 16); }
[original post] (http://stackoverflow.com/questions/4340471/is-there-a-hash-algorithm-that-produces-a-hash-size-of-64-bits-in-c)
no, there no difference. however, it's easier base64 string of first 12 bytes of array, instead of truncating array:
public override void inputbuffer_processinputrow(inputbufferbuffer row) { byte[] hasheddata = createsha256hash(row.hashstring); row.hashvalue = convert.tobase64string(hasheddata, 0, 12); }
the base 64 encoding puts 6 bits in each character, 3 bytes (24 bits) goes 4 characters. long splitting data @ 3 byte boundary, it's same splitting string @ 4 character boundary.
if try split data between these boundaries, base64 string padded filler data next boundary, result not same.
- Get link
- X
- Other Apps
Comments
Post a Comment