简体   繁体   English

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

[英]How to create fixed digit crypto key using Node.js Crypto

I am new to this, but my requirement is that I want 20 characters long crypto key using Node.js Crypto module. 我对此并不陌生,但是我的要求是我想使用Node.js Crypto模块使用20个字符长的加密密钥。

I tried but could not get fixed length key. 我尝试过,但是无法获得固定长度的钥匙。

Can anyone please help me. 谁能帮帮我吗。

Thanks in advance. 提前致谢。

You can use crypto.randomBytes to generate keys, for example: 您可以使用crypto.randomBytes生成密钥,例如:

const crypto = require('crypto');

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

You can also use the crypto.scrypt function to derive a fixed-length key from a longer passphrase: 您还可以使用crypto.scrypt函数从较长的密码短语中得出固定长度的密钥:

const crypto = require('crypto');

const key = crypto.scryptSync('my secret pass phrase', 'my salt', 10).toString('hex');
console.log(`key: ${key} length: ${key.length}`);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM