title: Python bcrypt TypeError: Unicode-objects must be encoded before checking
body: <p>I am trying to make a secure login system using <code>bcrypt</code> (Python).</p>
  <p>Now the error that I am getting is <code>TypeError: Unicode-objects must be encoded before checking</code>.
  I know that <code>bcrypt</code>'s <code>checkpw()</code> only takes <code>bytes</code> as a datatype, but as you can see in the code it should already be encoded to that datatype.</p>
  <p>Here are the error messages:</p>
  <blockquote>
  <p>Traceback (most recent call last):
  File &quot;C:\Users\olive\OneDrive\Dokumenter\Files\skole docs\Programering\test\Fixornot.py&quot;, line 37, in 
  logreg()</p>
  <p>File &quot;C:\Users\olive\OneDrive\Dokumenter\Files\skole docs\Programering\test\Fixornot.py&quot;, line 29, in logreg
  login(logname, logpass)</p>
  <p>File &quot;C:\Users\olive\OneDrive\Dokumenter\Files\skole docs\Programering\test\Fixornot.py&quot;, line 20, in login
  bcrypt.checkpw(logpass, hashed)</p>
  <p>File &quot;C:\Users\olive\AppData\Local\Programs\Python\Python39\lib\site-packages\bcrypt_<em>init</em>_.py&quot;,
  line 120, in checkpw
  raise TypeError(&quot;Unicode-objects must be encoded before checking&quot;)</p>
  <p>TypeError: Unicode-objects must be encoded before checking</p>
  </blockquote>
  <p>Here is the code (<a href="https://github.com/It-Snorlaxy/HashLogSystem/blob/main/test/Fixornot.py" rel="nofollow noreferrer">github</a>):</p>
  <pre><code>import bcrypt
  import hashlib
  LR = None
  
  salt = bcrypt.gensalt()
  
  def register ():
      print('Making a user')
      user = input('Navn: ')
      hashed = bcrypt.hashpw(input('password: ').encode('utf-8'), salt)
      file = open('user_details.txt','a')
      file.write(user + ',' + str(hashed) + ',' + str(salt) + '\n')
      file.close()
      logreg()
  
  def login (logname, logpass):
      file = open('user_details.txt','r')
      for i in file:
          user, hashed, salt = i.split(',')
      bcrypt.checkpw(logpass, hashed)
      file.close()
  
  def logreg():
      loginregist = input('log/reg? ')
      if loginregist == 'log':
          print('Du er ved at logge ind')
          logname = input('Navn: ')
          logpass = bcrypt.hashpw(input('password: ').encode('utf-8'), salt)
          login(logname, logpass)
      elif loginregist == 'reg':
          register()
      else:
          logreg()
  
  
  
  logreg()
  </code></pre>