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

CryptoJS equivalent #220

Closed
whittlem opened this issue May 24, 2019 · 6 comments
Closed

CryptoJS equivalent #220

whittlem opened this issue May 24, 2019 · 6 comments

Comments

@whittlem
Copy link

whittlem commented May 24, 2019

I'm trying to get CryptoJS to do the same as this from the "crypto" library...

const what = timestamp + method.toUpperCase() + path + body;
const key = Buffer.from(auth.secret, 'base64');
const hmac = crypto.createHmac('sha256', key);
const signature = hmac.update(what).digest('base64');

This is what I have...

const what = `${timestamp}${method}${path}${body}`;
const hmac = CryptoJS.HmacSHA512(what, secret).toString();
const wordArray = CryptoJS.enc.Utf8.parse(signed);
const signature = CryptoJS.enc.Base64.stringify(wordArray);

I'm sure it is something small but I can't seem to get it working.

Is someone able to help?

@whittlem
Copy link
Author

I have tried this now also with no luck...

const what = `${timestamp}${method}${path}${body}`;
const key = Buffer.from(secret, 'base64').toString('ascii');
const hmac = CryptoJS.HmacSHA512(what, secret64).toString();
const signature = Buffer.from(hmac).toString('base64');

@whittlem
Copy link
Author

whittlem commented May 25, 2019

I think the problem is this:

const signature = hmac.update(what).digest('base64');

How would I do this in crypto-js?

I thought maybe this would do it but it seems not...

const hmac = CryptoJS.HmacSHA512(what, secret64).toString();
const signature = Buffer.from(hmac).toString('base64');

@whittlem
Copy link
Author

I found another example where they did this...

var timestamp = Math.floor(Date.now() / 1000);
var message = timestamp + method + '/v2/' + path + bodyStr;
var signature = crypto.createHmac('sha256', this.apiSecret).update(message).digest('hex');

Does that mean the result is hex encoded?

@geastwood
Copy link

Hey @whittlem
I tried to reproduce what you have posted. By trying to replace
const hmac = CryptoJS.HmacSHA512(what, secret).toString();
to
const hmac = CryptoJS.HmacSHA512(what, secret).toString(CryptoJS.enc.Base64)
I managed to generate the same result.

@whittlem
Copy link
Author

Thanks very much. I will try that.

@Bardiamist
Copy link

@whittlem @geastwood Hey, maybe you know how to implement equivalent code if update calls twice?

const crypto = require('crypto');
const hmac = crypto.createHmac('sha512', 'key');
const str = hmac.update('what').digest('hex');
console.log(str);

Equivalent:

import hmacSHA512 from 'crypto-js/hmac-sha512';
const hmac = hmacSHA512('what', 'key');
const str = hmac.toString();
console.log(str)

But how to implemnet hmac.update('what').update('what2')?

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

3 participants