Skip to content

Instantly share code, notes, and snippets.

@mariorodriguespt
Created March 14, 2015 00:07
Show Gist options
  • Save mariorodriguespt/b8c8a96ad2eccfb9b359 to your computer and use it in GitHub Desktop.
Save mariorodriguespt/b8c8a96ad2eccfb9b359 to your computer and use it in GitHub Desktop.
//atmosphere packages: jparker:crypto-aes , matb33:collection-hooks
var encryptionKey = "adsffe534tryertrrtweGe";
//Before insert Hooks, I decide to use it only in the server. If you use on the client and server, you're encrypting it twice.
Todos.before.insert( function( userId , doc ){
doc.text = CryptoJS.AES.encrypt( doc.text , encryptionKey ).toString();
} );
Todos.before.update(function (userId, doc, fieldNames, modifier, options) {
if( modifier.$set && modifier.$set.text ){
modifier.$set.text =
CryptoJS.AES.encrypt( modifier.$set.text , encryptionKey )
.toString();
}
});
//Publishing and decrypting
Meteor.publish('todos', function(listId) {
check( listId , String );
var cursor = Todos.find( { listId : listId } ),
self = this;
var handle = cursor.observeChanges({
added : function( id , fields ){
fields.text = CryptoJS.AES
.decrypt( fields.text , encryptionKey )
.toString( CryptoJS.enc.Utf8 );
self.added( 'todos' , id , fields );
},
changed : function( id, fields ){
if( fields.text ){
fields.text = CryptoJS.AES
.decrypt( fields.text, encryptionKey )
.toString( CryptoJS.enc.Utf8);
}
self.changed('todos', id, fields);
},
removed : function( id ){
self.removed( 'todos' , id );
}
});
self.onStop( function () {
handle.stop();
} );
self.ready();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment