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 7041379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:02:13+00:00 2026-05-28T02:02:13+00:00

I am trying to create a md5 string from the byte array of an

  • 0

I am trying to create a md5 string from the byte array of an image uploaded by users at my web application..
This is because i want the images spread out in different folders.
And I dont have to use the userID as the folder name. Looks more professional.

The result would be something like:

/images/ 'first-two-char-of-md5' / 'the-complete-md5-string'.[jpg,png,bmp....]

Does this sound as a good solution to handle the images?

So.
My code ( Stuff from the internet.):

protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            if (CheckFileType(FileUpload1.FileName))
            {
                const int BUFFER_SIZE = 255;
                Byte[] Buffer = new Byte[BUFFER_SIZE];

                Stream theStream = FileUpload1.PostedFile.InputStream;
                nBytesRead = theStream.Read(Buffer, 0, BUFFER_SIZE);

                System.Text.ASCIIEncoding ASCIIEncoding = new ASCIIEncoding();
                System.Text.UTF8Encoding utf8 = new UTF8Encoding();
                //Just trying some stuff to see the output...
                Label1.Text =  ASCIIEncoding.GetString(CalculateMD5(theStream)) + "<br>" + utf8.GetString(CalculateMD5(theStream)) + "<br>" + Convert.ToBase64String(CalculateMD5(theStream));
             }
       }
  }


private static byte[] _emptyBuffer = new byte[0];

    public static byte[] CalculateMD5(Stream stream)
    {
        return CalculateMD5(stream, 64 * 1024);
    }

    public static byte[] CalculateMD5(Stream stream, int bufferSize)
    {
        MD5 md5Hasher = MD5.Create();

        byte[] buffer = new byte[bufferSize];
        int readBytes;

        while ((readBytes = stream.Read(buffer, 0, bufferSize)) > 0)
        {
            md5Hasher.TransformBlock(buffer, 0, readBytes, buffer, 0);
        }

        md5Hasher.TransformFinalBlock(_emptyBuffer, 0, 0);

        return md5Hasher.Hash;
    }

The result. I get some output from the “calculateMD5()” but when i am trying to put it to the label1. to see what is happening. there are just a bunch of wierd characters.
What am I doing wrong here? i want it to be htmlsafe… a-z, A-Z,0-9 only.

  • 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-05-28T02:02:14+00:00Added an answer on May 28, 2026 at 2:02 am

    The hash is being returned as an array of bytes. You need to convert this to a human readable form, e.g. 73868cb1848a216984dca1b6b0ee37bc. You can use something like the following:

    var s = new StringBuilder();
    foreach (byte b in md5Hasher.Hash)
        s.Append(b.ToString("x2").ToLower());
    return s.ToString();
    

    This iterates through the list of bytes returned from the hashing operation and converts each byte to hex. You can find more information on the format strings you can use for the byte type on MSDN.

    To answer the first part of your question:

    Does this sound as a good solution to handle the images?

    It should be sufficient, although if 2 users upload the same image then it will result in the same hash. You could try salting the data with the username and maybe a timestamp to mitigate this.

    You would also need some checking, because even though the probability is extremely small, you may generate the same hash (a ‘collision’) for different images/users, and you wouldn’t want a user to overwrite another user’s image. You could prevent this by generating a hash of the image, checking if it already exists, and if it does, add some bytes to the pre-hashed data, repeating until the hash is unique.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to import data from java web application to sugarCRM. I created client
I am trying to create a md5 hash for a string but the problem
I am trying to create an android MD5 hash string to equal the C#
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to create a small monitor application that displays current internet usage as percentage
I am trying to create an md5 value in php using the instruction given.
I am trying to create a md5 hash that I am comparing against a
Trying to create a script to read a remote file and check the md5
I'm trying create this function such that if any key besides any of the
I am trying create the image enlargement effect when you hover your mouse over

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.