NodeJS HMAC哈希创建

I am trying to follow part of a tutorial for an API written in PHP. They show the following example:

$public = 'JkAFq7M47kLN0xVD';
$private = 'E6X9FyZvMFeJbqtq.IwjlTuR.MKDoicB';

$url = 'https://pterodactyl.local/api/admin/users';
$body = '';

$hmac = hash_hmac('sha256', $url . $body, $private, true);

return $public . '.' . base64_encode($hmac);

// Should return the string below:
//
// JkAFq7M47kLN0xVD.wgIxj+V8RHgIetcQg2lRM0PRSH/y5M21cPz9zVhfFaQ=

But my method doesn't return the proper value. Instead it returns the following:

JkAFq7M47kLN0xVD./RKZS3U2FKfEt7/tEks4vWwyS+89lL+k8aEGO8NJWuo=

Here is my code:

    hmac = crypto.createHmac('sha256', private_key);
    hmac.write(url+body);
    hmac.end();
    hash = hmac.read().toString('base64');
    console.log(hash);

EDIT: I think the example they provided was invalid because as everyone is saying my code is almost identical and the PHP code outputs a different value then it said it should in the documentation.

duanchuang6978
duanchuang6978 是的,我做到了。你有$public和$private关键变量。这表明你认为这是某种公钥密码。
3 年多之前 回复
dpiuqwwyu187975836
dpiuqwwyu187975836 这不是公钥密码,这是一件好事。这只是一个客户端身份验证标头。如果您正确阅读问题,它与密钥无关。
3 年多之前 回复
douyu0725
douyu0725 正如我所说,没有。en.wikipedia.org/wiki/Public-key_cryptography
3 年多之前 回复
douci1677
douci1677 这不是公钥加密的工作原理吗?
3 年多之前 回复
douqing0713
douqing0713 这不是公钥加密的工作方式。HMAC是一个对称组件。
3 年多之前 回复
dongni8969
dongni8969 “应该返回下面的字符串”-嗯,它没有。您的PHP代码和node.js代码目前是等效的。也许你应该检查你的PHP代码是否真正按照你的想法运行。
3 年多之前 回复

2个回答



好吧,我使用这个工具 writephponline 运行这个PHP代码:</ p>

  $ public ='JkAFq7M47kLN0xVD'; 
$ private ='E6X9FyZvMFeJbqtq.IwjlTuR.MKDoicB';
\ n $ url ='https://pterodactyl.local/api/admin/users';
$ body ='';

$ hmac = hash_hmac('sha256',$ url。$ body,$ private, 是的;

echo $ public。 '' 。 base64_encode($ hmac);
</ code> </ pre>

并返回:</ p>

  JkAFq7M47kLN0xVD./RKZS3U2FKfEt7/tEks4vWwyS+89lL+k8aEGO8NJWuo  = 
</ code> </ pre>

所以我用它作为参数,然后我做了一个小的nodejs脚本:</ p>

  var crypto  = require('crypto'); 

var public ='JkAFq7M47kLN0xVD';

var private ='E6X9FyZvMFeJbqtq.IwjlTuR.MKDoicB';

var url ='https://pterodactyl.local/api/admin / users';

var body ='';

var hmac = crypto.createHmac('sha256',private).update(url + body).digest('base64');

console.log (public +'。'+ hmac);
</ code> </ pre>

并返回:</ p>

  JkAFq7M47kLN0xVD./RKZS3U2FKfEt7/tEks4vWwyS  + 89lL + k8aEGO8NJWuo = 
</ code> </ pre>

希望有所帮助。</ p>
</ div>

展开原文

原文

ok so, i used this tool writephponline to run this php code:

$public = 'JkAFq7M47kLN0xVD';
$private = 'E6X9FyZvMFeJbqtq.IwjlTuR.MKDoicB';

$url = 'https://pterodactyl.local/api/admin/users';
$body = '';

$hmac = hash_hmac('sha256', $url . $body, $private, true);

echo $public . '.' . base64_encode($hmac);

and returns:

JkAFq7M47kLN0xVD./RKZS3U2FKfEt7/tEks4vWwyS+89lL+k8aEGO8NJWuo=

So i used that as a parameter, then i made a little nodejs script:

var crypto = require('crypto');

var public = 'JkAFq7M47kLN0xVD';

var private = 'E6X9FyZvMFeJbqtq.IwjlTuR.MKDoicB';

var url = 'https://pterodactyl.local/api/admin/users';

var body = '';

var hmac = crypto.createHmac('sha256', private).update(url+body).digest('base64');

console.log(public + '.' + hmac);

and returns:

JkAFq7M47kLN0xVD./RKZS3U2FKfEt7/tEks4vWwyS+89lL+k8aEGO8NJWuo=

Hope it helps.



我更喜欢使用加密标准的 crypto-js </ code>库。 这个库是保持维护的。</ p>

  const crypto = require('crypto-js')

const public ='JkAFq7M47kLN0xVD'
nconst private ='E6X9FyZvMFeJbqtq.IwjlTuR.MKDoicB '
nbspst url ='https://pterodactyl.local/api/admin/users'
nstst body =''

nstst hmac = crypto.HmacSHA256(url + body,private).toString(crypto.enc.Base64 )

console.log(public +'。'+ hmac)
//打印值:
// JkAFq7M47kLN0xVD./RKZS3U2FKfEt7/tEks4vWwyS+89lL+k8aEGO8NJWuo=


</ DIV>

展开原文

原文

I am prefer use crypto-js library of crypto standards. This library is keep maintaining.

const crypto = require('crypto-js')

const public = 'JkAFq7M47kLN0xVD'
const private = 'E6X9FyZvMFeJbqtq.IwjlTuR.MKDoicB'
const url = 'https://pterodactyl.local/api/admin/users'
const body = ''

const hmac = crypto.HmacSHA256(url + body, private).toString(crypto.enc.Base64)

console.log(public + '.' + hmac)
// Print value:
// JkAFq7M47kLN0xVD./RKZS3U2FKfEt7/tEks4vWwyS+89lL+k8aEGO8NJWuo=

doutuo3935
doutuo3935 我正在使用的是由NodeJS神自己建造和维护的。
3 年多之前 回复
立即提问
相关内容推荐