Created
August 15, 2014 18:49
-
-
Save bachwehbi/6452fef2ac80767e3a07 to your computer and use it in GitHub Desktop.
Simple publisher: using Beebotte MQTT with secure connection, AES 256 encrypted data, QoS level 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2013-2014 Beebotte <[email protected]> | |
// This program is published under the MIT License (http://opensource.org/licenses/MIT). | |
///////////////////////////////////////////////////////////// | |
// This code uses the Beebotte API, you must have an account. | |
// You can register here: http://beebotte.com/register | |
///////////////////////////////////////////////////////////// | |
var mqtt = require('mqtt') | |
var crypto = require('crypto'); | |
//AES Cipherer | |
var aesCipher = function(opts) { | |
opts = opts || {}; | |
this.algo = opts.algo || 'aes-256-cbc'; | |
this.inputEncoding = opts.inputEncoding || 'utf8'; | |
this.outputEncoding = opts.outputEncoding || 'base64'; | |
this.key = '1234567890'; //Ciphering Key | |
//encrypts given data | |
this.encrypt = function(data) { | |
var cipher = crypto.createCipher(this.algo, this.key); | |
var encrypted = cipher.update(data, this.inputEncoding, this.outputEncoding); | |
encrypted += cipher.final(this.outputEncoding); | |
return encrypted; | |
} | |
//decrypts given encrypted data | |
this.decrypt = function(encrypted) { | |
var decipher = crypto.createDecipher(this.algo, this.key); | |
var deciphered = decipher.update(encrypted, this.outputEncoding, this.inputEncoding); | |
deciphered += decipher.final(this.inputEncoding); | |
return deciphered; | |
} | |
} | |
aes = new aesCipher(); | |
//Creates a secure (SSL) MQTT connection. Username is your private Key. | |
client = mqtt.createSecureClient(8883, 'mqtt.beebotte.com', {username: 'MY_SECRET_KEY', password: ''}); | |
setInterval(function() { | |
//Publishes an encrypted message with QoS 1 | |
client.publish('mychannel/resource', aes.encrypt('Hello World'), {qos: 1}); | |
}, 1000 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment