Skip to content

Instantly share code, notes, and snippets.

@agreatfool
Last active February 27, 2018 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agreatfool/a55f5cbdd66d097ba4855e47d42338e5 to your computer and use it in GitHub Desktop.
Save agreatfool/a55f5cbdd66d097ba4855e47d42338e5 to your computer and use it in GitHub Desktop.
{
"name": "profile",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"dependencies": {
"express": "^4.16.2"
}
}
#!/usr/bin/env bash
curl -X GET "http://localhost:5000/newUser?username=matt&password=password"
ab -k -c 20 -n 250 "http://localhost:5000/auth?username=matt&password=password"
#!/usr/bin/env bash
BASEDIR=$(dirname "$0")
cd ${BASEDIR}/../
NODE_ENV=production node --prof profile/server.js
'use strict';
const crypto = require('crypto');
const express = require('express');
const app = express();
let users = [];
app.get('/newUser', (req, res) => {
let username = req.query.username || '';
const password = req.query.password || '';
username = username.replace(/[!@#$%^&*]/g, '');
if (!username || !password || users.username) {
return res.sendStatus(400);
}
const salt = crypto.randomBytes(128).toString('base64');
const hash = crypto.pbkdf2Sync(password, salt, 10000, 512, 'sha512');
users[username] = { salt, hash };
res.sendStatus(200);
});
app.get('/auth', (req, res) => {
let username = req.query.username || '';
const password = req.query.password || '';
username = username.replace(/[!@#$%^&*]/g, '');
if (!username || !password || !users[username]) {
return res.sendStatus(400);
}
const hash = crypto.pbkdf2Sync(password, users[username].salt, 10000, 512, 'sha512');
if (users[username].hash.toString() === hash.toString()) {
res.sendStatus(200);
} else {
res.sendStatus(401);
}
});
app.listen(5000);
console.log('Server started, listening on port 5000 ...');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment