MD5 Large Files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chis2k

    #1

    MD5 Large Files

    I need help figuring out how to get the MD5 hash of large files.
    I cannot go about the traditional way of reading the entire file into a byte
    array, because then the huge file is placed in memory, and bye bye system
    resources! I know there might be way using streams, but I am unsure how to
    implement it. Can anyone give me an example?

    I've tried this but it didn't work, it still loaded the entire blasted thing
    into memory!.

    public byte[] GetMD5HashFromF ile( string file_name) {
    FileStream file = new FileStream( file_name, FileMode.Open );
    MD5 md5 = new MD5CryptoServic eProvider();
    byte[] retVal = md5.ComputeHash (file);
    file.Close();
    return retVal;
    }


  • Austin Ehlers

    #2
    Re: MD5 Large Files

    On Fri, 25 Jul 2003 15:03:38 -0700, "chis2k" <sfd@sdf.com> wrote:
    [color=blue]
    >I need help figuring out how to get the MD5 hash of large files.
    >I cannot go about the traditional way of reading the entire file into a byte
    >array, because then the huge file is placed in memory, and bye bye system
    >resources! I know there might be way using streams, but I am unsure how to
    >implement it. Can anyone give me an example?
    >
    >I've tried this but it didn't work, it still loaded the entire blasted thing
    >into memory!.
    >
    >public byte[] GetMD5HashFromF ile( string file_name) {
    > FileStream file = new FileStream( file_name, FileMode.Open );
    > MD5 md5 = new MD5CryptoServic eProvider();
    > byte[] retVal = md5.ComputeHash (file);
    > file.Close();
    > return retVal;
    >}
    >[/color]

    The ComputeHash method on the MD5 class can take a stream. My code is
    this:

    StringBuilder sb=new StringBuilder() ;
    FileStream fs=new FileStream(file name,FileMode.O pen);
    MD5 md5=new MD5CryptoServic eProvider();
    byte[] hash=md5.Comput eHash(fs);
    fs.Close();
    foreach (byte hex in hash)
    sb.Add(hex.ToSt ring("x2"));
    string md5sum=sb.ToStr ing();

    Comment

    • Austin Ehlers

      #3
      Re: MD5 Large Files

      On Fri, 25 Jul 2003 20:53:36 -0500, Austin Ehlers
      <the*remove*bor ed*me*guy16@hot mail.com> wrote:
      [color=blue]
      >On Fri, 25 Jul 2003 15:03:38 -0700, "chis2k" <sfd@sdf.com> wrote:
      >[color=green]
      >>I need help figuring out how to get the MD5 hash of large files.
      >>I cannot go about the traditional way of reading the entire file into a byte
      >>array, because then the huge file is placed in memory, and bye bye system
      >>resources! I know there might be way using streams, but I am unsure how to
      >>implement it. Can anyone give me an example?
      >>
      >>I've tried this but it didn't work, it still loaded the entire blasted thing
      >>into memory!.
      >>
      >>public byte[] GetMD5HashFromF ile( string file_name) {
      >> FileStream file = new FileStream( file_name, FileMode.Open );
      >> MD5 md5 = new MD5CryptoServic eProvider();
      >> byte[] retVal = md5.ComputeHash (file);
      >> file.Close();
      >> return retVal;
      >>}
      >>[/color]
      >
      >The ComputeHash method on the MD5 class can take a stream. My code is
      >this:
      >
      >StringBuilde r sb=new StringBuilder() ;
      >FileStream fs=new FileStream(file name,FileMode.O pen);
      >MD5 md5=new MD5CryptoServic eProvider();
      >byte[] hash=md5.Comput eHash(fs);
      >fs.Close();
      >foreach (byte hex in hash)
      > sb.Add(hex.ToSt ring("x2"));
      >string md5sum=sb.ToStr ing();[/color]

      I just noticed we have the same code. I think you're mistaken when
      you say that it reads the entire file into memory. A quick test I did
      had my MD5 winform using only 19MB of memory when processing a 140MB
      file.

      Austin Ehlers

      Comment

      Working...