title: Python 3.7 : Hashing a binary file
body: <p>I am trying to generate a hash for a given file, in this case the hash function got to a binary file (.tgz file) and then generated an error.  Is there a way I can read a binary file and generate a md5 hash of it?</p>
  
  <p>The Error I am receiving is:</p>
  
  <blockquote>
  <pre><code>buffer = buffer.decode('UTF-8') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbc in position 10: invalid start byte
  </code></pre>
  </blockquote>
  
  <p>The source code is:</p>
  
  <pre><code>import hashlib
  
  def HashFile(filename, readBlockSize = 4096):
      hash = hashlib.md5()
  
      with open(filename, 'rb') as fileHandle:
  
          while True:
              buffer = fileHandle.read(readBlockSize)
  
              if not buffer:
                  break
  
              buffer = buffer.decode('UTF-8')                
              hash.update(hashlib.md5(buffer).hexdigest())
  
      return
  </code></pre>
  
  <p>I am using Python 3.7 on Linux.</p>