From f4163ab11e0641be12283ac4da039263af8cac71 Mon Sep 17 00:00:00 2001 From: firdaous elhalafi Date: Wed, 24 Jan 2024 10:20:53 +0100 Subject: [PATCH] Interface simple pour le choix des mots de passe --- README.md | 2 +- app/app.js | 35 +++++++++++++++++------ app/index.html | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ app/utils.js | 3 +- 4 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 app/index.html diff --git a/README.md b/README.md index 320f5da..1f9715f 100644 --- a/README.md +++ b/README.md @@ -23,5 +23,5 @@ sudo docker service create --name scaler --network host scalabilite ## Redis ```shell -sudo docker service create --name redis-db --network mon-reseau redis:alpine +sudo docker service create --name redis-db --network host redis:alpine ``` diff --git a/app/app.js b/app/app.js index eced2a6..ed0a67e 100644 --- a/app/app.js +++ b/app/app.js @@ -3,11 +3,16 @@ const crypto = require('crypto'); const redis = require('redis'); const WebSocket = require('ws'); const http = require('http'); +const path = require('path'); +const bodyParser = require('body-parser'); const { generateSimulatedHash } = require('./utils'); const app = express(); + app.use(express.json()); +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: true })); // le client Redis const client = redis.createClient({ @@ -44,8 +49,12 @@ wss.on('connection', (ws) => { }); }); +app.get('/', (req, res) => { + res.sendFile(path.join(__dirname, 'index.html')); +}); + app.post('/generateHash', (req, res) => { - const difficulty = req.body.data; + const difficulty = req.body.complexity; if (!difficulty) { return res.status(400).json({error: 'Missing data in the request.'}); @@ -58,16 +67,21 @@ app.post('/generateHash', (req, res) => { } }); - res.json({hash}); + res.redirect('/'); }); -app.post('/calculate-md5', async (req, res) => { +app.post('/generateHashOfPassword', async (req, res) => { try { - const text = req.body.text; - const hash = crypto.createHash('md5').update(text).digest('hex'); + const password = req.body.password; + + if (!password) { + return res.status(400).json({error: 'Missing data in the request.'}); + } + + const hash = crypto.createHash('md5').update(password).digest('hex'); // on stocke le hash dans Redis avec la chaîne originale comme clé - await client.set(text, hash); + // await client.set(text, hash); // Envoi du texte au service esclave via WebSocket wss.clients.forEach((client) => { @@ -76,21 +90,26 @@ app.post('/calculate-md5', async (req, res) => { } }); - res.json({ text: text, hash: hash }); + res.redirect('/'); } catch (err) { res.status(500).json({ error: 'Erreur du serveur' }); } }); -app.get('/get-md5/:text', async (req, res) => { +app.get('/getHash/:text', async (req, res) => { try { const text = req.params.text; + if (!text) { + return res.status(400).json({error: 'Missing data in the request.'}); + } + //récupérer le hash de Redis const hash = await client.get(text); if (hash === null) { return res.status(404).json({ error: 'Texte non trouvé' }); } + res.json({ text: text, hash: hash }); } catch (err) { res.status(500).json({ error: 'Erreur du serveur' }); diff --git a/app/index.html b/app/index.html new file mode 100644 index 0000000..c2cd27e --- /dev/null +++ b/app/index.html @@ -0,0 +1,76 @@ + + + + + + Formulaire de Mot de Passe + + + + +
+

Formulaire de Mot de Passe

+ + +
+
+ + +
+ + + + + +
+
+ + + + + diff --git a/app/utils.js b/app/utils.js index accc27f..caf77b5 100644 --- a/app/utils.js +++ b/app/utils.js @@ -5,11 +5,12 @@ function generateSimulatedHash(difficulty) { if (difficulty === 'gentil') { data = crypto.randomBytes(16).toString('hex'); // Mode gentil (faible complexité) } else if (difficulty === 'normal') { - data = crypto.randomBytes(1000).toString('hex'); // Mode normal (moyenne complexité) + data = crypto.randomBytes(500).toString('hex'); // Mode normal (moyenne complexité) } else if (difficulty === 'agressif') { data = crypto.randomBytes(1000).toString('hex'); // Mode agressif (haute complexité) } + console.log(data); const hash = crypto.createHash('md5').update(data).digest('hex'); return hash; } -- GitLab