2018-06-19 02:14:06 -04:00
|
|
|
|
|
|
|
import * as hash from 'hash.js';
|
|
|
|
|
2018-07-22 18:25:36 -04:00
|
|
|
import { arrayify } from '../utils/bytes';
|
2018-06-19 02:14:06 -04:00
|
|
|
|
2018-07-22 18:25:36 -04:00
|
|
|
import * as errors from '../utils/errors';
|
2018-06-19 02:14:06 -04:00
|
|
|
|
2018-07-30 18:59:52 -04:00
|
|
|
///////////////////////////////
|
|
|
|
// Imported Types
|
|
|
|
|
|
|
|
import { Arrayish } from '../utils/bytes';
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
|
|
|
|
export type SupportedAlgorithms = 'sha256' | 'sha512';
|
|
|
|
|
2018-06-19 02:14:06 -04:00
|
|
|
const supportedAlgorithms = { sha256: true, sha512: true };
|
2018-06-22 20:30:50 -04:00
|
|
|
export function computeHmac(algorithm: SupportedAlgorithms, key: Arrayish, data: Arrayish): Uint8Array {
|
2018-06-19 02:14:06 -04:00
|
|
|
if (!supportedAlgorithms[algorithm]) {
|
|
|
|
errors.throwError('unsupported algorithm ' + algorithm, errors.UNSUPPORTED_OPERATION, { operation: 'hmac', algorithm: algorithm });
|
|
|
|
}
|
|
|
|
|
|
|
|
return arrayify(
|
2018-06-22 20:30:50 -04:00
|
|
|
hash.hmac(hash[algorithm], arrayify(key)).update(arrayify(data)).digest()
|
2018-06-19 02:14:06 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|