azat-co / expressworks

Learn Express.js from the author of one of the best books on Express.js—Pro Express.js— with this workshop that will teach you basics of Express.js.
MIT License
715 stars 220 forks source link

Crypto Hashes are coming different #130

Closed phoenisx closed 5 years ago

phoenisx commented 7 years ago

Your submission results compared to the expected:

               ACTUAL                                     EXPECTED

"c6792a568813a595f2762f4abf9cb603e4771dd4"!="39536007b53759db8a9262a595f77c8647b689c0"

✗ Submission results did not match expected!

Always getting a different value than expected, even though the program is as expected..

const express = require('express');
// const crypto = require('crypto');

var app = express();

app.put("/message/:id", function(req, res) {
  var crypted = require('crypto').createHash('sha1')
                      .update(new Date().toDateString() + req.param.id)
                      .digest('hex');

  res.send(crypted);
})

app.listen(process.argv[2]);
acrenwelge commented 7 years ago

Yup, I'm having the same problem too

var express = require('express');
var app = express();
var port = process.argv[2];

app.put('/message/:id', function(req,res) {
    res.send(require('crypto')
      .createHash('sha1')
      .update(new Date().toDateString() + req.param.id)
      .digest('hex'));
})

app.listen(port);
smachen commented 7 years ago

@Shub1427 @acrenwelge
parameters from within the request handlers, use:

    req.params.NAME

It's req.# params, not req.# param

jarifibrahim commented 7 years ago

Your submission results compared to the expected:

             ACTUAL                                 EXPECTED                

────────────────────────────────────────────────────────────────

"56b1b7981edc3f048173b5168d4d5c59cd90b0be" != "2a604ba8cfc3c332609b71fa07688ec79cc7bf99"

────────────────────────────────────────────────────────────────

✗ Submission results did not match expected!

var express = require('express');
var app = express();

app.put('/message/:id', function(req, res){
    var id = req.params.id;
    var str = require('crypto')
     .createHash('sha1')
     .update(new Date().toString() + id)
     .digest('hex');
    res.send(str);
});

app.listen(process.argv[2]);

@smachen This solution doesn't work as well.

jarifibrahim commented 7 years ago

I found my mistake. It should be new Date().toDateString() and not new Date().toString().