hash util function

This commit is contained in:
Daniel Odendahl Jr
2018-03-05 04:10:15 +00:00
parent 511154ef23
commit 6ee7cea56b
4 changed files with 13 additions and 8 deletions
+4 -4
View File
@@ -1,6 +1,6 @@
const { Command } = require('discord.js-commando'); const { Command } = require('discord.js-commando');
const snekfetch = require('snekfetch'); const snekfetch = require('snekfetch');
const crypto = require('crypto'); const { hash } = require('../../util/Util');
module.exports = class GravatarCommand extends Command { module.exports = class GravatarCommand extends Command {
constructor(client) { constructor(client) {
@@ -22,16 +22,16 @@ module.exports = class GravatarCommand extends Command {
} }
async run(msg, { email }) { async run(msg, { email }) {
const hash = crypto.createHash('md5').update(email).digest('hex'); const emailHash = hash(email, 'md5');
try { try {
const { body } = await snekfetch const { body } = await snekfetch
.get(`https://www.gravatar.com/avatar/${hash}`) .get(`https://www.gravatar.com/avatar/${emailHash}`)
.query({ .query({
size: 500, size: 500,
default: 404, default: 404,
rating: msg.channel.nsfw ? 'r' : 'pg' 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) { } catch (err) {
if (err.status === 404) return msg.say('Could not find any results.'); 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!`); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
+2 -2
View File
@@ -1,5 +1,5 @@
const { Command } = require('discord.js-commando'); const { Command } = require('discord.js-commando');
const crypto = require('crypto'); const { hash } = require('../../util/Util');
module.exports = class MD5Command extends Command { module.exports = class MD5Command extends Command {
constructor(client) { constructor(client) {
@@ -20,6 +20,6 @@ module.exports = class MD5Command extends Command {
} }
run(msg, { text }) { run(msg, { text }) {
return msg.say(crypto.createHash('md5').update(text).digest('hex')); return msg.say(hash(text, 'md5'));
} }
}; };
+2 -2
View File
@@ -1,5 +1,5 @@
const { Command } = require('discord.js-commando'); const { Command } = require('discord.js-commando');
const crypto = require('crypto'); const { hash } = require('../../util/Util');
module.exports = class SHA256Command extends Command { module.exports = class SHA256Command extends Command {
constructor(client) { constructor(client) {
@@ -20,6 +20,6 @@ module.exports = class SHA256Command extends Command {
} }
run(msg, { text }) { run(msg, { text }) {
return msg.say(crypto.createHash('sha256').update(text).digest('hex')); return msg.say(hash(text, 'sha256'));
} }
}; };
+5
View File
@@ -1,3 +1,4 @@
const crypto = require('crypto');
const yes = ['yes', 'y', 'ye', 'yeah', 'yup', 'yea']; const yes = ['yes', 'y', 'ye', 'yeah', 'yup', 'yea'];
const no = ['no', 'n', 'nah', 'nope']; const no = ['no', 'n', 'nah', 'nope'];
@@ -55,6 +56,10 @@ class Util {
return Buffer.from(text).toString('base64'); return Buffer.from(text).toString('base64');
} }
static hash(text, algorithm) {
return crypto.createHash(algorithm).update(text).digest('hex');
}
static cleanXML(text) { static cleanXML(text) {
return text return text
.replace(/<br \/>/g, '') .replace(/<br \/>/g, '')