@alessiofrittoli/crypto-signature
Version:
Lightweight TypeScript Signatures library
2 lines (1 loc) • 3.84 kB
JavaScript
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _chunkOQQ2JKPRjs = require('./chunk-OQQ2JKPR.js');var _crypto = require('crypto'); var _crypto2 = _interopRequireDefault(_crypto);var _Hmac = require('@alessiofrittoli/crypto-key/Hmac');var _cryptoalgorithm = require('@alessiofrittoli/crypto-algorithm');var _exception = require('@alessiofrittoli/exception');var _coercion = require('@alessiofrittoli/crypto-buffer/coercion');var c=class c{static sign(a,f,s=c.Algorithm){if(!a)throw new (0, _exception.Exception)("No data to sign has been provided.",{code:_chunkOQQ2JKPRjs.a.EMPTY_VALUE});if(!f)throw new (0, _exception.Exception)("No Private Key has been provided.",{code:_chunkOQQ2JKPRjs.a.NO_PRIVATEKEY});let d=c.jwkAlgToHash(s),A=_coercion.coerceToUint8Array.call(void 0, a);if(!d)throw new (0, _exception.Exception)("Invalid JWK Algorithm name.",{code:_chunkOQQ2JKPRjs.a.INVALID_JWKNAME});try{if(s.startsWith("HS")){let e=f;return e=e instanceof CryptoKey?_crypto2.default.KeyObject.from(e):e,_Hmac.Hmac.digest(A,e,d)}if(s==="EdDSA"){let e=f;return e=e instanceof CryptoKey?_crypto2.default.KeyObject.from(e):e,_crypto2.default.sign(null,A,e)}let i=f;i=i instanceof CryptoKey?_crypto2.default.KeyObject.from(i):i;let S=_crypto2.default.createSign(d);return S.write(A),S.end(),S.sign(i)}catch(i){throw new (0, _exception.Exception)("An error occured while creating the signature.",{code:_chunkOQQ2JKPRjs.a.UNKNOWN,cause:i})}}static isValid(a,f,s,d=c.Algorithm){if(!a)throw new (0, _exception.Exception)("No signature provided.",{code:_chunkOQQ2JKPRjs.a.NO_SIGN});if(!f)throw new (0, _exception.Exception)("The signed data is needed for integrity controls.",{code:_chunkOQQ2JKPRjs.a.EMPTY_VALUE});if(!s)throw new (0, _exception.Exception)("No Public Key has been provided.",{code:_chunkOQQ2JKPRjs.a.NO_PUBLICKEY});let A=c.jwkAlgToHash(d);if(!A)throw new (0, _exception.Exception)("Invalid JWK Algorithm name.",{code:_chunkOQQ2JKPRjs.a.INVALID_JWKNAME});let i=_coercion.coerceToUint8Array.call(void 0, a),S=_coercion.coerceToUint8Array.call(void 0, f);try{if(d.startsWith("HS")){let o=s;if(o=o instanceof CryptoKey?_crypto2.default.KeyObject.from(o):o,!_Hmac.Hmac.isValid(Buffer.from(i),S,o,A))throw new (0, _exception.Exception)("Invalid signature.",{code:_chunkOQQ2JKPRjs.a.INVALID_SIGN});return!0}if(d==="EdDSA"){let o=s;if(o=o instanceof CryptoKey?_crypto2.default.KeyObject.from(o):o,!_crypto2.default.verify(null,S,o,i))throw new (0, _exception.Exception)("Invalid signature.",{code:_chunkOQQ2JKPRjs.a.INVALID_SIGN});return!0}let e=s;e=e instanceof CryptoKey?_crypto2.default.KeyObject.from(e):e;let l=_crypto2.default.createVerify(A);if(l.write(S),l.end(),!l.verify(e,i))throw new (0, _exception.Exception)("Invalid signature.",{code:_chunkOQQ2JKPRjs.a.INVALID_SIGN});return!0}catch(e){throw _exception.Exception.isException(e)?e:new (0, _exception.Exception)("An error occured while verifying the signature.",{code:_chunkOQQ2JKPRjs.a.UNKNOWN,cause:e})}}static jwkAlgToHash(a){return _optionalChain([_cryptoalgorithm.Algorithm, 'access', _ => _.by, 'call', _2 => _2({jwkAlg:a}), 'optionalAccess', _3 => _3.hash])}};c.Algorithm="HS256",c.HashDigest="SHA-256";var h=c;exports.Signature = h;