@launchdarkly/js-server-sdk-common-edge
Version:
LaunchDarkly Server SDK for JavaScript - common Edge SDK code
32 lines • 1.05 kB
JavaScript
import CryptoJS from 'crypto-js';
export default class CryptoJSHmac {
constructor(algorithm, key) {
let algo;
switch (algorithm) {
case 'sha1':
algo = CryptoJS.algo.SHA1;
break;
case 'sha256':
algo = CryptoJS.algo.SHA256;
break;
default:
throw new Error('unsupported hash algorithm. Only sha1 and sha256 are supported.');
}
this._cryptoJSHmac = CryptoJS.algo.HMAC.create(algo, key);
}
digest(encoding) {
const result = this._cryptoJSHmac.finalize();
if (encoding === 'base64') {
return result.toString(CryptoJS.enc.Base64);
}
if (encoding === 'hex') {
return result.toString(CryptoJS.enc.Hex);
}
throw new Error('unsupported output encoding. Only base64 and hex are supported.');
}
update(data) {
this._cryptoJSHmac.update(data);
return this;
}
}
//# sourceMappingURL=cryptoJSHmac.js.map