Bcrypt ValueError:使用自定义盐时盐无效

发布于 2025-01-16 15:35:34 字数 869 浏览 2 评论 0原文

我正在开发一个需要散列的项目。问题是我想创建一个自定义哈希(使用添加非英语字符和符号的程序生成)。当使用简单的字符串进行测试时,我收到错误 ValueError: Invalid salt。我尝试过的代码:

def test():
    password = b"pass123"
    password2 = input("Enter password: ")
    bpassword2 = password2.encode('utf-8')

    salt = b"test"

    hashed = bcrypt.hashpw(password, salt)
    print(hashed)

    hash2 = bcrypt.hashpw(bpassword2, bcrypt.gensalt())
    print(hash2)


test()

这会返回错误。我也尝试过:

def test():
    password = b"pass123"
    password2 = input("Enter password: ")
    bpassword2 = password2.encode('utf-8')

    salt = "test"

    hashed = bcrypt.hashpw(password, salt)
    print(hashed)

    hash2 = bcrypt.hashpw(bpassword2, bcrypt.gensalt())
    print(hash2)


test()

但是这会返回错误TypeError:Unicode-objects必须在散列之前进行编码。老实说,我不知道该怎么做,如果这是一个非常简单的修复,那么我很抱歉。

I'm working on a project where hashing is required. The problem is that I want to create a custom hash (generated with a program that would add non-english characters and symbols). When testing this with a simple string I get the error ValueError: Invalid salt. The code I tried:

def test():
    password = b"pass123"
    password2 = input("Enter password: ")
    bpassword2 = password2.encode('utf-8')

    salt = b"test"

    hashed = bcrypt.hashpw(password, salt)
    print(hashed)

    hash2 = bcrypt.hashpw(bpassword2, bcrypt.gensalt())
    print(hash2)


test()

This returns the error. I've also tried:

def test():
    password = b"pass123"
    password2 = input("Enter password: ")
    bpassword2 = password2.encode('utf-8')

    salt = "test"

    hashed = bcrypt.hashpw(password, salt)
    print(hashed)

    hash2 = bcrypt.hashpw(bpassword2, bcrypt.gensalt())
    print(hash2)


test()

However this returns the error TypeError: Unicode-objects must be encoded before hashing. I honestly don't know what to do and if this is a really simple fix then I'm sorry ????.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苯莒 2025-01-23 15:35:34

问题是 bcrypt.hashsalt 期望盐的长度 > 16 个字符。这是该方法的源代码:

def hashpw(password: bytes, salt: bytes) -> bytes:
  """
  Hash the password with the given salt.
  """
  if len(salt) != 16:
    raise ValueError("Invalid salt")

  hashed = _bcrypt.ffi.new("char[]", 60)
  _bcrypt.lib.bcrypt_hashpw(password, salt, hashed)
  return _bcrypt.ffi.buffer(hashed, 60)[:]

The issue is that the bcrypt.hashsalt expects the salt to be of length > 16 characters. here is the source code of the method:

def hashpw(password: bytes, salt: bytes) -> bytes:
  """
  Hash the password with the given salt.
  """
  if len(salt) != 16:
    raise ValueError("Invalid salt")

  hashed = _bcrypt.ffi.new("char[]", 60)
  _bcrypt.lib.bcrypt_hashpw(password, salt, hashed)
  return _bcrypt.ffi.buffer(hashed, 60)[:]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文