nsutgio
nsutgio

Reputation: 240

Can we convert a file of any formats into bytes, hash it, save it and be able to retrieve it in C#?

I know this might be quite complicated, but I'm just looking for opinions about this idea. I'm planning to save a specific file in a database, then I was wondering if how could I make it more secure. So I think of converting it into bytes, perform a hashing algorithm, then save it in my database. But my problem with this is that, I'm not sure, and I have no idea if this is the right way to do it. And can I be able to retrieve those files to its original phase? Hope someone could help. Thanks!

Upvotes: 0

Views: 565

Answers (2)

Eric J.
Eric J.

Reputation: 150228

You can surely hash a file and save the hash value.

Have a look at HashAlgorithm.ComputeHash

http://msdn.microsoft.com/en-us/library/xa627k19.aspx

The MSDN example gets the point across but is flawed:

FileStream fileStream = fInfo.Open(FileMode.Open);
// Be sure it's positioned to the beginning of the stream.
fileStream.Position = 0;
// Compute the hash of the fileStream.
hashValue = myRIPEMD160.ComputeHash(fileStream);

Please be sure and dispose of things like FileStream that implement IDisposable.

UPDATE

Just to be clear... the hash is useful to validate the file has not been changed/tampered with since the hash was created.

You still must store the file itself. You cannot re-create the file from the hash.

Upvotes: 2

Stephen Wrighton
Stephen Wrighton

Reputation: 37880

If you're talking about performing a cryptographic hash in order to make it more secure (i.e. encrypted) the problem with that, is that it is non-recoverable.

If you want to encrypt the file, then I would look into using AES (Simple insecure two-way "obfuscation" for C#)

Now, if you're creating a hash check value (or MD5 Checksum) of the file for later comparison purposes that's perfectly acceptable and do-able as per Eric's post

Upvotes: 1

Related Questions