1

Should I encode the data before hashing it to avoid Invalid length for a Base-64 char array.

# my .cs :

Stream des = file.InputStream;
byte[] data = new byte[file.ContentLength];
des.Read(data, 0, file.ContentLength);
FileStream f = new FileStream(targetFileName, FileMode.Create, FileAccess.ReadWrite);
f.Write(data, 0, data.Length);
f.Flush();
f.Close();
///////////////////////////////////////////////////////////////////////////////////////////
var sha = new System.Security.Cryptography.SHA512Managed();
string hash2 = Convert.ToBase64String(sha.ComputeHash(data));

and if the answer is yes How to encode the byte array.

Note:

I use hashing to uniquely identify the uploaded files for any change.

sehe
  • 374,641
  • 47
  • 450
  • 633
Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
  • 1
    @sehe while true, I don't think that relates to the context of the question, which is about **creating** a base-64 string. You don't pad inputs. – Marc Gravell Nov 29 '11 at 08:23
  • I face similar problem here http://stackoverflow.com/questions/4555249/invalid-length-for-a-base-64-char-array-during-decoding-decryption .. And i fair to face the same problem if i hach before encoding my data – Anyname Donotcare Nov 29 '11 at 08:24
  • 1
    where exactly do you get the "Invalid length for a base-64 char array" error? I'm pretty sure it isn't in the code shown... – Marc Gravell Nov 29 '11 at 08:26
  • yeah not in this code . Any expected exceptions from this code ?and i will store `hash2` in my database to identify if any change did happen to my uploaded file from the last failure , should i store the byte array before conversion or storing `hash2` perform the goal. – Anyname Donotcare Nov 29 '11 at 08:30
  • 1
    none; that should just work; storing `hash2` should work fine, as long as you store it robustly. – Marc Gravell Nov 29 '11 at 09:01
  • Thank U so much . i don't know what did u mean by `robustly`? – Anyname Donotcare Nov 29 '11 at 09:10
  • 1
    somewhere you are storing `hash2` yes? all I mean is that **when you fetch this value again**, double check (in a breakpoint etc) that you successfully got back **exactly** the same string that was produced by `ToBase64String`, and that you haven't damaged it during storage. If the string is intact, it will work fine. If it is erroring, the string has almost certainly not been stored/fetched correctly, but that is **unrelated** to the fact that it is base-64, and simply relates to your storage code. – Marc Gravell Nov 29 '11 at 09:25
  • I see, thanks U so much. – Anyname Donotcare Nov 29 '11 at 09:38

1 Answers1

3

That error happens when decoding base-64, since a valid base-64 string is always a multiple of 4 characters (sometimes with a few padding chars). No, you do not need to encode/pad the input to ToBase64String. Rather, it sounds like you are losing some characters when you are transporting / storing it (are you perhaps passing it on a query-string, and having a + vs space issue?)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900