Sudantha
Sudantha

Reputation: 16214

Programmatically View image hash values

How to Programmatically View image hashes in C# or PHP ?

Upvotes: 1

Views: 314

Answers (2)

seriousdev
seriousdev

Reputation: 7656

In PHP you can simply use sha1_file() or hash_file() for better algos.

Upvotes: 3

Donut
Donut

Reputation: 112885

If you mean "get the hash of an Image file":

In C#, you can use the MD5CryptoServiceProvider class to calculate an MD5 hash. Here's an example function that uses this class and a filename to accomplish what you want (thanks to this page):

public string GetMD5HashFromFile(string fileName)
{
   FileStream file = new FileStream(fileName, FileMode.Open);
   MD5 md5 = new MD5CryptoServiceProvider();
   byte[] retVal = md5.ComputeHash(file);
   file.Close();

   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < retVal.Length; i++)
   {
     sb.Append(retVal[i].ToString("x2"));
   }
   return sb.ToString();
}

For PHP, you can just use the md5_file() function, e.g.:

$file = 'MyImage.jpg';

$hash = md5_file($file);

Note that both of these examples will work for any file type, not just image files.

Upvotes: 4

Related Questions