Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. var express = require('express'),
  2. app = express(),
  3. cors = require('cors'),
  4. helmet = require('helmet'),
  5. mysql = require('mysql'),
  6. bodyParser = require('body-parser'),
  7. crypto = require('crypto'),
  8. algorithm = 'aes-256-ctr',
  9. password = 'zyxcba',
  10. port = 3000;
  11.  
  12. app.use(cors());
  13. app.use(helmet());
  14. app.use(bodyParser.json());
  15. app.use(bodyParser.urlencoded({
  16. extended: false
  17. }));
  18.  
  19. var database = mysql.createPool({
  20. connectionLimit : 100,
  21. host: 'localhost',
  22. user: '*****',
  23. password: '*****',
  24. database: 'devesh',
  25. port: 3306
  26. });
  27.  
  28. /**
  29. * Encrypt function
  30. */
  31. function encrypt(text){
  32. var cipher = crypto.createCipher(algorithm,password)
  33. var crypted = cipher.update(text,'utf8','hex')
  34. crypted += cipher.final('hex');
  35. return crypted;
  36. }
  37.  
  38. /**
  39. * Decrypt function
  40. */
  41. function decrypt(text){
  42. var decipher = crypto.createDecipher(algorithm,password)
  43. var dec = decipher.update(text,'hex','utf8')
  44. dec += decipher.final('utf8');
  45. return dec;
  46. }
  47.  
  48. app.post('/encrypt',function(req,res){
  49.  
  50. const appData = {};
  51. const first_name = req.body.first_name;
  52. const last_name = req.body.last_name;
  53. const age = req.body.age;
  54.  
  55. const userData = {
  56. first_name: encrypt(first_name),
  57. last_name : encrypt(last_name),
  58. age: age
  59. }
  60.  
  61. database.getConnection(function(err,connection){
  62. if(err){
  63. appData['error'] = 1;
  64. appData['data'] = 'Internal server error'
  65. res.status(500).json(appData);
  66. }else{
  67. connection.query('INSERT INTO employee SET?',userData,function(err,rows,fields){
  68. if(err){
  69. appData['error'] = 1;
  70. appData['data'] = 'Error adding data'
  71. res.status(500).json(appData);
  72. }else{
  73. appData['error'] = 0;
  74. appData['data'] = 'Data saved in encrypted form succesfully!'
  75. res.status(200).json(appData);
  76. }
  77. });
  78. connection.release();
  79. }
  80. });
  81. });
  82.  
  83. app.listen(port, function(){
  84. console.log('Server listening to port',port);
  85. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement