Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const forge = require('node-forge');
function readFile(file) {
return fs.readFileSync(path.resolve(__dirname, file), 'utf8');
}
let secretKey;
if (process.env.NODE_ENV === 'production') {
secretKey = {
public: readFile('./secret_key.pub'),
private: readFile('./secret_key')
};
} else {
// Generate a random keypair for dev/testing.
// See https://gist.github.com/sebadoom/2b70969e70db5da9a203bebd9cff099f
const keypair = forge.rsa.generateKeyPair({ bits: 2048 });
secretKey = {
public: forge.pki.publicKeyToPem(keypair.publicKey, 72),
private: forge.pki.privateKeyToPem(keypair.privateKey, 72)
};
}
module.exports = secretKey;
"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var helpers = require('./helpers');
var forge = require('node-forge');
var pki = forge.pki,
rsa = forge.rsa;
var _require = require('./constants'),
DEFAULT_MESSAGE_DIGEST = _require.DEFAULT_MESSAGE_DIGEST,
AES_STANDARD = _require.AES_STANDARD;
var Crypt =
/*#__PURE__*/
function () {
function Crypt() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, Crypt);
this.options = Object.assign({}, {
md: DEFAULT_MESSAGE_DIGEST,
entropy: undefined
PrivateKey.deserialize = function(dto) {
var n = new Hex.deserialize(dto.n).as(BigIntForge).value;
var e = new Hex.deserialize(dto.e).as(BigIntForge).value;
var d = new Hex.deserialize(dto.d).as(BigIntForge).value;
var p = new Hex.deserialize(dto.p).as(BigIntForge).value;
var q = new Hex.deserialize(dto.q).as(BigIntForge).value;
var dP = new Hex.deserialize(dto.dP).as(BigIntForge).value;
var dQ = new Hex.deserialize(dto.dQ).as(BigIntForge).value;
var qInv = new Hex.deserialize(dto.qInv).as(BigIntForge).value;
return new PrivateKey(forge.rsa.setPrivateKey(n, e, d, p, q, dP, dQ, qInv));
}
export function decryptRSAWithJWK(privateJWK, encryptedBlob) {
const n = _b64UrlToBigInt(privateJWK.n);
const e = _b64UrlToBigInt(privateJWK.e);
const d = _b64UrlToBigInt(privateJWK.d);
const p = _b64UrlToBigInt(privateJWK.p);
const q = _b64UrlToBigInt(privateJWK.q);
const dP = _b64UrlToBigInt(privateJWK.dp);
const dQ = _b64UrlToBigInt(privateJWK.dq);
const qInv = _b64UrlToBigInt(privateJWK.qi);
const privateKey = forge.rsa.setPrivateKey(n, e, d, p, q, dP, dQ, qInv);
const bytes = forge.util.hexToBytes(encryptedBlob);
const decrypted = privateKey.decrypt(bytes, 'RSA-OAEP', {
md: forge.md.sha256.create(),
});
return decodeURIComponent(decrypted);
}
module.exports.generateDkim = async () => {
let keypair = await util.promisify(forge.rsa.generateKeyPair)({ bits: 2048, workers: -1 });
let privateKeyPem = forge.pki.privateKeyToPem(keypair.privateKey);
let publicKeyPem = forge.pki.publicKeyToPem(keypair.publicKey);
let fp = fingerprint(privateKeyPem, 'sha256', true);
let selector = [
(config.title || '').replace(/[^a-z0-9]/g, ''),
new Date()
.toString()
.substr(4, 3)
.toLowerCase() +
new Date()
.getFullYear()
.toString()
.substr(-2),
crypto.randomBytes(2).toString('hex')
]
PublicKey.deserialize = function(dto) {
var n = new Hex.deserialize(dto.n).as(BigIntForge).value;
var e = new Hex.deserialize(dto.e).as(BigIntForge).value;
var internalKey = forge.rsa.setPublicKey(n, e);
return new PublicKey(internalKey);
}
function generateKeyPair(options, callback) {
if (callback) {
forge.rsa.generateKeyPair(options, function(err, keypair) {
if (err) {
callback(err, keypair);
} else {
callback(err, {
publicKey: new PublicKey(keypair.publicKey),
privateKey: new PrivateKey(keypair.privateKey)
});
}
});
} else {
var keypair = forge.rsa.generateKeyPair(options);
return {
publicKey: new PublicKey(keypair.publicKey),
privateKey: new PrivateKey(keypair.privateKey)
};
}
PublicKey.deserialize = function(dto) {
var n = new Hex.deserialize(dto.n).as(BigIntForge).value;
var e = new Hex.deserialize(dto.e).as(BigIntForge).value;
var internalKey = forge.rsa.setPublicKey(n, e);
return new PublicKey(internalKey);
}
PrivateKey.deserialize = function(dto) {
var n = new Hex.deserialize(dto.n).as(BigIntForge).value;
var e = new Hex.deserialize(dto.e).as(BigIntForge).value;
var d = new Hex.deserialize(dto.d).as(BigIntForge).value;
var p = new Hex.deserialize(dto.p).as(BigIntForge).value;
var q = new Hex.deserialize(dto.q).as(BigIntForge).value;
var dP = new Hex.deserialize(dto.dP).as(BigIntForge).value;
var dQ = new Hex.deserialize(dto.dQ).as(BigIntForge).value;
var qInv = new Hex.deserialize(dto.qInv).as(BigIntForge).value;
return new PrivateKey(forge.rsa.setPrivateKey(n, e, d, p, q, dP, dQ, qInv));
}