Title: hashlib command line interface
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.6
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: SilentGhost, avalentino, christian.heimes, gregory.p.smith, gvanrossum, haypo, martin.panter, palaviv, rhettinger, terry.reedy
Priority: normal Keywords: patch

Created on 2016-03-05 21:54 by palaviv, last changed 2016-08-25 07:44 by rhettinger. This issue is now closed.

File name Uploaded Description Edit
hashlib-script-mod.patch palaviv, 2016-03-05 21:54 review
hashlib-script-mod-2.patch palaviv, 2016-03-30 13:03 review
hashlib-script-mod-md5sum-style.patch palaviv, 2016-03-31 18:36 review
hashlib-script-mod-md5sum-style-2.patch palaviv, 2016-03-31 20:46 review
hashlib-script-mod-md5sum-style-3.patch palaviv, 2016-04-01 08:52 review
hashlib-script-mod-md5sum-style-4.patch palaviv, 2016-04-01 20:43 review
hashlib-script-mod-md5sum-style-5.patch palaviv, 2016-04-02 08:39 review
Messages (29)
msg261225 - (view) Author: Aviv Palivoda (palaviv) * Date: 2016-03-05 21:54
I am suggesting to add a command line interface to the hashlib module. A simple example of the api I suggest is:
$ python -m hashlib md5 /bin/sh
$ d985d0ea551c1253c2305140c583d11f

A patch is included.
msg261611 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2016-03-11 21:51
This strikes me as a sensible addition.  While I did not review the patch in detail, it seems cleanly written and is complete in having a new main() for the code, a new doc section, and a new unittest Test class.
msg262544 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-03-28 01:24
FWIW I am not really comfortable with argparse.FileType; see Issue 13824. How does this patch perform when warnings are enabled, or if you specify a file, but then cause some other argparse error?
msg262565 - (view) Author: Aviv Palivoda (palaviv) * Date: 2016-03-28 08:24
I actually have noticed issue 13824 while working on this issue. The patch I uploaded to Issue 14156 actually fixes the problem in issue 13824 in addition to the problem with the stdin/stdout.
I think we should add a dependency in issue 14156 for this issue.
msg262613 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-03-29 15:29
Here is a quick check versus other command-line tools:

$ md5 xml_prolog.diff 
MD5 (xml_prolog.diff) = c30e1fe70651d5a472efe9598db70771
$ python3.6 -m hashlib md5 xml_prolog.diff 
$ shasum -a 1 xml_prolog.diff 
db5fd60f3002ed270fe4469c7f343aba515d9b1a  xml_prolog.diff
$ python3.6 -m hashlib sha1 xml_prolog.diff 
$ shasum -a 224 xml_prolog.diff 
d0d18eb3e4d71269a610308e51e49d8b4650134bf2757dd22d69430b  xml_prolog.diff
$ python3.6 -m hashlib sha224 xml_prolog.diff 
$ shasum -a 256 xml_prolog.diff 
0bbd834589d5fc9e26e32e5c665de4a2fbd93ea3bb4688ea25ef1139cd152b09  xml_prolog.diff
$ python3.6 -m hashlib sha256 xml_prolog.diff 
$ shasum -a 384 xml_prolog.diff 
f627b6c2bdc95e1af00da2a4b5d3897284127d0e820963f60758e34e5cfeb64cb529bfb306789f73c91d58d7594a3f3b  xml_prolog.diff
$ python3.6 -m hashlib sha384 xml_prolog.diff 
$ shasum -a 512 xml_prolog.diff 
c93babfef5a25bb569e4fd5c6edbb6d5e1de92044576b68522abcd9d9a356eca6b791df97a08e5c75cd090f00d5f2f73d940b901796b190681d370e5da74e4d8  xml_prolog.diff
$ python3.6 -m hashlib sha512 xml_prolog.diff 
msg262614 - (view) Author: SilentGhost (SilentGhost) * Date: 2016-03-29 15:36
I've left some comment on rietveld yesterday, not sure if you got the e-mail.
msg262615 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-03-29 15:36
Overall, I'm +1 on the idea and think the patch looks good.  There are two issues to look at:

1) Neither the md5 or shasum command-line tools offer control over the blocksize.  I suggest that option be dropped from the command-line API giving a nice simplification and usability improvement.

The stdin option has issues:

$ md5

$ python3.6 -m hashlib md5
^CTraceback (most recent call last):
  File "/Users/raymond/cpython/Lib/", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/raymond/cpython/Lib/", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/raymond/cpython/Lib/", line 246, in <module>
  File "/Users/raymond/cpython/Lib/", line 238, in main
    data =
msg262627 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2016-03-29 21:30
About the compatibility with existing tools, I recall a discussion when the tarfile module got a CLI. First I expected a clone of the UNIX tar command, but it was decided to design a new *simpler* CLI.

$ python3 -m tarfile
usage: [-h] [-v] [-l <tarfile> | -e <tarfile> [<output_dir> ...] |
                  -c <name> [<file> ...] | -t <tarfile>]

A simple command line interface for tarfile module.

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         Verbose output
  -l <tarfile>, --list <tarfile>
                        Show listing of a tarfile
  -e <tarfile> [<output_dir> ...], --extract <tarfile> [<output_dir> ...]
                        Extract tarfile into target dir
  -c <name> [<file> ...], --create <name> [<file> ...]
                        Create tarfile from sources
  -t <tarfile>, --test <tarfile>
                        Test if a tarfile is valid

A common trap of the md5sum CLI is that users write "echo string|md5sum" which adds a newline to string. For an unknown reason, my french manual page of the md5sum command has a -s STRING/--string=STRING argument, but not my effective md5sum program. Maybe we should consider adding such option to avoid the trap?

Do you want to implement a function to compare computed hash to a file which contains the expected hash? Check for file integrity, md5sum -c FILE/--check=FILE. Example:
$ md5sum test_socket_with.patch > check
$ cat check 
cfc1d69e76c827c32af4f28f50714a5e  test_socket_with.patch

$ md5sum -c check
test_socket_with.patch: OK

$ vim test_socket_with.patch 
<modify something in the file>

$ md5sum -c check
test_socket_with.patch: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

I worked hard to release the GIL when a hash is released. It would be super cool (a killer feature?) to automatically spawn threads to compute the hash. For example, use N threads where N is the number of CPU (os.cpu_count() or 1). Last time I wrote my, it was much faster than the UNIX md5sum tool since it uses all my CPU cores. You should just ensure that output is written in the correct order.

Raymond wrote:
> 1) Neither the md5 or shasum command-line tools offer control over the blocksize.  I suggest that option be dropped from the command-line API giving a nice simplification and usability improvement.

I agree. You should compute it per file using os.stat().st_blksize:

The io module uses st_blksize if it is greater than 1, or 8 * 1024 bytes.

(By the way, it looks like shutil.copyfile() doesn't use st_blksize.)
msg262628 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2016-03-29 21:31
> I think we should add a dependency in issue 14156 for this issue.

Maybe you can start using strings for filename parameters and open files manually, and later consider switching back to FileType once it's fixed?
msg262663 - (view) Author: Aviv Palivoda (palaviv) * Date: 2016-03-30 13:03
Sorry on the late response for some reason i don't receive email notification from the tracker for the past few days.

1) Thanks for the review SilentGhost the patch attached include your CR suggestions.

2) Raymond I have fixed the problem with ctrl+D. I tried writing a test to simulate this problem but i don't seem to be able to simulated the terminal behavior on ctrl+D.

3) Removed the block_size option as suggested by Raymond and Victor and using os.stat().st_blksize as suggested by Victor.

4) I changed the CLI to support all available algorithms in hashlib. I am not sure if this is too many choices to show in the --help message.

5) About removing the use of argparse.FileType i would prefer resolving issue 14156 but if you think this is problematic i will do the change.

6) What do you think about changing the API to be more like md5sum?
    a) Allowing * in the file name to calcualte on multiple files.
    b) Adding the check option.
    c) printing file name in output.
msg262666 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2016-03-30 13:56
I see a potential performance issue here. The block size is a small value, usually a couple of kb. With such a small value, the runtime will be dominated by Python call overhead and syscalls.
msg262687 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-03-31 04:29
I concur.  The blocksize should be fixed and large (perhaps 256kB).
msg262699 - (view) Author: Aviv Palivoda (palaviv) * Date: 2016-03-31 18:36
I am adding a new patch with a API compatible to GNU md5sum:

$ python -m hashlib md5 /bin/sh
$ d985d0ea551c1253c2305140c583d11f /bin/sh

I will soon add the feature's requested by Victor:
  1) The check option.
  2) Running the hash calculation for different files in different threads.

If anyone would like any other feature he thinks will be helpful please post it.

If we choose to use this API we should remove the dependency on issue 14156.
msg262703 - (view) Author: SilentGhost (SilentGhost) * Date: 2016-03-31 19:34
The mailing system is acting up, so just for the record, I've left comments on rietveld regarding md5sum-style patch.
msg262705 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2016-03-31 20:10
Threading doesn't make much sense here. The runtime of hash computation is usually dominated by I/O performance. On a typical consumer computer SSDs have a sequential read performance of 200 to 500 MiB/sec. SHA-512 performance between 100 to 150 Mib/sec. Threading could make parallel computation a bit faster, but at the cost of a much more complex implementation. Let's keep it simple.

Both Python's hashlib implementation and OpenSSL aren't the best foundation if you are aiming for maximum performance. All libraries do couple of malloc()s, memcpy() and have additional overheads, too.

And please don't use MD5 in your examples. Go for sha256.
msg262706 - (view) Author: Aviv Palivoda (palaviv) * Date: 2016-03-31 20:43
Thanks for the review SilentGhost. I am including the patch after the changes from your CR comments.
msg262727 - (view) Author: Aviv Palivoda (palaviv) * Date: 2016-04-01 08:52
I am adding a new patch with changes from Martin CR (Thanks for the review) and support in the "check" option.
I also changed to examples in the Documentation to use sha256 instead of md5 as Christian asked. I left one example with sha1 so when someone read it he will see that other algorithms are supported.
As for the multi-threading feature I checked on my PC and I never reach 100% CPU when calculating a single hash so I think leaving this feature out is better.
msg262747 - (view) Author: SilentGhost (SilentGhost) * Date: 2016-04-01 16:41
Left comments on Rietveld.
msg262749 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2016-04-01 17:12
> The blocksize should be fixed and large (perhaps 256kB).

I used strace to check md5sum & sha1sum: they use read() syscalls of 32,768 bytes.

stat().st_blksize is 4,096 bytes.

I'm not sure that it matters so much to use large read. But I don't really care, I'm also ok to use something large like 256 kB.

Note: The cp command uses read() syscalls of 131,072 bytes.
msg262768 - (view) Author: Aviv Palivoda (palaviv) * Date: 2016-04-01 20:43
Publishing another patch after SilentGhost and Victor CR. I also changed the block size to 256 KB. If someone can remove the dependency on issue 14156 (I don't think I have permissions).
msg262780 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-04-02 00:32
I left some replies to Rietveld comments (sending review emails seems buggy).

For a chunk size, don’t worry too much about it. I would say keep it large enough to limit time spent executing Python code and syscalls, keep it small to avoid wasting high speed cache memory, and keep it a power of two to work with OS and filesystem buffers.
msg262792 - (view) Author: Aviv Palivoda (palaviv) * Date: 2016-04-02 08:39
Adding new patch after CR changes.
msg273053 - (view) Author: Aviv Palivoda (palaviv) * Date: 2016-08-18 18:23
Hi, is there anything more I need to do on this patch? If not do you think this can be added in 3.6?
msg273055 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2016-08-18 18:36
There are so many existing tools that already do this I don't really see why Python needs to become yet another one.  What is the value in doing this?

Just use the openssl command.  "openssl sha256 myfile"  Or any of the md5sum, sha1sum and other plethora of commands people also have installed.

Overall the change looks pretty good (i left a couple comments on the patch), i'm not going to object to it going in.  But I don't know why we're bothering.
msg273056 - (view) Author: Aviv Palivoda (palaviv) * Date: 2016-08-18 18:54
The use case that made me think about this feature was when I was working on a Windows PC and needed to calculate an md5 of a file. I agree that in a unix environment there are existing tools but on windows you usually don't have them.
msg273059 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2016-08-18 19:10
> There are so many existing tools that already do this I don't really see
why Python needs to become yet another one.  What is the value in doing

Portability. You described UNIX. There is no such tool by default on
Windows for example (as Avid said).
msg273304 - (view) Author: Antonio Valentino (avalentino) Date: 2016-08-21 16:36
Hi all,
please note that to stay compatible with the GNU md5sum utility you should print the file name prefixed by a "*" if you read the file in binary mode.

Also when digests is checked, files to check should be opened in binary or text mode according the presence/absence of the "*" character before the file name.

A explicit error should be raised IMO if some specific mode is not supported (e.g. text mode) by the check function.

Also, since the tool supports different hashing algorithms the openssl/BSD-style output format could be more appropriate IMO:

MD5 (file01.dat) = 101b455ce70d2e73e1a4d92a3e8c29e1

FYI I wrote a the hashsum package [1] that provides a command line tool that is intended to be a "Python drop-in replacement for md5sum and co.".

If my understanding is correct you want to keep this patch as simple as possible but, if you are interested in, I could provide patches to:

* fix the GNU style output: binary ("*") vs text mode
* implement full support for the text mode
* implement full support for BSD-style output format

I could also convert the utility [1] to be integrated directly in hashlib.

msg273327 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-08-22 02:11
Guido, would you can to opine on this?  Every now and then we get a request to make command-line utilities out of tools in the standard library.  Whether we should or not depends on whether the standard library intends to be primarily a library for Python code or whether it is also about providing general purpose toolkits that might be helpful in a non-unix environment.

We've has some of these that have met with success (for example, timeit, json.tool, and SimpleHTTPServer) and others that were just a waste or a were a pale shadow of their full featured Unix counterparts (or left to rot in the Tools directory).  

If we go further down this road, it would be nice for you to lay out the ground roads for what kind of command line tools would be acceptable, how stable their API would be, and whether they should be separated from the module itself.  Do you even want to be in the business of offering command-line APIs that duplicate commonly available Unix tools?
msg273329 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-08-22 02:18
I prefer not to go down this road. The modules that do this where I use it
are typically Python specific, e.g. pdb or timeit. In the past we sometimes
had little main() functions in modules for testing but I think we have
better ways to test modules these days.

--Guido (mobile)
Date User Action Args
2016-08-25 07:44:07rhettingersetstatus: open -> closed
resolution: rejected
2016-08-22 02:18:31gvanrossumsetmessages: + msg273329
2016-08-22 02:11:59rhettingersetassignee: gvanrossum

messages: + msg273327
nosy: + gvanrossum
2016-08-22 01:58:27rhettingersetassignee: rhettinger -> (no value)
2016-08-21 16:36:06avalentinosetnosy: + avalentino
messages: + msg273304
2016-08-18 19:10:29hayposetmessages: + msg273059
2016-08-18 18:54:04palavivsetmessages: + msg273056
2016-08-18 18:36:24gregory.p.smithsetmessages: + msg273055
2016-08-18 18:23:45palavivsetmessages: + msg273053
2016-04-02 08:39:24palavivsetfiles: + hashlib-script-mod-md5sum-style-5.patch

messages: + msg262792
2016-04-02 00:32:18martin.pantersetdependencies: - argparse.FileType for '-' doesn't work for a mode of 'rb'
messages: + msg262780
2016-04-01 20:43:23palavivsetfiles: + hashlib-script-mod-md5sum-style-4.patch

messages: + msg262768
2016-04-01 17:12:30hayposetmessages: + msg262749
2016-04-01 16:41:19SilentGhostsetmessages: + msg262747
2016-04-01 08:52:44palavivsetfiles: + hashlib-script-mod-md5sum-style-3.patch

messages: + msg262727
2016-03-31 20:46:15palavivsetfiles: + hashlib-script-mod-md5sum-style-2.patch
2016-03-31 20:43:14palavivsetmessages: + msg262706
2016-03-31 20:10:57christian.heimessetmessages: + msg262705
2016-03-31 19:34:50SilentGhostsetmessages: + msg262703
2016-03-31 18:36:50palavivsetfiles: + hashlib-script-mod-md5sum-style.patch

messages: + msg262699
2016-03-31 04:29:57rhettingersetmessages: + msg262687
2016-03-30 13:56:19christian.heimessetmessages: + msg262666
2016-03-30 13:03:16palavivsetfiles: + hashlib-script-mod-2.patch

messages: + msg262663
2016-03-29 21:31:55hayposetmessages: + msg262628
2016-03-29 21:30:47hayposetnosy: + haypo
messages: + msg262627
2016-03-29 15:36:11rhettingersetmessages: + msg262615
2016-03-29 15:36:10SilentGhostsetmessages: + msg262614
2016-03-29 15:29:08rhettingersetmessages: + msg262613
2016-03-29 15:27:22rhettingersetassignee: rhettinger

nosy: + rhettinger
2016-03-28 09:06:48SilentGhostsetnosy: + SilentGhost
dependencies: + argparse.FileType for '-' doesn't work for a mode of 'rb'
2016-03-28 08:24:24palavivsetmessages: + msg262565
2016-03-28 01:24:26martin.pantersetnosy: + martin.panter
messages: + msg262544
2016-03-11 21:51:01terry.reedysetnosy: + terry.reedy

messages: + msg261611
stage: patch review
2016-03-05 21:54:26palavivcreate