md5 from python different then md5 from command line

Discussion in 'Python' started by ursache.marius@gmail.com, May 7, 2006.

  1. Guest

    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
    0f0f60ac801a9eec2163083a22307deb -

    >>> test = md5.new("marius")
    >>> print test.hexdigest()

    242aa1a97769109065e3b4df359bcfc9


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

    Thanks
    , May 7, 2006
    #1
    1. Advertising

  2. said the following on 07.05.2006 12:07:
    > 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
    > 0f0f60ac801a9eec2163083a22307deb -
    >
    >>>> test = md5.new("marius")
    >>>> print test.hexdigest()

    > 242aa1a97769109065e3b4df359bcfc9
    >
    >
    > Any idea why? and how to get the same md5 sum for both calls?
    >
    > Thanks
    >


    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
    Dejan Rodiger, May 7, 2006
    #2
    1. Advertising

  3. Just Guest

    In article <>,
    "" <> wrote:

    > 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
    > 0f0f60ac801a9eec2163083a22307deb -
    >
    > >>> test = md5.new("marius")
    > >>> print test.hexdigest()

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


    echo adds a newline:

    >>> import md5
    >>> test = md5.new("marius\n")
    >>> print test.hexdigest()

    0f0f60ac801a9eec2163083a22307deb

    Just
    Just, May 7, 2006
    #3
  4. > echo adds a newline:
    >
    > >>> import md5
    > >>> test = md5.new("marius\n")
    > >>> print test.hexdigest()

    > 0f0f60ac801a9eec2163083a22307deb
    >
    > Just



    Thanks, that was it ;)
    Marius Ursache, May 7, 2006
    #4
  5. Paul Rubin Guest

    "Marius Ursache" <> writes:
    > > >>> import md5
    > > >>> test = md5.new("marius\n")
    > > >>> print test.hexdigest()

    > > 0f0f60ac801a9eec2163083a22307deb

    >
    > Thanks, that was it ;)


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

    $ echo -n marius | md5sum
    242aa1a97769109065e3b4df359bcfc9 -
    Paul Rubin, May 7, 2006
    #5
  6. John Salerno Guest

    wrote:
    > 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
    > 0f0f60ac801a9eec2163083a22307deb -
    >
    >>>> test = md5.new("marius")
    >>>> print test.hexdigest()

    > 242aa1a97769109065e3b4df359bcfc9
    >
    >
    > Any idea why? and how to get the same md5 sum for both calls?
    >
    > Thanks
    >


    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.
    John Salerno, May 7, 2006
    #6
  7. Paul Rubin Guest

    John Salerno <> writes:
    > 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?


    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()
    Paul Rubin, May 7, 2006
    #7
  8. John Salerno Guest

    Paul Rubin wrote:
    > John Salerno <> writes:
    >> 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?

    >
    > 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()


    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.
    John Salerno, May 8, 2006
    #8
  9. Paul Rubin Guest

    John Salerno <> writes:
    > Any reason you can't just read the whole file at once and update m?


    Yes, you could say

    print md5.new(file('foo.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.

    > 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?


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

    len('ab\0cd') ==> 5
    Paul Rubin, May 8, 2006
    #9
  10. John Salerno Guest

    Paul Rubin wrote:
    > John Salerno <> writes:
    >> Any reason you can't just read the whole file at once and update m?

    >
    > Yes, you could say
    >
    > print md5.new(file('foo.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.
    >
    >> 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?

    >
    > Yes, t would still be a string. You can have NUL bytes and so forth
    > in Python strings:
    >
    > len('ab\0cd') ==> 5


    Thanks! I didn't expect it to be so easy. :)
    John Salerno, May 8, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    1,708
    Terry Hancock
    Apr 23, 2005
  2. Smurff
    Replies:
    10
    Views:
    2,408
    Keith Thompson
    Nov 21, 2008
  3. Replies:
    0
    Views:
    273
  4. Peter Woodsky

    create a md5 / md5 passwd with a salt

    Peter Woodsky, Nov 20, 2008, in forum: Ruby
    Replies:
    6
    Views:
    213
    Brian Candler
    Nov 21, 2008
  5. John Budd
    Replies:
    4
    Views:
    110
    Raphael Clancy
    Feb 13, 2009
Loading...

Share This Page