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-07-16 10:27:49 +03:00
|
|
|
import { arrayify } from './bytes';
|
2018-06-13 22:39:39 +03:00
|
|
|
|
2018-07-31 01:59:52 +03:00
|
|
|
// Imported Types
|
|
|
|
import { Arrayish } from './bytes';
|
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-08-02 00:33:23 +03:00
|
|
|
export enum SupportedAlgorithms { sha256 = 'sha256', sha512 = 'sha512' };
|
2018-07-31 01:59:52 +03:00
|
|
|
|
2018-06-23 03:30:50 +03:00
|
|
|
export function computeHmac(algorithm: SupportedAlgorithms, key: Arrayish, data: Arrayish): Uint8Array {
|
2018-08-02 00:33:23 +03:00
|
|
|
if (!SupportedAlgorithms[algorithm]) {
|
2018-06-19 09:12:57 +03:00
|
|
|
errors.throwError('unsupported algorithm ' + algorithm, errors.UNSUPPORTED_OPERATION, { operation: 'hmac', algorithm: algorithm });
|
|
|
|
}
|
2018-09-04 17:20:31 +03:00
|
|
|
return arrayify(createHmac(algorithm, Buffer.from(arrayify(key))).update(Buffer.from(arrayify(data))).digest());
|
2018-06-13 22:39:39 +03:00
|
|
|
}
|
|
|
|
|