js怎么实现hmacsha256,在Javascript中实现HMAC-SHA256 for Keybase

本文介绍了如何在JavaScript中使用CryptoJS库实现HMAC-SHA256,特别是在处理Keybase API登录过程中的密钥验证。文章中提到,将UInt8Array转换为CryptoJS的WordArray是解决输入类型不匹配问题的关键,并提供了一个未测试的转换器示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am working with the keybase.io API - trying to drive it from javascript. Logging in is a two step process. The second step is detailed at

https://keybase.io/docs/api/1.0/call/login.

I am stuck on the following;

The server and the client share this secret, and for the client

to successfully log the user in, it must prove knowledge of this

secret to the server. To protect against replay attacks, it does

not send the secret itself. Rather, it treats pwh as MAC key, and

MACs the temporary login_session retrieved in the previous step:

hmac_pwh = HMAC-SHA512(pwh, base64decode(login_session))

Both inputs are in binary format; the pwh key was output in binary

format from scrypt above, and the login_session is base64-decoded

and then fed into HMAC in binary.

I am using the CryptoJS library which gives the following example for implementation

var hash = CryptoJS.HmacSHA256('Message','Secret Passphrase');

I have a couple of problems;

as a matter of terminology does 'MAC key' equal 'Secret Passphrase' and hence the CryptoJS function parameters are reversed in their order vs the code example given on Keybase?

The CryptoJS example has plain ascii inputs whilst the instructions on Keybase are to feed binary inputs. When I try and feed it a uint8array parameter (which is what I get from the previous step in using the keybase API) it keels over as follows;

TypeError: g.clamp is not a function

e,m=4*h;

g.sigBytes>m&&(g=f.finalize(g));

g.clamp();

for(var r=this._oKey=g.clone()

解决方案

CryptoJS.HmacSHA256() happily takes its own WordArray as a key. So you only need to convert your UInt8Array to CryptoJS' WordArray.

This post provides such an (untested) converter created by Vincenzo Ciancia:

CryptoJS.enc.u8array = {

/**

* Converts a word array to a Uint8Array.

*

* @param {WordArray} wordArray The word array.

*

* @return {Uint8Array} The Uint8Array.

*

* @static

*

* @example

*

* var u8arr = CryptoJS.enc.u8array.stringify(wordArray);

*/

stringify: function (wordArray) {

// Shortcuts

var words = wordArray.words;

var sigBytes = wordArray.sigBytes;

// Convert

var u8 = new Uint8Array(sigBytes);

for (var i = 0; i < sigBytes; i++) {

var byte = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;

u8[i]=byte;

}

return u8;

},

/**

* Converts a Uint8Array to a word array.

*

* @param {string} u8Str The Uint8Array.

*

* @return {WordArray} The word array.

*

* @static

*

* @example

*

* var wordArray = CryptoJS.enc.u8array.parse(u8arr);

*/

parse: function (u8arr) {

// Shortcut

var len = u8arr.length;

// Convert

var words = [];

for (var i = 0; i < len; i++) {

words[i >>> 2] |= (u8arr[i] & 0xff) << (24 - (i % 4) * 8);

}

return CryptoJS.lib.WordArray.create(words, len);

}

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值