I am currently working on an application which allows users to save sensitive date. Since it's a web application we are using NodeJS
and MongoDB
for persistence. (BTW I am completely new to Node and NoSQL)
We do have users who can store kind of a medical history. Name and email are stored within a user document while the other stuff is stored within the profile.
To improve security I would like to encrypt
the references from a user to his profile and vice versa.
At the moment I am using the Crypto
library of NodeJS
to encrypt (AES256) the user_id
reference within the users profile. As a consequence the reference is not a type of ObjectID anymore but a string
So by viewing the database directly it is not possible to check which profile belongs to which user. The secret key to encrypt
and decrypt
the users id is stored somewhere in a js file of the NodeJS
server.
Is this a common/good way or am I doing something completely wrong? Are there any better ways – I read that mongoDB is not supporting any "built in encryption"
At least, here is the code for the en/decryption
module.exports = function() {
this.encryptionSecret = "ANYSECRET";
this.crypto = require('crypto');
this.algorithm = 'aes256';
this.encrypt = function (key) {
var cipher = this.crypto.createCipher(this.algorithm, this.encryptionSecret);
var encrypted = cipher.update(""+key, 'utf8', 'hex') + cipher.final('hex');
return encrypted;
};
this.decrypt = function (encryptedKey) {
var decipher = this.crypto.createDecipher(this.algorithm, this.encryptionSecret);
var decrypted = decipher.update(encryptedKey, 'hex', 'utf8') + decipher.final('utf8');
return decrypted;
};
};