ethers.js/src.ts/shims/hmac.ts

27 lines
734 B
TypeScript
Raw Normal View History

import * as hash from 'hash.js';
2018-07-23 01:25:36 +03:00
import { arrayify } from '../utils/bytes';
2018-07-23 01:25:36 +03:00
import * as errors from '../utils/errors';
///////////////////////////////
// Imported Types
import { Arrayish } from '../utils/bytes';
///////////////////////////////
export enum SupportedAlgorithms { sha256 = 'sha256', sha512 = 'sha512' };
export function computeHmac(algorithm: SupportedAlgorithms, key: Arrayish, data: Arrayish): Uint8Array {
if (!SupportedAlgorithms[algorithm]) {
errors.throwError('unsupported algorithm ' + algorithm, errors.UNSUPPORTED_OPERATION, { operation: 'hmac', algorithm: algorithm });
}
return arrayify(
hash.hmac(hash[algorithm], arrayify(key)).update(arrayify(data)).digest()
);
}