Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an MD5 hash to a string and use it as a file name

Tags:

c#

md5

I am taking the MD5 hash of an image file and I want to use the hash as a filename.

How do I convert the hash to a string that is valid filename?

EDIT: toString() just gives "System.Byte[]"

like image 543
Malcolm Avatar asked Apr 17 '09 12:04

Malcolm


People also ask

Can we convert MD5 to string?

You cannot reverse the MD5 function, so your only option is to generate a new password and send that to the user (preferably over some secure channel).

Can you generate a file from a hash?

It is possible to create a file given a hash value. However, if you have a file and create a MD5 hash this process will not recreate that file but create a small file filled with gibberish.

Does MD5 change with filename?

The usual definition of "MD5 hash of a file" is that the hash is based on the file contents. The name can be freely changed.

Does MD5 hash include metadata?

In the eDiscovery process, they are usually used to spot exact duplicates of files, as two identical files will have the same MD5 hash values (and thus the same digital signature.) The values are based on the low-level, binary data of files and not directly on the textual content or metadata of the file.


1 Answers

How about this:

string filename = BitConverter.ToString(yourMD5ByteArray);

If you prefer a shorter filename without hyphens then you can just use:

string filename =
    BitConverter.ToString(yourMD5ByteArray).Replace("-", string.Empty);
like image 100
LukeH Avatar answered Nov 15 '22 09:11

LukeH