Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在节点中使用加密?_Node.js - Fatal编程技术网

Node.js 如何在节点中使用加密?

Node.js 如何在节点中使用加密?,node.js,Node.js,我退出新节点。 我读过一篇文章,建议使用加密对密码进行哈希和盐析,使其不可见。但实际上,我有点困惑如何使用它!!??输入密码和存储的哈希密码之间的比较函数是什么?!! 你能帮我使用Crpyto吗 app.post("/signup", async (req, res, next) => { const { username, password} = req.body; if (!username && !password ) { res.jso

我退出新节点。 我读过一篇文章,建议使用加密对密码进行哈希和盐析,使其不可见。但实际上,我有点困惑如何使用它!!??输入密码和存储的哈希密码之间的比较函数是什么?!! 你能帮我使用Crpyto吗


app.post("/signup", async (req, res, next) => {
    const { username, password} = req.body;
    if (!username && !password ) {
      res.json({
        success: false,
        message: "Complete your Sign up "
      });
    }
    const query = `INSERT INTO Users (username ,password) VALUES ('${username}','${password}') `;
    const student = await db.run(query);
    .....

我发现阅读有关加密的节点文档非常有用:

以下是我在文件中使用它的方式:

const hash = crypto.createHash("md5");

function createHash(filePath) {
    const hash = crypto.createHash("md5");

    const readStream = fs.createReadStream(filePath);
    readStream.on('data', (data) => {
        hash.update(data);
    });

    return new Promise((resolve, reject) => {
        readStream.on('end', () => {
            resolve(hash.digest('hex'));
        })
    });
}
对于散列密码,这是可以的:

const hash = crypto.createHash("md5");
function hashPassword(password) {
    //replace md5 with sha256 or another algorithm
    const hash = crypto.createHash("md5");
    hash.update(password);
    return hash.digest('hex');
}
但不要使用md5对密码进行散列,请使用sha256

然后将此哈希放入数据库,从数据库中获取,再次对给定密码进行哈希,并将其与数据库中的哈希进行比较

如果您想要更好的安全性,您可以将生成的一些常量字符串连接到密码。例如:

const fixedString = "dfgd5fgd6g5df6g5!u(è§è'"; // make this as complicated as possible.

function hashPassword(password) {


    //concatenate this to the password
    password += fixedString;

    const hash = crypto.createHash("sha256");
    hash.update(password);
    return hash.digest('hex');
}

我希望这有帮助,祝你好运

我发现阅读有关加密的节点文档非常有用:

以下是我在文件中使用它的方式:

const hash = crypto.createHash("md5");

function createHash(filePath) {
    const hash = crypto.createHash("md5");

    const readStream = fs.createReadStream(filePath);
    readStream.on('data', (data) => {
        hash.update(data);
    });

    return new Promise((resolve, reject) => {
        readStream.on('end', () => {
            resolve(hash.digest('hex'));
        })
    });
}
对于散列密码,这是可以的:

const hash = crypto.createHash("md5");
function hashPassword(password) {
    //replace md5 with sha256 or another algorithm
    const hash = crypto.createHash("md5");
    hash.update(password);
    return hash.digest('hex');
}
但不要使用md5对密码进行散列,请使用sha256

然后将此哈希放入数据库,从数据库中获取,再次对给定密码进行哈希,并将其与数据库中的哈希进行比较

如果您想要更好的安全性,您可以将生成的一些常量字符串连接到密码。例如:

const fixedString = "dfgd5fgd6g5df6g5!u(è§è'"; // make this as complicated as possible.

function hashPassword(password) {


    //concatenate this to the password
    password += fixedString;

    const hash = crypto.createHash("sha256");
    hash.update(password);
    return hash.digest('hex');
}

我希望这有帮助,祝你好运

我会使用类似于
bcryptjs
argon2
的东西,其中包括用于散列和比较的库函数,并帮助避免编写自己的代码时出现问题(这很容易出错或不太正确)我会使用类似于
bcryptjs
argon2
的东西,其中包括用于散列和比较的库函数,并帮助避免编写自己的代码时出现问题(这很容易出错或不太正确)