From 6ee7cea56b63c04b3adbe7e84b4816686e88d14a Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Mon, 5 Mar 2018 04:10:15 +0000 Subject: [PATCH] hash util function --- commands/search/gravatar.js | 8 ++++---- commands/text-edit/md5.js | 4 ++-- commands/text-edit/sha-256.js | 4 ++-- util/Util.js | 5 +++++ 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/commands/search/gravatar.js b/commands/search/gravatar.js index 97e48edc..f27d8af4 100644 --- a/commands/search/gravatar.js +++ b/commands/search/gravatar.js @@ -1,6 +1,6 @@ const { Command } = require('discord.js-commando'); const snekfetch = require('snekfetch'); -const crypto = require('crypto'); +const { hash } = require('../../util/Util'); module.exports = class GravatarCommand extends Command { constructor(client) { @@ -22,16 +22,16 @@ module.exports = class GravatarCommand extends Command { } async run(msg, { email }) { - const hash = crypto.createHash('md5').update(email).digest('hex'); + const emailHash = hash(email, 'md5'); try { const { body } = await snekfetch - .get(`https://www.gravatar.com/avatar/${hash}`) + .get(`https://www.gravatar.com/avatar/${emailHash}`) .query({ size: 500, default: 404, rating: msg.channel.nsfw ? 'r' : 'pg' }); - return msg.say({ files: [{ attachment: body, name: `${hash}.jpg` }] }); + return msg.say({ files: [{ attachment: body, name: `${emailHash}.jpg` }] }); } catch (err) { if (err.status === 404) return msg.say('Could not find any results.'); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); diff --git a/commands/text-edit/md5.js b/commands/text-edit/md5.js index 2001b77b..7f3b7bad 100644 --- a/commands/text-edit/md5.js +++ b/commands/text-edit/md5.js @@ -1,5 +1,5 @@ const { Command } = require('discord.js-commando'); -const crypto = require('crypto'); +const { hash } = require('../../util/Util'); module.exports = class MD5Command extends Command { constructor(client) { @@ -20,6 +20,6 @@ module.exports = class MD5Command extends Command { } run(msg, { text }) { - return msg.say(crypto.createHash('md5').update(text).digest('hex')); + return msg.say(hash(text, 'md5')); } }; diff --git a/commands/text-edit/sha-256.js b/commands/text-edit/sha-256.js index f9de9217..5ae52d55 100644 --- a/commands/text-edit/sha-256.js +++ b/commands/text-edit/sha-256.js @@ -1,5 +1,5 @@ const { Command } = require('discord.js-commando'); -const crypto = require('crypto'); +const { hash } = require('../../util/Util'); module.exports = class SHA256Command extends Command { constructor(client) { @@ -20,6 +20,6 @@ module.exports = class SHA256Command extends Command { } run(msg, { text }) { - return msg.say(crypto.createHash('sha256').update(text).digest('hex')); + return msg.say(hash(text, 'sha256')); } }; diff --git a/util/Util.js b/util/Util.js index d564f969..e0376819 100644 --- a/util/Util.js +++ b/util/Util.js @@ -1,3 +1,4 @@ +const crypto = require('crypto'); const yes = ['yes', 'y', 'ye', 'yeah', 'yup', 'yea']; const no = ['no', 'n', 'nah', 'nope']; @@ -55,6 +56,10 @@ class Util { return Buffer.from(text).toString('base64'); } + static hash(text, algorithm) { + return crypto.createHash(algorithm).update(text).digest('hex'); + } + static cleanXML(text) { return text .replace(/
/g, '')