2018-06-19 09:14:06 +03:00
|
|
|
|
|
|
|
import * as hash from 'hash.js';
|
|
|
|
|
|
|
|
import { arrayify, Arrayish } from '../src.ts/utils/bytes';
|
|
|
|
|
|
|
|
import * as errors from '../src.ts/utils/errors';
|
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
export type SupportedAlgorithms = 'sha256' | 'sha512';
|
2018-06-19 09:14:06 +03:00
|
|
|
|
|
|
|
const supportedAlgorithms = { sha256: true, sha512: true };
|
2018-06-23 03:30:50 +03:00
|
|
|
export function computeHmac(algorithm: SupportedAlgorithms, key: Arrayish, data: Arrayish): Uint8Array {
|
2018-06-19 09:14:06 +03:00
|
|
|
if (!supportedAlgorithms[algorithm]) {
|
|
|
|
errors.throwError('unsupported algorithm ' + algorithm, errors.UNSUPPORTED_OPERATION, { operation: 'hmac', algorithm: algorithm });
|
|
|
|
}
|
|
|
|
|
|
|
|
return arrayify(
|
2018-06-23 03:30:50 +03:00
|
|
|
hash.hmac(hash[algorithm], arrayify(key)).update(arrayify(data)).digest()
|
2018-06-19 09:14:06 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|