insomnia/plugins/insomnia-plugin-hash/index.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

const crypto = require('crypto');
2017-11-01 11:24:00 +00:00
2018-06-25 17:42:50 +00:00
module.exports.templateTags = [
{
name: 'hash',
displayName: 'Hash',
2018-07-23 17:24:41 +00:00
description: 'apply hash to a value',
2018-06-25 17:42:50 +00:00
args: [
{
displayName: 'Algorithm',
type: 'enum',
options: [
{ displayName: 'MD5', value: 'md5' },
{ displayName: 'SHA1', value: 'sha1' },
{ displayName: 'SHA256', value: 'sha256' },
{ displayName: 'SHA512', value: 'sha512' },
],
2018-06-25 17:42:50 +00:00
},
{
displayName: 'Digest Encoding',
description: 'The encoding of the output',
type: 'enum',
options: [
{ displayName: 'Hexadecimal', value: 'hex' },
{ displayName: 'Base64', value: 'base64' },
],
2018-06-25 17:42:50 +00:00
},
{
displayName: 'Input',
type: 'string',
placeholder: 'Value to hash',
},
2018-06-25 17:42:50 +00:00
],
run(context, algorithm, encoding, value = '') {
2018-10-17 16:42:33 +00:00
if (encoding !== 'hex' && encoding !== 'latin1' && encoding !== 'base64') {
throw new Error(`Invalid encoding ${encoding}. Choices are hex, latin1, base64`);
2018-06-25 17:42:50 +00:00
}
2017-11-01 11:24:00 +00:00
2018-06-25 17:42:50 +00:00
const valueType = typeof value;
if (valueType !== 'string') {
throw new Error(`Cannot hash value of type "${valueType}"`);
}
2017-11-01 11:24:00 +00:00
2018-06-25 17:42:50 +00:00
const hash = crypto.createHash(algorithm);
hash.update(value || '', 'utf8');
return hash.digest(encoding);
},
},
2018-06-25 17:42:50 +00:00
];