Help getting the md5 module to work

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

    Help getting the md5 module to work

    I'm trying to use the md5 module but it gives me a different result from
    what other programs give. This is the code I have and the result from
    hexdigest() never matches what I expect. Its probably something stupid
    but its driving me crazy.

    f = file (filename, "r")
    m = md5.new()
    data = f.read (CHUNK_SIZE)
    while data != "":
    m.update (data)
    data = f.read (CHUNK_SIZE)
  • Carl Banks

    #2
    Re: Help getting the md5 module to work

    Grumfish wrote:[color=blue]
    > I'm trying to use the md5 module but it gives me a different result from
    > what other programs give. This is the code I have and the result from
    > hexdigest() never matches what I expect. Its probably something stupid
    > but its driving me crazy.
    >
    > f = file (filename, "r")
    > m = md5.new()
    > data = f.read (CHUNK_SIZE)
    > while data != "":
    > m.update (data)
    > data = f.read (CHUNK_SIZE)[/color]


    I'll wager this is happening on Windows. When reading data in text
    mode on Windows, Python converts "\r\n" sequence to "\n". You need to
    open the file in binary mode. Try changing the first line to:

    f = file(filename," rb")




    --
    CARL BANKS http://www.aerojockey.com/software
    "If you believe in yourself, drink your school, stay on drugs, and
    don't do milk, you can get work."
    -- Parody of Mr. T from a Robert Smigel Cartoon

    Comment

    • Amy G

      #3
      Re: Help getting the md5 module to work

      I have never used the md5 module that python offers... but after reading
      this post I thought I would try it out.
      [color=blue][color=green][color=darkred]
      >>> n = md5.new()
      >>> n.update("help" )
      >>> n.hexdigest()[/color][/color][/color]
      '657f8b8da628ef 83cf69101b68171 50a'

      But I run FreeBSD 5.0 and when I execute
      $ echo help | openssl md5
      45b758a4f518f3f f31363696132f5f 5a

      What gives? What am I missing here?

      Thanks in advance for shedding some light on my "easy" question.


      Carl Banks" <imbosol@aerojo ckey.invalid> wrote in message
      news:m1O6c.2577 4$P45.20864@fe1 .columbus.rr.co m...[color=blue]
      > Grumfish wrote:[color=green]
      > > I'm trying to use the md5 module but it gives me a different result from
      > > what other programs give. This is the code I have and the result from
      > > hexdigest() never matches what I expect. Its probably something stupid
      > > but its driving me crazy.
      > >
      > > f = file (filename, "r")
      > > m = md5.new()
      > > data = f.read (CHUNK_SIZE)
      > > while data != "":
      > > m.update (data)
      > > data = f.read (CHUNK_SIZE)[/color]
      >
      >
      > I'll wager this is happening on Windows. When reading data in text
      > mode on Windows, Python converts "\r\n" sequence to "\n". You need to
      > open the file in binary mode. Try changing the first line to:
      >
      > f = file(filename," rb")
      >
      >
      >
      >
      > --
      > CARL BANKS http://www.aerojockey.com/software
      > "If you believe in yourself, drink your school, stay on drugs, and
      > don't do milk, you can get work."
      > -- Parody of Mr. T from a Robert Smigel Cartoon[/color]


      Comment

      • Paul Rubin

        #4
        Re: Help getting the md5 module to work

        "Amy G" <amy-g-art@cox.net> writes:
        [color=blue][color=green][color=darkred]
        > >>> n.update("help" )
        > >>> n.hexdigest()[/color][/color]
        > '657f8b8da628ef 83cf69101b68171 50a'
        >
        > But I run FreeBSD 5.0 and when I execute
        > $ echo help | openssl md5
        > 45b758a4f518f3f f31363696132f5f 5a
        >
        > What gives? What am I missing here?
        >
        > Thanks in advance for shedding some light on my "easy" question.[/color]

        Try n.update("help\ n").

        Comment

        • Erik Max Francis

          #5
          Re: Help getting the md5 module to work

          Paul Rubin wrote:
          [color=blue]
          > Try n.update("help\ n").[/color]

          Right. For Amy: You can suppress the newline on echo with the -n
          option, by the way:

          max@oxygen:~% echo -n help | md5sum
          657f8b8da628ef8 3cf69101b681715 0a -

          --
          __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
          / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
          \__/ We are circumstances of our birth.
          -- Sade Adu

          Comment

          • Amy G

            #6
            Re: Help getting the md5 module to work


            "Erik Max Francis" <max@alcyone.co m> wrote in message
            news:405BFC92.E 1C8C186@alcyone .com...[color=blue]
            > Paul Rubin wrote:
            >[color=green]
            > > Try n.update("help\ n").[/color]
            >
            > Right. For Amy: You can suppress the newline on echo with the -n
            > option, by the way:
            >[/color]
            I discovered this last night.

            But what is the true MD5 hash for help? With or without the newline?



            Comment

            • Peter Hansen

              #7
              Re: Help getting the md5 module to work

              Amy G wrote:[color=blue]
              > "Erik Max Francis" <max@alcyone.co m> wrote in message
              > news:405BFC92.E 1C8C186@alcyone .com...
              >[color=green]
              >>Paul Rubin wrote:
              >>
              >>[color=darkred]
              >>>Try n.update("help\ n").[/color]
              >>
              >>Right. For Amy: You can suppress the newline on echo with the -n
              >>option, by the way:
              >>[/color]
              >
              > I discovered this last night.
              >
              > But what is the true MD5 hash for help? With or without the newline?[/color]

              You already had the right hashes for both! Your Python attempt did
              not include a newline, while the shell approach with "echo" did, so you
              were comparing apples and oranges.

              Paul and Erik gave you the solutions to switch either approach to do
              what the other does...

              -Peter

              Comment

              • Erik Max Francis

                #8
                Re: Help getting the md5 module to work

                Amy G wrote:
                [color=blue]
                > I discovered this last night.
                >
                > But what is the true MD5 hash for help? With or without the newline?[/color]

                It's completely up to you. It's your hash, you decide what goes in it.
                All that's important is whoever's comparing hashes with yours (for
                whatever reason) is doing it consistently.

                --
                __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
                \__/ I'm spreading my wings and I fly / I'm still as the night
                -- Chante Moore

                Comment

                Working...