Directions:
Create an Express.js server that takes
a parameter from a browser’s request (in this case, it’s going to take a
parameter from the request called ‘id’) and PUT
it in a specified endpoint (in this case, PUT
'/message/:id').
(So that if ‘id’ is ‘526aa677a8,’ the
endpoint ends up like this: ‘/message/526aa677a8’
)
Then, for your response, return the
SHA1 hash of the current date
plus the sent ID (basically, take the
current date plus the ID and encrypt all of it!)
Use SHA1 hash to encrypt stuff this way:
require('crypto')
.createHash('sha1')
.update(new Date().toDateString() + id)
.digest('hex')
Code:
var express =
require('express')
var app = express();
app.put('/message/:id',
function (req, res) {
var id = req.params.id // ‘params’ on the IncomingMessage object is ‘params: { id:
'4b693a688d2115d70c6f1d95edac8651' }’
//
if you want to, do this to see the request from the browser: var test1 = req console.log(test1)
var str =
require('crypto'). // Now, add it to the
current date and encrypt everything
createHash('sha1').
update(new
Date().
toDateString() +
id).
digest('hex')
res.send(str)
// send the string of encrypted characters
back to the client
})
app.listen(process.argv[2]) //OK, to get the
ball rolling listen for requests here
OUTPUT:
‘1d408bcfd164a6c39dc07530c1505c353f103d98’ (yep, a bunch of gobbledygook- because it’s encrypted!)
No comments:
Post a Comment