2018-05-17 17:11:34 +02:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Operation from "../Operation";
|
|
|
|
import Utils from "../Utils";
|
|
|
|
import CryptoApi from "crypto-api/src/crypto-api";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HMAC operation
|
|
|
|
*/
|
|
|
|
class HMAC extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HMAC constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "HMAC";
|
|
|
|
this.module = "Hashing";
|
|
|
|
this.description = "Keyed-Hash Message Authentication Codes (HMAC) are a mechanism for message authentication using cryptographic hash functions.";
|
2018-08-21 20:07:13 +02:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/HMAC";
|
2018-05-17 17:11:34 +02:00
|
|
|
this.inputType = "ArrayBuffer";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
"name": "Key",
|
2018-10-12 13:05:32 +02:00
|
|
|
"type": "toggleString",
|
|
|
|
"value": "",
|
|
|
|
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"]
|
2018-05-17 17:11:34 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Hashing function",
|
|
|
|
"type": "option",
|
|
|
|
"value": [
|
|
|
|
"MD2",
|
|
|
|
"MD4",
|
|
|
|
"MD5",
|
|
|
|
"SHA0",
|
|
|
|
"SHA1",
|
|
|
|
"SHA224",
|
|
|
|
"SHA256",
|
|
|
|
"SHA384",
|
|
|
|
"SHA512",
|
|
|
|
"SHA512/224",
|
|
|
|
"SHA512/256",
|
|
|
|
"RIPEMD128",
|
|
|
|
"RIPEMD160",
|
|
|
|
"RIPEMD256",
|
|
|
|
"RIPEMD320",
|
|
|
|
"HAS160",
|
|
|
|
"Whirlpool",
|
|
|
|
"Whirlpool-0",
|
|
|
|
"Whirlpool-T",
|
|
|
|
"Snefru"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {ArrayBuffer} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
2018-10-12 13:05:32 +02:00
|
|
|
const key = Utils.convertToByteString(args[0].string || "", args[0].option),
|
2018-05-17 17:11:34 +02:00
|
|
|
hashFunc = args[1].toLowerCase(),
|
|
|
|
msg = Utils.arrayBufferToStr(input, false),
|
|
|
|
hasher = CryptoApi.getHasher(hashFunc);
|
|
|
|
|
|
|
|
const mac = CryptoApi.getHmac(CryptoApi.encoder.fromUtf(key), hasher);
|
|
|
|
mac.update(msg);
|
|
|
|
return CryptoApi.encoder.toHex(mac.finalize());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default HMAC;
|