node.js 中的承诺加密
Promisified crypto in node.js
当我们查看 https://nodejs.org/api/crypto.html#cryptogeneratekeypairtype-options-callback 的 crypto 文档时,我们看到了这句话。
If this method is invoked as its util.promisify()ed version, it returns a Promise for an Object with publicKey and privateKey properties.
那么,promisified怎么用呢?我们尝试了十多种方法,其中 none 行得通。
你可以这样使用它。它解析为包含 publicKey 和 privateKey
的对象
const util = require('util');
const crypto = require('crypto');
const gen = util.promisify(crypto.generateKeyPair);
(async () => {
const res = await gen('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'top secret'
}
});
console.log(res);
})();
当我们查看 https://nodejs.org/api/crypto.html#cryptogeneratekeypairtype-options-callback 的 crypto 文档时,我们看到了这句话。
If this method is invoked as its util.promisify()ed version, it returns a Promise for an Object with publicKey and privateKey properties.
那么,promisified怎么用呢?我们尝试了十多种方法,其中 none 行得通。
你可以这样使用它。它解析为包含 publicKey 和 privateKey
的对象const util = require('util');
const crypto = require('crypto');
const gen = util.promisify(crypto.generateKeyPair);
(async () => {
const res = await gen('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'top secret'
}
});
console.log(res);
})();