ethers.js/src.ts/utils/hmac.ts

21 lines
671 B
TypeScript
Raw Normal View History

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
import { arrayify } from './bytes';
2018-06-13 22:39:39 +03:00
// Imported Types
import { Arrayish } from './bytes';
2018-06-13 22:39:39 +03:00
import * as errors from './errors';
export enum SupportedAlgorithms { sha256 = 'sha256', sha512 = 'sha512' };
export function computeHmac(algorithm: SupportedAlgorithms, key: Arrayish, data: Arrayish): Uint8Array {
if (!SupportedAlgorithms[algorithm]) {
2018-06-19 09:12:57 +03:00
errors.throwError('unsupported algorithm ' + algorithm, errors.UNSUPPORTED_OPERATION, { operation: 'hmac', algorithm: algorithm });
}
return arrayify(createHmac(algorithm, Buffer.from(arrayify(key))).update(Buffer.from(arrayify(data))).digest());
2018-06-13 22:39:39 +03:00
}