简体   繁体   中英

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.

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:

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:

const crypto = require('crypto');

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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