I'm not entirely sure which lib/what is wrong but have been having some issues with encoding a password I keep getting TypeError: Unicode-objects must be encoded before hashing
I am passing b'Password1' (but using'Password1'.encode()or'Password1'.encode('utf-8')does not change the result.print(password)` reports that the string is bytes.
I dug into the backtrace and printed out the password and salt from the bycrypt hashpw function. That password string is 'test'. Following this back this is defined in passlib/handlers/ bycrypt.py:L374
Edit 1: I should mention that I am trying to call current_app.user_manager.password_manager.hash_password(password)
(i've made the paths relative to the venv in the project dir)
b'test'
$2$04$5BJqKfqMQvV7nS.yUguNcu
Traceback (most recent call last):
File "app_env/bin/flask", line 11, in <module>
sys.exit(main())
File "app_env/lib/python3.7/site-packages/flask/cli.py", line 894, in main
cli.main(args=args, prog_name=name)
File "app_env/lib/python3.7/site-packages/flask/cli.py", line 557, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "app_env/lib/python3.7/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "app_env/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "app_env/lib/python3.7/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "app_env/lib/python3.7/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "app_env/lib/python3.7/site-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "app_env/lib/python3.7/site-packages/flask/cli.py", line 412, in decorator
return __ctx.invoke(f, *args, **kwargs)
File "app_env/lib/python3.7/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "presale/__init__.py", line 73, in seed_data
seed_db()
File "presale/commands/init_db.py", line 19, in seed_db
create_users()
File "presale/commands/init_db.py", line 30, in create_users
user = find_or_create_user(u'Admin', u'Example', u'admin@example.com', b'Password1', admin_role)
File "presale/commands/init_db.py", line 82, in find_or_create_user
print(current_app.user_manager.password_manager.hash_password(password))
File "app_env/lib/python3.7/site-packages/flask_user/password_manager.py", line 50, in hash_password
password_hash = self.password_crypt_context.encrypt(password)
File "app_env/lib/python3.7/site-packages/passlib/utils/decor.py", line 191, in wrapper
return func(*args, **kwds)
File "app_env/lib/python3.7/site-packages/passlib/context.py", line 2265, in encrypt
return self.hash(*args, **kwds)
File "app_env/lib/python3.7/site-packages/passlib/context.py", line 2253, in hash
return record.hash(secret, **kwds)
File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 748, in hash
self.checksum = self._calc_checksum(secret)
File "app_env/lib/python3.7/site-packages/passlib/handlers/bcrypt.py", line 530, in _calc_checksum
self._stub_requires_backend()
File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 2221, in _stub_requires_backend
cls.set_backend()
File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 2123, in set_backend
return owner.set_backend(name, dryrun=dryrun)
File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 2130, in set_backend
return cls.set_backend(name, dryrun=dryrun)
File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 2155, in set_backend
cls._set_backend(name, dryrun)
File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 2278, in _set_backend
super(SubclassBackendMixin, cls)._set_backend(name, dryrun)
File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 2191, in _set_backend
ok = loader(**kwds)
File "app_env/lib/python3.7/site-packages/passlib/handlers/bcrypt.py", line 671, in _load_backend_mixin
return mixin_cls._finalize_backend_mixin(name, dryrun)
File "app_env/lib/python3.7/site-packages/passlib/handlers/bcrypt.py", line 374, in _finalize_backend_mixin
result = safe_verify("test", test_hash_20)
File "app_env/lib/python3.7/site-packages/passlib/handlers/bcrypt.py", line 293, in safe_verify
return verify(secret, hash)
File "app_env/lib/python3.7/site-packages/passlib/utils/handlers.py", line 761, in verify
return consteq(self._calc_checksum(secret), chk)
File "app_env/lib/python3.7/site-packages/passlib/handlers/bcrypt.py", line 688, in _calc_checksum_raw
hash = _pybcrypt.hashpw(secret, config)
File "app_env/lib/python3.7/site-packages/bcrypt/__init__.py", line 63, in hashpw
raise TypeError("Unicode-objects must be encoded before hashing")
TypeError: Unicode-objects must be encoded before hashing
Hello,
I'm not entirely sure which lib/what is wrong but have been having some issues with encoding a password I keep getting
TypeError: Unicode-objects must be encoded before hashing
I am passing
b'Password1' (but using
'Password1'.encode()or
'Password1'.encode('utf-8')does not change the result.
print(password)` reports that the string is bytes.I dug into the backtrace and printed out the password and salt from the bycrypt
hashpw
function. That password string is'test'
. Following this back this is defined inpasslib/handlers/ bycrypt.py:L374
Edit 1: I should mention that I am trying to call
current_app.user_manager.password_manager.hash_password(password)
Any idea what's going on here?
Python
(i've made the paths relative to the venv in the project dir)