Last active
January 15, 2019 04:17
-
-
Save kakopappa/cba7852b26f2cb59d3712690f0ebde24 to your computer and use it in GitHub Desktop.
hmac nodejs esp8266
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://github.com/rcarmo/azure-iot-esp-01-minimal-cpp/blob/master/iothub.cpp | |
//http://bitoftech.net/2014/12/15/secure-asp-net-web-api-using-api-key-authentication-hmac-authentication/ | |
/* | |
var hmac = crypto.createHmac('sha256', secret_key); | |
hmac.update(request.body.message); | |
var signature = hmac.digest('hex')); | |
*/ | |
var str = payload_string; | |
var public_key = pk; | |
var secret_key = sk; | |
var hmac = crypto.createHmac('sha384', sk).update(str).digest('hex'); | |
request.post({uri:..., json:{hmac:hmac, public_key:public_key, payload: str}, function(err, response, body){ | |
console.log(body); | |
}); | |
exports.... = function(req, res) | |
{ | |
var hmac = req.body.hmac; | |
var pk = req.body.public_key; | |
var payload = req.body.payload; | |
// retrieve authorized user | |
User.findOne({pk:pk},function(err, user){ | |
if(err || !user){ | |
return res.status(403).json({error:"Invalid user"}); | |
} | |
else | |
{ | |
// recompute hmac | |
var compute_hmac= crypto.createHmac('sha384', user.sk).update(payload).digest('hex'); | |
// check hmac | |
if(compute_hmac != hmac){ | |
return res.status(403).json({error:"Security check failed"}); | |
} | |
else | |
{ | |
// do stg | |
return res.status(200).json({success:"ok"}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment