Hashing file in Python 3?

前端 未结 3 1103
慢半拍i
慢半拍i 2021-01-18 14:08

In Python 2, one could hash a string by just running:

someText = \"a\"
hashlib.sha256(someText).hexdigest()

But in Python 3, it needs to be

相关标签:
3条回答
  • 2021-01-18 14:16

    On Unix systems, in Python 2 there was no distinction between binary- and text-mode files, so it didn't matter how you opened them.

    But in Python 3 it matters on every platform. sha256() requires binary input, but you opened the file in text mode. That's why @BrenBam suggested you open the file in binary mode.

    Since you opened the file in text mode, Python 3 believes it needs to decode the bits in the file to turn the bytes into Unicode strings. But you don't want decoding at all, right?

    Then open the file in binary mode, and you'll read byte strings instead, which is what sha256() wants.

    By the way, your:

    someText = "a".encode("ascii")
    hashlib.sha256(someText).hexdigest()
    

    can be done more easily in a related way:

    hashlib.sha256(b"a").hexdigest()
    

    That is, pass it the binary data directly, instead of bothering with encoding a Unicode string (which the literal "a" is).

    0 讨论(0)
  • 2021-01-18 14:17

    I have programmed a module which is able to hash big files with different algorithms.

    pip3 install py_essentials
    

    Use the module like this:

    from py_essentials import hashing as hs
    hash = hs.fileChecksum("path/to/the/file.txt", "sha256")
    

    Take a look at the documentation.

    0 讨论(0)
  • 2021-01-18 14:32

    Try opening the file in binary mode with open(fin, "rb").

    0 讨论(0)
提交回复
热议问题