Created
October 3, 2017 06:38
-
-
Save devesh2605/dad26461162f6b041ab71d050f708b4a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var express = require('express'), | |
app = express(), | |
cors = require('cors'), | |
helmet = require('helmet'), | |
mysql = require('mysql'), | |
bodyParser = require('body-parser'), | |
crypto = require('crypto'), | |
algorithm = 'aes-256-ctr', | |
password = 'zyxcba', | |
port = 3000; | |
app.use(cors()); | |
app.use(helmet()); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ | |
extended: false | |
})); | |
var database = mysql.createPool({ | |
connectionLimit : 100, | |
host: 'localhost', | |
user: '*****', | |
password: '*****', | |
database: 'devesh', | |
port: 3306 | |
}); | |
/** | |
* Encrypt function | |
*/ | |
function encrypt(text){ | |
var cipher = crypto.createCipher(algorithm,password) | |
var crypted = cipher.update(text,'utf8','hex') | |
crypted += cipher.final('hex'); | |
return crypted; | |
} | |
/** | |
* Decrypt function | |
*/ | |
function decrypt(text){ | |
var decipher = crypto.createDecipher(algorithm,password) | |
var dec = decipher.update(text,'hex','utf8') | |
dec += decipher.final('utf8'); | |
return dec; | |
} | |
app.post('/encrypt',function(req,res){ | |
const appData = {}; | |
const first_name = req.body.first_name; | |
const last_name = req.body.last_name; | |
const age = req.body.age; | |
const userData = { | |
first_name: encrypt(first_name), | |
last_name : encrypt(last_name), | |
age: age | |
} | |
database.getConnection(function(err,connection){ | |
if(err){ | |
appData['error'] = 1; | |
appData['data'] = 'Internal server error' | |
res.status(500).json(appData); | |
}else{ | |
connection.query('INSERT INTO employee SET?',userData,function(err,rows,fields){ | |
if(err){ | |
appData['error'] = 1; | |
appData['data'] = 'Error adding data' | |
res.status(500).json(appData); | |
}else{ | |
appData['error'] = 0; | |
appData['data'] = 'Data saved in encrypted form succesfully!' | |
res.status(200).json(appData); | |
} | |
}); | |
connection.release(); | |
} | |
}); | |
}); | |
app.listen(port, function(){ | |
console.log('Server listening to port',port); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment