Skip to content

Instantly share code, notes, and snippets.

@bbstilson
Created March 16, 2018 20:54
Show Gist options
  • Save bbstilson/3e4287e508e34dd4725321bf20488001 to your computer and use it in GitHub Desktop.
Save bbstilson/3e4287e508e34dd4725321bf20488001 to your computer and use it in GitHub Desktop.
Encrypted homework
const express = require('express');
const app = express();
const crypto = require('crypto');
const supportedAlgos = new Set(['SHA1', 'SHA256', 'SHA512']);
app.use(express.static('./'));
app.get('/encrypt', (req, res) => {
const { algorithm, string } = req.query;
if (supportedAlgos.has(algorithm) && string.length > 0) {
try {
const encrypted = crypto.createHash(algorithm).update(string).digest('base64');
res.json({
statusCode: 200,
message: encrypted
});
} catch(error) {
res.json({
statusCode: 500,
message: 'Something went wrong. Please try again.'
});
}
} else if (string.length === 0) {
res.json({
statusCode: 400,
message: 'Empty string cannot be hashed.'
});
} else {
res.json({
statusCode: 400,
message: 'Algorithm not supported.'
});
}
});
app.listen(1337, () => console.log('Listening on port 1337'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment