@unvision/jose
Version:
Implementation of the RFCs of the JOSE Working Group.
82 lines • 3.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PS512 = exports.PS384 = exports.PS256 = exports.RS512 = exports.RS384 = exports.RS256 = void 0;
const crypto_1 = require("crypto");
const util_1 = require("util");
const invalid_jsonwebkey_exception_1 = require("../../exceptions/invalid-jsonwebkey.exception");
const invalid_jsonwebsignature_exception_1 = require("../../exceptions/invalid-jsonwebsignature.exception");
const jsonwebsignature_algorithm_1 = require("./jsonwebsignature.algorithm");
const signAsync = (0, util_1.promisify)(crypto_1.sign);
const verifyAsync = (0, util_1.promisify)(crypto_1.verify);
/**
* Implementation of the JSON Web Signature RSASSA Algorithm.
*/
class RsaSsaAlgorithm extends jsonwebsignature_algorithm_1.JsonWebSignatureAlgorithm {
/**
* Instantiates a new JSON Web Signature RSASSA Algorithm to Sign and Verify Messages.
*
* @param algorithm Name of the JSON Web Signature Algorithm.
* @param hash Hash Algorithm used to Sign and Verify Messages.
* @param padding RSA Padding used by the JSON Web Signature RSASSA Algorithm to Sign and Verify Messages.
*/
constructor(algorithm, hash, padding) {
super(algorithm, hash, 'RSA');
this.padding = padding;
}
/**
* Signs a Message with the provided JSON Web Key.
*
* @param message Message to be Signed.
* @param key JSON Web Key used to Sign the provided Message.
* @returns Resulting Signature of the provided Message.
*/
async sign(message, key) {
this.validateJsonWebKey(key);
const cryptoKey = key['cryptoKey'];
if (cryptoKey.type !== 'private') {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('A Private Key is needed to Sign a JSON Web Signature Message.');
}
const signature = await signAsync(this.hash, message, { key: cryptoKey, padding: this.padding });
return signature;
}
/**
* Checks if the provided Signature matches the provided Message based on the provide JSON Web Key.
*
* @param signature Signature to be matched against the provided Message.
* @param message Message to be matched against the provided Signature.
* @param key JSON Web Key used to verify the Signature and Message.
*/
async verify(signature, message, key) {
this.validateJsonWebKey(key);
const cryptoKey = key['cryptoKey'];
const result = await verifyAsync(this.hash, message, { key: cryptoKey, padding: this.padding }, signature);
if (!result) {
throw new invalid_jsonwebsignature_exception_1.InvalidJsonWebSignatureException();
}
}
}
/**
* RSASSA-PKCS1-v1_5 using SHA-256.
*/
exports.RS256 = new RsaSsaAlgorithm('RS256', 'SHA256', crypto_1.constants.RSA_PKCS1_PADDING);
/**
* RSASSA-PKCS1-v1_5 using SHA-384.
*/
exports.RS384 = new RsaSsaAlgorithm('RS384', 'SHA384', crypto_1.constants.RSA_PKCS1_PADDING);
/**
* RSASSA-PKCS1-v1_5 using SHA-512.
*/
exports.RS512 = new RsaSsaAlgorithm('RS512', 'SHA512', crypto_1.constants.RSA_PKCS1_PADDING);
/**
* RSASSA-PSS using SHA-256 and MGF1 with SHA-256.
*/
exports.PS256 = new RsaSsaAlgorithm('PS256', 'SHA256', crypto_1.constants.RSA_PKCS1_PSS_PADDING);
/**
* RSASSA-PSS using SHA-384 and MGF1 with SHA-384.
*/
exports.PS384 = new RsaSsaAlgorithm('PS384', 'SHA384', crypto_1.constants.RSA_PKCS1_PSS_PADDING);
/**
* RSASSA-PSS using SHA-512 and MGF1 with SHA-512.
*/
exports.PS512 = new RsaSsaAlgorithm('PS512', 'SHA512', crypto_1.constants.RSA_PKCS1_PSS_PADDING);
//# sourceMappingURL=rsassa.algorithm.js.map