Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what is the WordArray object and how does it convert to Uint8Array? for migrating to the WebCrypto API? #462

Open
tx0c opened this issue Aug 30, 2023 · 0 comments

Comments

@tx0c
Copy link

tx0c commented Aug 30, 2023

I'm getting a piece of legacy code which was using this crypto-js but new project is using all WebCrypto API,

> CryptoJS.algo.HMAC.create( CryptoJS.algo.SHA256, CryptoJS.enc.Base64.parse(secret) )
    .update(`GET\n/api/v0/ipns\nlimit=1000&ts=1693416815`)
    .finalize()
    .toString(CryptoJS.enc.Base64)
'orkI7k9LisIpA5mwlhqVVlTZg3tf3zthnMZ3HcUABBQ='

this can make correct expected signature code as above,

to convert this HMAC_SHA256 to WebCrypto API, I found this code snippet, I am passing the secret as a string but can't make exact same output, so I see the difference is here crypto-js is converting secret input with CryptoJS.enc.Base64.parse(secret) which gets a WordArray,

Not sure how does this WordArray map to a Uint8Array expected by WebCrypto API?

https://stackoverflow.com/a/67744336

(async ()=>{
'use strict';

let secret = "xxx"; // the secret key
let enc = new TextEncoder("utf-8");
let body = "GET\n/api/v0/ipns\nlimit=1000&ts=1693416815";
let algorithm = { name: "HMAC", hash: "SHA-256" };

let key = await crypto.subtle.importKey("raw", enc.encode(secret), algorithm, false, ["sign", "verify"]);
let signature = await crypto.subtle.sign(algorithm.name, key, enc.encode(body));
let digest = btoa(String.fromCharCode(...new Uint8Array(signature)));

console.log(digest);

})();

Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant