ethers.js/utils/hmac.js

21 lines
952 B
JavaScript
Raw Normal View History

2018-06-13 22:39:39 +03:00
'use strict';
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
2018-06-19 09:12:57 +03:00
var crypto_1 = require("crypto");
2018-06-17 23:47:28 +03:00
var bytes_1 = require("./bytes");
2018-06-19 09:12:57 +03:00
var errors = __importStar(require("./errors"));
var supportedAlgorithms = { sha256: true, sha512: true };
function computeHmac(algorithm, key, data) {
if (!supportedAlgorithms[algorithm]) {
errors.throwError('unsupported algorithm ' + algorithm, errors.UNSUPPORTED_OPERATION, { operation: 'hmac', algorithm: algorithm });
2018-06-13 22:39:39 +03:00
}
2018-06-19 09:12:57 +03:00
return bytes_1.arrayify(crypto_1.createHmac(algorithm, new Buffer(bytes_1.arrayify(key))).update(new Buffer(bytes_1.arrayify(data))).digest());
2018-06-13 22:39:39 +03:00
}
2018-06-19 09:12:57 +03:00
exports.computeHmac = computeHmac;