From 79dd53e4c4bee4ae6be8c9127bc1d1d3c17cd157 Mon Sep 17 00:00:00 2001 From: amki Date: Sat, 25 Apr 2015 04:06:01 +0200 Subject: [PATCH] Moved crypto module to internal node crypto api, removed (pretty) senseless cipher command and added random command --- install | 2 +- modules/crypto/README.md | 14 ++++-------- modules/crypto/crypto.js | 48 +++++++++++++++++++++++----------------- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/install b/install index 2279732..4e7573d 100755 --- a/install +++ b/install @@ -14,7 +14,7 @@ then exit 1 fi -npm install googlemaps feedparser node-units tvdb crypto-js method-override 500px process async wordnik node-uuid underscore request sandbox express moment-timezone moment jade databank databank-redis ent passport passport-local password-hash connect-flash +npm install googlemaps feedparser node-units tvdb method-override 500px process async wordnik node-uuid underscore request sandbox express moment-timezone moment jade databank databank-redis ent passport passport-local password-hash connect-flash cd public/ wget https://github.com/twbs/bootstrap/releases/download/v3.3.2/bootstrap-3.3.2-dist.zip diff --git a/modules/crypto/README.md b/modules/crypto/README.md index d037b42..e866796 100644 --- a/modules/crypto/README.md +++ b/modules/crypto/README.md @@ -7,15 +7,9 @@ This module calculates different hashes or ciphertexts for some algorithms. ### Commands -#### ~md5 [text] -Calculate the MD5 hash of the given text. +#### ~hash [algorithm] [text] +Calculate the hash of the given text using [algorithm]. -#### ~sha1 [text] -Calculate the SHA1 hash of the given text. - -#### ~sha256 [text] -Calculate the SHA256 hash of the given text. - -#### ~aes "[text]" "[key]" -Calculates the hash of the given text. +#### ~random [number] +Gives [number] bytes of cryptographically strong pseudo-random data as hex string. diff --git a/modules/crypto/crypto.js b/modules/crypto/crypto.js index 842b720..2d25aac 100644 --- a/modules/crypto/crypto.js +++ b/modules/crypto/crypto.js @@ -3,32 +3,40 @@ * Description: Allows the magic of cryptography to take place. */ -var MD5 = require('crypto-js/md5'); -var SHA1 = require('crypto-js/sha1'); -var SHA256 = require('crypto-js/sha256'); -var SHA512 = require('crypto-js/sha512'); -var AES = require('crypto-js/aes'); +var cr = require('crypto'); var crypto = function(dbot) { this.commands = { - '~md5': function(event) { - event.reply("MD5 hash of "+event.input[1]+" is: "+MD5(event.input[1])); + '~hash': function(event) { + var hash = event.params[1]; + try { + var h = cr.createHash(hash); + var tohash = event.params.splice(2, event.params.length-1).join(' '); + h.update(tohash); + event.reply(hash+" of \""+tohash+"\" is: "+h.digest('hex')); + } catch(err) { + event.reply(err); + } }, - '~sha1': function(event) { - event.reply("SHA1 hash of "+event.input[1]+" is: "+SHA1(event.input[1])); - }, - '~sha256': function(event) { - event.reply("SHA256 hash of "+event.input[1]+" is: "+SHA256(event.input[1])); - }, - '~aes': function(event) { - event.reply("AES of "+event.input[1]+" is: "+AES.encrypt(event.input[1],event.input[2])); + '~random': function(event) { + try { + var count = parseInt(event.params[1]); + if(count > 222) { + event.reply("Sorry man, I can't paste that much random data."); + return; + } + cr.randomBytes(count, function(err,buf) { + if(err) { + event.reply(err); + return; + } + event.reply(buf.toString('hex')); + }); + } catch (err) { + event.reply(err); + } } }; - - this.commands['~md5'].regex = /^md5 ([^ ]+)$/; - this.commands['~sha1'].regex = /^sha1 ([^ ]+)$/; - this.commands['~sha256'].regex = /^sha256 ([^ ]+)$/; - this.commands['~aes'].regex = /^aes "(.*)" "(.*)"$/; }; exports.fetch = function(dbot) {