2
0
mirror of synced 2025-02-25 04:25:16 +00:00

18 lines
691 B
TypeScript
Raw Normal View History

2018-06-13 15:39:39 -04:00
'use strict';
2018-06-19 02:12:57 -04:00
import { createHmac } from 'crypto';
2018-06-13 15:39:39 -04:00
2018-06-17 16:47:28 -04:00
import { arrayify, Arrayish } from './bytes';
2018-06-13 15:39:39 -04:00
2018-06-19 02:12:57 -04:00
import * as errors from './errors';
2018-06-13 15:39:39 -04:00
2018-06-19 02:12:57 -04:00
const supportedAlgorithms = { sha256: true, sha512: true };
export function computeHmac(algorithm: string, key: Arrayish, data: Arrayish): Uint8Array {
if (!supportedAlgorithms[algorithm]) {
errors.throwError('unsupported algorithm ' + algorithm, errors.UNSUPPORTED_OPERATION, { operation: 'hmac', algorithm: algorithm });
}
//return arrayify(_hmac(_hash[algorithm], arrayify(key)).update(arrayify(data)).digest());
return arrayify(createHmac(algorithm, new Buffer(arrayify(key))).update(new Buffer(arrayify(data))).digest());
2018-06-13 15:39:39 -04:00
}