hash.py 844 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """
  2. [hash.py]
  3. Provides the `hash` function, with various tools for hashing.
  4. """
  5. import hashlib
  6. def md5(string):
  7. m = hashlib.md5()
  8. m.update(string)
  9. return m.digest()
  10. def sha256(string):
  11. s = hashlib.sha256()
  12. s.update(string)
  13. return s.digest()
  14. def hash(argc):
  15. """hash: various hashing functions.
  16. Usage:
  17. hash (md5|sha256) FILE
  18. hash (md5|sha256) --string STRING
  19. """
  20. if argc.args['FILE']:
  21. if argc.args['md5']:
  22. return md5(open(argc.args['FILE']).read())
  23. elif argc.args['sha256']:
  24. return sha256(open(argc.args['FILE']).read())
  25. elif argc.args['--string']:
  26. if argc.args['md5']:
  27. return md5(argc.args['STRING'])
  28. elif argc.args['sha256']:
  29. return sha256(argc.args['STRING'])
  30. exports = {"hash": hash}