Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The same string will generate different md5sums by python and md5sum tool.

50 views
Skip to first unread message

hongy...@gmail.com

unread,
Oct 12, 2021, 3:35:35 AM10/12/21
to
For short, as shown below:

$ md5sum <<< 'TASCJO3RJMVKWDJKXLZM'
e1b8fa8fe09c295811063bb76cefc337 -

$ ipython
In [1]: from hashlib import md5

In [2]: my_str='TASCJO3RJMVKWDJKXLZM'

In [3]: md5(my_str.encode('utf-8')).hexdigest()
Out[3]: 'e9032994dabac08080091151380478a2'

Any hints for this behavior?

Regards,
HZ

Ben Bacarisse

unread,
Oct 12, 2021, 6:25:43 AM10/12/21
to
>>> md5("TASCJO3RJMVKWDJKXLZM\n".encode('utf-8')).hexdigest()
'e1b8fa8fe09c295811063bb76cefc337'

--
Ben.

hongy...@gmail.com

unread,
Oct 12, 2021, 8:03:58 AM10/12/21
to
Thank you for pointing this out. I confirmed it with the following command:

$ cat -A <<< 'TASCJO3RJMVKWDJKXLZM'
TASCJO3RJMVKWDJKXLZM$

But how to supply string for md5sum without '\n'?

HZ

hongy...@gmail.com

unread,
Oct 12, 2021, 8:19:28 AM10/12/21
to
I see. Here it is:

$ echo -n 'TASCJO3RJMVKWDJKXLZM' | md5sum
e9032994dabac08080091151380478a2 -

HZ

hongy...@gmail.com

unread,
Oct 12, 2021, 8:21:02 AM10/12/21
to
So, it seems that the `<<<' will also append `\n' at the end of the string.

HZ

David W. Hodgins

unread,
Oct 12, 2021, 9:08:30 AM10/12/21
to
$ echo -n 'TASCJO3RJMVKWDJKXLZM' |md5sum
e9032994dabac08080091151380478a2 -

Regards, Dave Hodgins

--
Change dwho...@nomail.afraid.org to davidw...@teksavvy.com for
email replies.

William Unruh

unread,
Oct 12, 2021, 9:12:10 AM10/12/21
to
From man bash

Here Strings
A variant of here documents, the format is:

[n]<<<word

The word undergoes tilde expansion, parameter and variable expansion, command substitu-
tion, arithmetic expansion, and quote removal. Pathname expansion and word splitting are
not performed. The result is supplied as a single string, with a newline appended, to
the command on its standard input (or file descriptor n if n is specified).


Note the second last line.

Christian Weisgerber

unread,
Oct 12, 2021, 9:30:08 AM10/12/21
to
On 2021-10-12, hongy...@gmail.com <hongy...@gmail.com> wrote:

> But how to supply string for md5sum without '\n'?

$ printf '%s' 'TASCJO3RJMVKWDJKXLZM' | md5
e9032994dabac08080091151380478a2

--
Christian "naddy" Weisgerber na...@mips.inka.de

Ed Morton

unread,
Oct 12, 2021, 10:15:21 AM10/12/21
to
Right, it's saving a string into a temp file and than using that as the
input to the command so, since every line in a POSIX text file must end
in `\n`, `<<<` has to add one. It's equivalent to manually writing:

tmp=$(mktemp) &&
printf '%s\n' 'TASCJO3RJMVKWDJKXLZM' > "$tmp" &&
md5sum < "$tmp"
rm -f -- "$tmp"

Regards,

Ed.

Helmut Waitzmann

unread,
Oct 12, 2021, 2:23:56 PM10/12/21
to
Ed Morton <morto...@gmail.com>:

>Right, it's saving a string into a temp file and than using that as
>the input to the command so, since every line in a POSIX text file
>must end in `\n`, `<<<` has to add one. It's equivalent to manually
>writing:
>
> tmp=$(mktemp) &&
> printf '%s\n' 'TASCJO3RJMVKWDJKXLZM' > "$tmp" &&
> md5sum < "$tmp"
> rm -f -- "$tmp"

On my debian linux machine it's more like


(
tmp="$(mktemp)" &&
printf '%s\n' 'TASCJO3RJMVKWDJKXLZM' > "$tmp" &&
exec 0< "$tmp" &&
rm -f -- "$tmp" &&
exec md5sum
)

hongy...@gmail.com

unread,
Oct 12, 2021, 10:53:43 PM10/12/21
to
On Tuesday, October 12, 2021 at 9:30:08 PM UTC+8, Christian Weisgerber wrote:
> On 2021-10-12, hongy...@gmail.com <hongy...@gmail.com> wrote:
> > But how to supply string for md5sum without '\n'?
> $ printf '%s' 'TASCJO3RJMVKWDJKXLZM' | md5

I don't have md5 command, but instead md5sum.

hongy...@gmail.com

unread,
Oct 12, 2021, 11:06:10 PM10/12/21
to
I'm on Ubuntu 20.04.2 LTS, both the above two methods work, but why do you prefer the latter?

HZ

hongy...@gmail.com

unread,
Oct 12, 2021, 11:07:53 PM10/12/21
to

Geoff Clare

unread,
Oct 14, 2021, 8:41:05 AM10/14/21
to
Only if you use a version of echo that does not conform to UNIX
requirements (even though it may conform to POSIX requirements).
POSIX allows -n to be special, but UNIX does not. A UNIX-conforming
echo does this:

$ echo -n foo
-n foo
$

The portable way to omit the newline is to use printf.

--
Geoff Clare <net...@gclare.org.uk>

hongy...@gmail.com

unread,
Oct 15, 2021, 12:41:05 AM10/15/21
to
Thank you for pointing this out.

HZ
0 new messages