2018-06-13 22:39:39 +03:00
|
|
|
'use strict';
|
|
|
|
|
2018-06-19 09:12:57 +03:00
|
|
|
import { createHmac } from 'crypto';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-06-17 23:47:28 +03:00
|
|
|
import { arrayify, Arrayish } from './bytes';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-07-15 00:19:08 +03:00
|
|
|
import { SupportedAlgorithms } from './types';
|
|
|
|
export { SupportedAlgorithms };
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-07-15 00:19:08 +03:00
|
|
|
import * as errors from './errors';
|
2018-06-23 03:30:50 +03:00
|
|
|
|
2018-06-19 09:12:57 +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:12:57 +03:00
|
|
|
if (!supportedAlgorithms[algorithm]) {
|
|
|
|
errors.throwError('unsupported algorithm ' + algorithm, errors.UNSUPPORTED_OPERATION, { operation: 'hmac', algorithm: algorithm });
|
|
|
|
}
|
|
|
|
return arrayify(createHmac(algorithm, new Buffer(arrayify(key))).update(new Buffer(arrayify(data))).digest());
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|