md5 from python different then md5 from command line

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ursache.marius@gmail.com

    #1

    md5 from python different then md5 from command line

    Hi

    I noticed that the md5 computed with md5 module from python is
    different then the md5 sum computed with md5sum utility (on slackware
    and gentoo).

    i.e.
    $echo marius|md5sum
    0f0f60ac801a9ee c2163083a22307d eb -
    [color=blue][color=green][color=darkred]
    >>> test = md5.new("marius ")
    >>> print test.hexdigest( )[/color][/color][/color]
    242aa1a97769109 065e3b4df359bcf c9


    Any idea why? and how to get the same md5 sum for both calls?

    Thanks

  • Just

    #2
    Re: md5 from python different then md5 from command line

    In article <1146996448.577 162.283480@j73g 2000cwa.googleg roups.com>,
    "ursache.marius @gmail.com" <ursache.marius @gmail.com> wrote:
    [color=blue]
    > I noticed that the md5 computed with md5 module from python is
    > different then the md5 sum computed with md5sum utility (on slackware
    > and gentoo).
    >
    > i.e.
    > $echo marius|md5sum
    > 0f0f60ac801a9ee c2163083a22307d eb -
    >[color=green][color=darkred]
    > >>> test = md5.new("marius ")
    > >>> print test.hexdigest( )[/color][/color]
    > 242aa1a97769109 065e3b4df359bcf c9
    >
    >
    > Any idea why? and how to get the same md5 sum for both calls?[/color]

    echo adds a newline:
    [color=blue][color=green][color=darkred]
    >>> import md5
    >>> test = md5.new("marius \n")
    >>> print test.hexdigest( )[/color][/color][/color]
    0f0f60ac801a9ee c2163083a22307d eb

    Just

    Comment

    • Dejan Rodiger

      #3
      Re: md5 from python different then md5 from command line

      ursache.marius@ gmail.com said the following on 07.05.2006 12:07:[color=blue]
      > Hi
      >
      > I noticed that the md5 computed with md5 module from python is
      > different then the md5 sum computed with md5sum utility (on slackware
      > and gentoo).
      >
      > i.e.
      > $echo marius|md5sum
      > 0f0f60ac801a9ee c2163083a22307d eb -
      >[color=green][color=darkred]
      >>>> test = md5.new("marius ")
      >>>> print test.hexdigest( )[/color][/color]
      > 242aa1a97769109 065e3b4df359bcf c9
      >
      >
      > Any idea why? and how to get the same md5 sum for both calls?
      >
      > Thanks
      >[/color]

      try md5sum marius
      probably "new line" character is not computed in "echo marius|md5sum"


      --
      Dejan Rodiger - PGP ID 0xAC8722DC
      Delete wirus from e-mail address

      Comment

      • Marius Ursache

        #4
        Re: md5 from python different then md5 from command line

        > echo adds a newline:[color=blue]
        >[color=green][color=darkred]
        > >>> import md5
        > >>> test = md5.new("marius \n")
        > >>> print test.hexdigest( )[/color][/color]
        > 0f0f60ac801a9ee c2163083a22307d eb
        >
        > Just[/color]


        Thanks, that was it ;)

        Comment

        • Paul Rubin

          #5
          Re: md5 from python different then md5 from command line

          "Marius Ursache" <ursache.marius @gmail.com> writes:[color=blue][color=green][color=darkred]
          > > >>> import md5
          > > >>> test = md5.new("marius \n")
          > > >>> print test.hexdigest( )[/color]
          > > 0f0f60ac801a9ee c2163083a22307d eb[/color]
          >
          > Thanks, that was it ;)[/color]

          Also, the -n option suppresses the newline from echo:

          $ echo -n marius | md5sum
          242aa1a97769109 065e3b4df359bcf c9 -

          Comment

          • John Salerno

            #6
            Re: md5 from python different then md5 from command line

            ursache.marius@ gmail.com wrote:[color=blue]
            > Hi
            >
            > I noticed that the md5 computed with md5 module from python is
            > different then the md5 sum computed with md5sum utility (on slackware
            > and gentoo).
            >
            > i.e.
            > $echo marius|md5sum
            > 0f0f60ac801a9ee c2163083a22307d eb -
            >[color=green][color=darkred]
            >>>> test = md5.new("marius ")
            >>>> print test.hexdigest( )[/color][/color]
            > 242aa1a97769109 065e3b4df359bcf c9
            >
            >
            > Any idea why? and how to get the same md5 sum for both calls?
            >
            > Thanks
            >[/color]

            Just a quick md5-related follow-up question: I was experimenting with it
            and making md5 sums for strings, but how do you use the md5 module to
            create a sum for an actual file, such as an .exe file?

            Thanks.

            Comment

            • Paul Rubin

              #7
              Re: md5 from python different then md5 from command line

              John Salerno <johnjsal@NOSPA Mgmail.com> writes:[color=blue]
              > Just a quick md5-related follow-up question: I was experimenting with
              > it and making md5 sums for strings, but how do you use the md5 module
              > to create a sum for an actual file, such as an .exe file?[/color]

              m = md5.new()
              f = file('foo.exe', 'b') # open in binary mode
              while True:
              t = f.read(1024)
              if len(t) == 0: break # end of file
              m.update(t)
              print m.hexdigest()

              Comment

              • John Salerno

                #8
                Re: md5 from python different then md5 from command line

                Paul Rubin wrote:[color=blue]
                > John Salerno <johnjsal@NOSPA Mgmail.com> writes:[color=green]
                >> Just a quick md5-related follow-up question: I was experimenting with
                >> it and making md5 sums for strings, but how do you use the md5 module
                >> to create a sum for an actual file, such as an .exe file?[/color]
                >
                > m = md5.new()
                > f = file('foo.exe', 'b') # open in binary mode
                > while True:
                > t = f.read(1024)
                > if len(t) == 0: break # end of file
                > m.update(t)
                > print m.hexdigest()[/color]

                Any reason you can't just read the whole file at once and update m?

                Also, doesn't the parameter for update have to be a string? If you're
                reading the file in binary mode, would t still be a string?

                Thanks.

                Comment

                • Paul Rubin

                  #9
                  Re: md5 from python different then md5 from command line

                  John Salerno <johnjsal@NOSPA Mgmail.com> writes:[color=blue]
                  > Any reason you can't just read the whole file at once and update m?[/color]

                  Yes, you could say

                  print md5.new(file('f oo.exe').read() ).hexdigest()


                  but that means reading the whole file into memory at once. If the
                  file is very large, that could thrash or fail.
                  [color=blue]
                  > Also, doesn't the parameter for update have to be a string? If you're
                  > reading the file in binary mode, would t still be a string?[/color]

                  Yes, t would still be a string. You can have NUL bytes and so forth
                  in Python strings:

                  len('ab\0cd') ==> 5

                  Comment

                  • John Salerno

                    #10
                    Re: md5 from python different then md5 from command line

                    Paul Rubin wrote:[color=blue]
                    > John Salerno <johnjsal@NOSPA Mgmail.com> writes:[color=green]
                    >> Any reason you can't just read the whole file at once and update m?[/color]
                    >
                    > Yes, you could say
                    >
                    > print md5.new(file('f oo.exe').read() ).hexdigest()
                    >
                    >
                    > but that means reading the whole file into memory at once. If the
                    > file is very large, that could thrash or fail.
                    >[color=green]
                    >> Also, doesn't the parameter for update have to be a string? If you're
                    >> reading the file in binary mode, would t still be a string?[/color]
                    >
                    > Yes, t would still be a string. You can have NUL bytes and so forth
                    > in Python strings:
                    >
                    > len('ab\0cd') ==> 5[/color]

                    Thanks! I didn't expect it to be so easy. :)

                    Comment

                    Working...