小编ssh*_*h98的帖子

Bcrypt Hash 返回 TypeError(“Unicode 对象必须在散列之前编码”)和无效盐

我已经查看了与此相关的所有 StackOverflow 问题,但我似乎无法弄清楚这一点。当我对密码进行散列并对其自身进行检查时,它会使用当前代码返回 TypeError“Unicode 对象必须在散列之前进行编码”:

from scripts import tabledef
from flask import session
from sqlalchemy.orm import sessionmaker
from contextlib import contextmanager
import bcrypt

(Unrelated Python code...)

def hash_password(password):
     return bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())


def credentials_valid(username, password):
    with session_scope() as s:
        user = s.query(tabledef.User).filter(
            tabledef.User.username.in_([username])).first()
        if user:

            return bcrypt.checkpw(password.encode('utf8'), user.password)
        else:
            return False
Run Code Online (Sandbox Code Playgroud)

当我尝试通过设置修复此错误时user.password= user.password.encode('utf8'),我收到“Invalid Salt”。

这段代码有什么问题?

更新:我通过用户的 Flask 输入存储密码:

import json
import sys
import os
import plotly
import pandas as pd
import numpy as np
import plotly.graph_objs as go


from …
Run Code Online (Sandbox Code Playgroud)

python salt bcrypt flask

4
推荐指数
1
解决办法
1773
查看次数

模拟 Slack Web Api 笑话

我有两个文件,其中一个是我初始化 slack Web API 客户端、发布一条消息,并根据模拟值对其进行测试:

main.ts:

import { WebClient } from '@slack/web-api';
const slack = new WebClient(process.env.SLACK_API_KEY as string);

slack.chat.postMessage({...});
Run Code Online (Sandbox Code Playgroud)

test.ts

import { WebClient } from '@slack/web-api';
let slack: WebClient;

beforeAll(async () => {
  slack = new WebClient();
});

jest.mock('@slack/web-api', () => {
  return {
    chat: jest.fn(),
    postMessage: jest.fn(),
  };
});

describe('test', () => {
  it("tests slack message", async () => {
    expect(slack.chat.postMessage).toBeCalledWith({...});
  })
})
Run Code Online (Sandbox Code Playgroud)

问题是当我运行此代码时出现以下错误:TypeError: web_api_1.WebClient is not a constructor

我尝试了各种不同的方法,包括模拟模块、使用esModule: true而不是定义 WebClient。我的问题是我做错了什么?

node.js typescript jestjs

4
推荐指数
1
解决办法
3675
查看次数

标签 统计

bcrypt ×1

flask ×1

jestjs ×1

node.js ×1

python ×1

salt ×1

typescript ×1