Tag: hmac

无法从node.js中的PHP中重现sha512 hmac

我有一些PHP代码生成一个hmac,如下所示: <?php $secret = "7pgj8Dm6"; $message = "Test\0Message"; echo base64_encode(hash_hmac('sha512', $message, base64_decode($secret), true))."\n"; echo "69H45OZkKcmR9LOszbajUUPGkGT8IqasGPAWqW/1stGC2Mex2qhIB6aDbuoy7eGfMsaZiU8Y0lO mQxlsWNPrw==\n"; ?> 当我尝试在node.js中生成类似的代码时,我得到了一个不同于我想要的base64编码结果,而我不知道为什么。 var hmac = function(msg, secret){ var s = (new Buffer(secret, 'base64')).toString('utf8'); var hmac = require('crypto').createHmac('sha512',s); hmac.update(msg); return hmac.digest('base64'); }; var secret = "7pgj8Dm6"; var message = "Test\0Message"; var wanted = "69H45OZkKcmR9LOszbajUUPGkGT8IqasGPAWqW/1stGC2Mex2qhIB6aDbuoy7eGfMsaZiU8Y0lO3mQxlsWNPrw=="; var got = hmac(message, secret); if(wanted […]

使用API​​无法获得HMAC身份validation

我试图使用HMAC与LocalBitcoins API进行身份validation。 这是用Python编写的authentication: message = str(nonce) + hmac_auth_key + relative_path + get_or_post_params_urlencoded signature = hmac.new(hmac_auth_secret, msg=message, digestmod=hashlib.sha256).hexdigest().upper() 以及创buildHMAC消息的参数: Nonce. A 63 bit positive integer, for example unix timestamp as milliseconds. HMAC authentication key. This is the first one of a key/secret pair. Relative path, for example /api/wallet/. GET or POST parameters in their URL encoded […]

即使req.body不为空,也不会调用POST中的Node.js – req.on(“data”)

我正在做以下事情: …… …. app.use(cookieParser()); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use('/public', express.static(__dirname + '/public')); app.post('/uninstalled', function (req, res, next) { var bodyAsQueryString = queryString.stringify(req.body); console.log('bodyAsQueryString = ' + bodyAsQueryString); var hmacReceived = req.headers['x-shopify-hmac-sha256']; var calculatedHmac = crypto.createHmac('SHA256', config.app.secret); req.on("data", function(data) { console.log('on data…'); calculatedHmac.update(data); }); req.on("end", function() { console.log('on end…'); calculatedHmac = calculatedHmac.digest("base64"); console.log('hmacReceived = ' […]

PHP – 计算正确的HMAC签名为nodejs脚本

所以我正在处理一个PHP脚本,查询使用HMAC身份validation标头的API。 不过,我一直在试图正确编码HMAC签名。 我有一个预先存在的nodejs脚本作为模板工作。 在nodejs脚本中,使用以下方法计算HMAC签名: var crypto = require('crypto'); var hmac = []; hmac.secret = 'ODc0YTM3YzUxODFlMWQ1YTdhMGQwY2NiZmE1N2Y1ODdjYzM5NTgyMDJhZjVkYTE4MmQxYzQ5ODk0M2QzNWQxYw=='; hmac.timestamp = 1457326475000; hmac.path = '/account/'; hmac.message = hmac.path +'\n' + hmac.timestamp; var sig = crypto.createHmac('sha512', new Buffer(hmac.secret, 'base64')); hmac.signature = sig.update(hmac.message).digest('base64'); console.log(hmac); 这正确地计算HMAC签名为:bWjIFFtFmWnj0 + xHLW2uWVa6M6DpbIV81uyUWwRFCJUg + 0Xyt40QWZWQjGvfPUB / JbjGZHUoso0Qv5JHMYEv3A ==。 同时,在PHP中,我正在使用: <?php $hmac['secret'] = 'ODc0YTM3YzUxODFlMWQ1YTdhMGQwY2NiZmE1N2Y1ODdjYzM5NTgyMDJhZjVkYTE4MmQxYzQ5ODk0M2QzNWQxYw=='; $hmac['nonce'] = '1457326475000'; $hmac['path'] […]

如果秘密作为文字variables传递,nodejs crypto hmac会产生不同的散列

我试图从它的encryption库中使用nodejs的createHmac函数。 问题:当给出(看似)相同的参数时,会产生不同的哈希值。 唯一的区别是“秘密”参数是stringvariables还是string文字。 以下SPA隔离这个问题。 我正在使用nwjs(node webkit)SDK flavor v 0.14.2在OS X El Cap上运行此代码。 任何帮助和build议感激地收到。 的index.html <!DOCTYPE html> <html> <head> <title>Context Menu</title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> </head> <body style="width: 100%; height: 100%;"> <div id="wrapper"> </div> <script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> <script type="text/javascript" src="./index.js"></script> </body> </html> index.js var nodeCrypto = require('crypto'); var payload = […]

Uber webhook签名问题nodeJs(hmac)

我有一些NodeJS的问题和Uber Sandbox的WebHooktesting。 我能够收到正确的POST响应,但是安全检查(X-Uber-Signature)总是错误的。 module.exports = { myWebService: function(req, res) { const hmac = crypto.createHmac('sha256','<MYSECRET>'); var hash = hmac.update(JSON.stringify(req.body)).digest('hex'); //Those values are always different.. console.log("Constructed hash : " + hash +"\n"); console.log("Received hash : " + req.header('X-Uber-Signature') + "\n"); JSON.stringify(req.body)内容: {"event_id":"08db06df-559a-457a-ba92-3c8380bb7ec7","resource_href":"https://sandbox-api.uber.com/v1/requests/8f5cc257-cfdf-4654-9acd-085aae740107","meta":{"status":"arriving","rider_id":"8IMl8ulC-yJTqkbsq5g4HuyuYeRQ5b5aSsmLt2vpjl6H8Fk_JPz_5AZYj4ERi6M7MagmrJtPM7L_rAnHLgO0qLgGtpD8Lg32rnGTEUkWHAbPjDZIl0-X91PrrujPY_IYGA==","user_id":"ecb2e871-b768-4f76-bfa9-8bc253bced0e","resource_id":"8f5cc257-cfdf-4654-9acd-085aae740107"},"event_type":"all_trips.status_changed","event_time":1508163555} 我尝试了许多(很多)这个string转换,但没有任何工作,如果任何人有一个想法,将不胜感激..谢谢

如何在javascript中创buildHMAC

我绑定在nodejs中使用java函数作为参考实现hmac函数。 这是我需要模拟的Java函数: private static String printMacAsBase64(byte[] macKey, String counter) throws Exception { // import AES 128 MAC_KEY SecretKeySpec signingKey = new SecretKeySpec(macKey, "AES"); // create new HMAC object with SHA-256 as the hashing algorithm Mac mac = Mac.getInstance("HmacSHA256"); mac.init(signingKey); // integer -> string -> bytes -> encrypted bytes byte[] counterMac = mac.doFinal(counter.getBytes("UTF-8")); // base 64 […]

NodeJS crypto.createHmac SHA256无法正常工作

我目前正在试图实现我们正在使用的库的authentication部分,但是我偶然发现了一个奇怪的数据签名问题,NodeJS中crypto.createHmac的输出大约是hash_hmac的一半在PHP中,这是PHP和NodeJS之间数据的唯一部分(我们需要在这里使用NodeJS) 用于在NodeJS中创build签名的确切代码是, authorization["oauth_signature"] = crypto.createHmac('SHA256', process.env.SECRET).update(toSign).digest('base64'); 而对于PHP来说 $authorization["oauth_signature"] = base64_encode(hash_hmac("SHA256", $signatureString . $signingKey, $secret)); 然而,NodeJS版本的输出是 7LkQP+qKR1gSdPq/AgH/3No3ps7EtZXnqwjivMixvM8= 而对于PHP来说 NmQ0MWIzYmJiMjI2YzFlMDhiYzY3NzVmMWY0MzEwNDlhNDU3NDI0ZGJlMzU3NjJhYmMwYjgwYzZjMDE4NDM4OA== 其中有两倍以上的数据 是否必须为NodeJS版本使用不同的库,而不是一个版本? 我们在Microsoft Azure上托pipe我们的NodeJS后端,不知道这是否相关,但似乎至less有效。 编辑: 我发现这个问题,在PHP中的hash_hmac自动导出它的数据为hex数据,crypto.createHmac导出它的数据作为原始的二进制数据,我直接转换成base64,我需要做的是首先导出数据到hex,然后转换那base64。

去朗生成hmac

我是新来的语言,我需要用go语言创build一个Hmac。 我在nodejs中创build了一个Hmac,需要用go语言生成相同的Hamc。 尝试下面的代码,但得到完全不同的输出。 我不知道我做错了什么。 这是我的尝试 package main import ( "crypto/hmac" "crypto/sha256" "encoding/base64" "fmt" ) func ComputeHmac256(message string, secret string) string { key := []byte(secret) h := hmac.New(sha256.New, key) h.Write([]byte(message)) return base64.StdEncoding.EncodeToString(h.Sum(nil)) } func main() { fmt.Println(ComputeHmac256("sms1", "b5fb5b3a65b8429693c3a029308e2e46")) } Output: JVN7kUPFL0aQ09lIH4YOsFJA3A2faqTuu6zIaYo61VI= 需要去下面的nodejs代码 var crypto = require('crypto'), text = 'sms1', key = 'b5fb5b3a65b8429693c3a029308e2e46' var hash = crypto.createHmac('sha256', […]

NodeJS返回来自HMAC的其他二进制结果,而不是php

我正在windows上运行node.js和php,并在node.js中使用包含的encryption模块。 PHP脚本: hash_hmac("sha256", "foo", "bar", true) // the true enables binary output 输出: ¶y3!è¬╝♂ï►ó│Ñ├Fä╚┘CA╝±G6▄rp¸t↑Q Node.js脚本: crypto.createHmac("sha256", "bar").update("foo").digest("binary"); 输出: ¶y3!?ª¼♂?►¢³¥AF?ÈÙCA¼ñG6Ürp+ T↑Q 我也想知道为什么一些数字是相同的,但其他一些没有。 我也尝试得到hex而不是二进制结果,他们都输出相同 。 hash_hmac("sha256", "foo", "bar", false); // false outputs hex data crypto.createHmac("sha256", "bar").update("foo").digest("hex"); // notice "hex" 这不是一个解决scheme,因为我无法将hex数据转换为二进制: var hmac = crypto.createHmac("sha256", "bar").update("foo").digest("hex"); var binary = new Buffer(hmac, "hex"); 可变的binary输出: ¶y3!???♂?►???? F ??? ?? CA […]