Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:49:35+00:00 2026-06-13T20:49:35+00:00

I generate a MD5 Hash from a String and from a File Containing the

  • 0

I generate a MD5 Hash from a String and from a File Containing the Same String using System.Security.Cryptography.MD5. However the Hash Values Differ.

Here is the code to generate from a string

byte[] data = Encoding.ASCII.GetBytes("The quick brown fox jumps over the lazy dog");
byte[] hash = MD5.Create().ComputeHash(data);
return BitConverter.ToString(hash).Replace("-", "").ToLower();

And Here is the code when I generate the hash from the file

internal static string CalculateFileHashTotal(string fileLocation) 
    {
        using(var md5 = MD5.Create())
        {
            using (var stream = File.OpenRead(fileLocation))
            {
                byte[] b = md5.ComputeHash(stream);
                stream.Close();
                return BitConverter.ToString(b).Replace("-", "").ToLower();
            }
        }
    }

The Hash from the string is correct, So I assume the Hash from the file reads some extra stuffs or doesn’t read the entire file. I couldn’t find an answer on Google.

Any Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:49:37+00:00Added an answer on June 13, 2026 at 8:49 pm

    The hash differs because the data differs.

    The file is UTF-8, not ASCII, so you should use the UTF-8 encoding to convert the string to bytes to get the same result:

    byte[] data = Encoding.UTF8.GetBytes("The quick brown fox jumps over the lazy dog");
    

    Also, the file may contain a BOM (byte order mark) at the beginning. That is included in the data, as the file isn’t read as text.

    Adding the UTF-8 BOM at the beginning of the data would give the same hash:

    byte[] bom = { 239, 187, 191 };
    byte[] data = Encoding.UTF8.GetBytes("The quick brown fox jumps over the lazy dog");
    
    byte[] bomdata = new byte[bom.Length + data.Length];
    bom.CopyTo(bomdata, 0);
    data.CopyTo(bomdata, bom.Length);
    byte[] hash = MD5.Create().ComputeHash(bomdata);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that hash input string to generate MD5 pass from it
when using oracle forms to generate md5 hash, i get result that is different
Possible Duplicate: Generate a Hash from string in Javascript/jQuery Can anyone suggest a simple
I want to generate the hash code from a string,e.g.咖啡,but the hashcode I get
I am trying to generate a Unix-Style password hash using MD5. I undestand that
How can I generate a hash with let's say md5 from set of mixed
i use Force MD5 encryption of the password in htpasswd to generate a hash
I'm using MD5 function and Base64 Encoding to generate a User Secret (used to
I'm trying to compare two UIImages from the file system to see if they
Possible Duplicate: Generate MD5 hash in Java Hi, I want to compute the MD5

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.