如何使用Node.js加密创建固定位数的加密密钥

时间:2019-09-10 06:07:23

标签: javascript node.js

对此我是陌生的,但我的要求是我想使用Node.js加密模块来使用20个字符长的加密密钥。

我尝试过但无法获得固定长度的密钥。

任何人都可以帮助我。

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用crypto.randomBytes生成密钥,例如:

  componentDidMount() {
    fetch("https://api.myjson.com/bins/1ep1fh")
      .then(res => res.json())
      .then(result => {
        let texts = result
          .map((el, i) => {
            return el.text;
          })
          .join("");
        let highlights = result
          .map((el, i) => {
            return el.highlight;
          })
          .join("").split(",");
        console.log(highlights);
        this.setState({
          text: texts,
          highlight: highlights
        });
      });
  }

您还可以使用crypto.scrypt函数从较长的密码短语中得出固定长度的密钥:

const crypto = require('crypto');

const key = crypto.randomBytes(10).toString('hex');
console.log(`key: ${key} length: ${key.length}`);