Tag: hmac

跨语言HMAC / SHA256 / Base64的问题

我正在使用node.js脚本为azure documentDB创build一个签名 – 简化版本是(结果在底部): – var crypto = require("crypto"); var masterKey = "ABCDE" var key = new Buffer(masterKey, "base64"); var signature = crypto.createHmac("sha256", key).update("FGHIJ").digest("base64"); console.log("\n\n"+signature) // RNkID54/1h1H9p3NWPeRA0mOW2L0c0HUJGTTY2GPbDo= 这工作,并做我需要的。 我试图用CommonCrypto在Swift中做同样的事情 let keyString = "ABCDE" let body = "FGHIJ" let utf8data = keyString.dataUsingEncoding(NSUTF8StringEncoding) let key = utf8data!.base64EncodedDataWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) let str = body.cStringUsingEncoding(NSUTF8StringEncoding) let strLen = body.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) let […]

HMAC签名与github中的x-hub-signature不匹配

我正在处理来自github的传入Webhook,并且想要validationx-hub-signature。 我使用hmac来散列“秘密”,然后比较这两个哈希值。 问题是他们从来不匹配。 这是我的设置: router.route("/auth") .post((req, res) => { var hmac = crypto.createHmac("sha1", process.env.WEBHOOK_SECRET); var calculatedSignature = "sha1=" + hmac.update(JSON.stringify(req.body)).digest("hex"); console.log(req.headers["x-hub-signature"] === calculatedSignature); // Returns false console.log(req.headers["x-hub-signature"]) // => sha1=blablabla console.log(calculatedSignature) // => sha1=foofoofoo res.end(); }); 我已经尝试了一切,但不能使它工作。 想知道如果hmac.update()应该保存另一个参数比JSON.stringify(req.body) 。 有谁知道他们为什么不匹配?

crypto createHMAC输出根据nodejs版本的不同而不同

在升级我的节点版本时,我遇到了encryption模块问题。 创build的HMAC取决于节点的版本。 您会在下面find重现问题的代码。 如果我将密钥编码为BASE64(或任何),则HMAC不依赖于node.js版本。 如果我把它编码为二进制,如果我改变我的node.js版本,HMAC是不同的。 [编辑]根据为什么crypto.createHash在新版本中返回不同的输出? 调用update函数时添加了编码 代码片段 "use strict"; const crypto = require('crypto'); console.log(process.version); let key = '5ece799aa73a7a8e687876f8e0eabe2e200b967ef5728d845f72fc9ea27dbcd90cd4e06e8bc90d823ac8a54ce91f68ca37fc2e7bbf3f5ef9d82b4c6b938f1936'; let _key64 = (new Buffer(key, 'hex')).toString('base64'); console.log("B64 KEY: "+crypto.createHmac('sha512', _key64).update("hey", "binary").digest('hex').toUpperCase()); let _keyBin = (new Buffer(key, 'hex')).toString('binary'); console.log("BIN KEY: "+crypto.createHmac('sha512', _keyBin).update("hey", "binary").digest('hex').toUpperCase()); 输出是以下两个版本的node.js v5.6.0 B64 KEY: 0DC11C737E575B17DD575042F8F372E3D63A86C3B56C06FB74C9B0AB8E96A5FC8A2DC33667280DC5B306C93AA3DECBAF0D8EDE56F3666C11BFC25A70CFC027D0 BIN KEY: E5A9F813D9AA64A6791BEA91035553FFC730DBE635D0CE7AC722C0195DFDD77A969323FDDFB4E5054E59073DAE9B9BF00CFF73CF20F2FACEE01F79F25E7B9303 v8.1.4 B64 KEY: 0DC11C737E575B17DD575042F8F372E3D63A86C3B56C06FB74C9B0AB8E96A5FC8A2DC33667280DC5B306C93AA3DECBAF0D8EDE56F3666C11BFC25A70CFC027D0 BIN KEY: […]

Nodejs – Expressjs – validationwebify的shopify

我正在尝试validation在开发环境中从shopify webhook发送的hmac代码。 然而shopify不会发送一个webhook发送到一个非活的端点的请求,所以我使用requestbin捕获请求,然后使用postman发送到我的本地networking服务器。 从shopify 文档 ,我似乎做的一切正常,也尝试应用在node-shopify-auth verifyWebhookHMAC函数中使用的方法。 但迄今为止这一切都没有奏效。 代码从来不匹配。 我在这里做错了什么? 我的代码来validationwebhook: function verifyWebHook(req, res, next) { var message = JSON.stringify(req.body); //Shopify seems to be escaping forward slashes when the build the HMAC // so we need to do the same otherwise it will fail validation // Shopify also seems to replace '&' with \u0026 … […]

试图嘲笑github webhook请求,得到:“X-Hub-Signature不匹配blob签名”

这是一个小代理服务器设置来处理github webhooks: require('dotenv').config(); var http = require('http'); var createHandler = require('github-webhook-handler'); var handler = createHandler({ path: '/webhook', secret: process.env.GIT_WEBHOOK_SECRET }); http .createServer(function(req, res) { handler(req, res, function(err) { res.statusCode = 404; res.end('no such location'); }); }) .listen(8080); handler.on('error', function(err) { console.error('Error:', err.message); }); handler.on('push', function(event) { console.log( 'Received a push event for %s to %s', […]

Node.jsencryption中的HMAC与Google Apps脚本(GAS)

你可以解释一下,使用Node.JS的Crypto模块创buildHmacSha512签名和Google Apps脚本有什么不同吗? 代码1 – Node.JS var secret = "my secret"; var message = "message"; var crypto = require("crypto"); var hmac = new crypto.createHmac("sha512", secret); var signature = hmac.update(message).digest("base64"); console.log(signature); 代码1 – Google Apps脚本 var secret = "my secret"; var message = "message"; var signature = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, message, secret)); Logger.log(signature); 两个代码都会生成相同的签名: g4fZkM2XGNjhti9Wah3TU2/rvmxbL3nk4F3ZLljpED23oQ7Y7dtVmVKprQKuzyt0B4Spo214isWCvnoXXVTS8g== 但是当我们拥有base64编码密钥forms的秘密时,问题就来了。 所以,我们要做的第一步就是准备这个秘密。 我们来修改代码: 代码2 […]

如何validation超级webhook api?

优步文件说 此字段的值是webhook HTTP请求正文的hexHMAC签名,使用客户端密钥作为密钥,SHA256作为哈希函数。 但是HTTP请求体是什么? 我认为这是从webhook( https://developer.uber.com/docs/webhooks#section-example-post )收到的JSON正文。 如果是这样,那么如何在NodeJS中validation它作为HMAC的encryption模块不接受JSON [我尝试过JSONstring化,但它会生成一个不同的哈希]。 或者如何将JSON转换为缓冲区,因为这是下一个最佳select 如果不是,那我该用什么? [UPDATE1]用于任务的代码: app.post("/",function(req,res){ const crypto = require('crypto'); var input = res.body var str_input=JSON.stringify(input) const hmac = crypto.createHmac('sha256', '<CLIENT SECRET>'); hmac.update(str_input); console.log(hmac.digest('hex')); // print same as below console.log("e034ac7db29c3c0c10dfeced41a6cd850ed74c1c3c620863d47654cc7390359a") })

将PHP hash_hmac(sha512)转换为NodeJS

我正在将php脚本移植到节点上,而对encryption我不太了解。 php脚本使用这个函数: hash_hmac('sha512', text, key); 所以,我需要在Node js中实现一个使用hmac方法(SHA512)返回键控哈希的函数。 从我可以看到,节点具有通过encryption模块( http://nodejs.org/docs/latest/api/crypto.html#crypto_crypto )内置此function – 但我不清楚如何重现此function。 任何帮助,将不胜感激。 谢谢,

我怎样才能解密一个HMAC?

我可以使用以下方法制作HMAC: var encrypt = crypto.createHmac("SHA256", secret).update(string).digest('base64'); 我正试图用这个秘密解密一个编码的HMAC: var decrypt = crypto.createDecipher("SHA256", secret).update(string).final("ascii"); 以下是不成功的。 我如何用密钥解密HMAC? 我得到以下错误: node-crypto : Unknown cipher SHA256 crypto.js:155 return (new Decipher).init(cipher, password); ^ Error: DecipherInit error

(Not-so)聪明钥匙导致Node JS中的SHA512 Hmac问题

这是一个古怪的问题,但我一直在这个工作几个小时,并没有取得很大的进展。 我希望这里有人能够build议… 我正在移植一个脚本从PHP到节点。 php脚本使用这个函数: hash_hmac('sha512', $text, $key); 我已经在使用encryption模块的节点中再现了这一点: var hash = crypto.createHmac( "sha512", key ); hash.update( text ); return hash.digest( "hex" ); 我已经证实,当给定相同的文本和密钥时,这些函数产生相同的散列。 除… 在php中用于键的string看起来类似于:(不要问) define("SITE_KEY", " __ , ,' e`—o (( ( | ___,' \\~——————————-' \_;/ ( / /) ._______________________________. ) (( ( (( ( “-' “-' "); 我试图在Javascript中重现这一点,就像这样: var key = "\ __\ […]