Skip to content

fix(amazon-cognito-identity-js): replace crypto-browserify with crypto-js #1773

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

Merged
merged 3 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/amazon-cognito-identity-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"types": "./index.d.ts",
"dependencies": {
"buffer": "4.9.1",
"crypto-browserify": "1.0.9",
"crypto-js": "^3.1.9-1",
"js-cookie": "^2.1.4"
},
"devDependencies": {
Expand Down
32 changes: 20 additions & 12 deletions packages/amazon-cognito-identity-js/src/AuthenticationHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
*/

import { Buffer } from 'buffer/';
import * as crypto from 'crypto-browserify';
const createHash = crypto.createHash;
const createHmac = crypto.createHmac;
const randomBytes = crypto.randomBytes;
import * as CryptoJS from 'crypto-js';

const randomBytes = function(nBytes) {
return Buffer.from((CryptoJS.lib.WordArray.random(nBytes)).toString(), 'hex');
}

import BigInteger from './BigInteger';

Expand Down Expand Up @@ -202,7 +203,9 @@ export default class AuthenticationHelper {
* @private
*/
hash(buf) {
const hashHex = createHash('sha256').update(buf).digest('hex');
const str = buf instanceof Buffer ? CryptoJS.lib.WordArray.create(buf) : buf;
const hashHex = CryptoJS.SHA256(str).toString();

return (new Array(64 - hashHex.length).join('0')) + hashHex;
}

Expand All @@ -224,13 +227,18 @@ export default class AuthenticationHelper {
* @private
*/
computehkdf(ikm, salt) {
const prk = createHmac('sha256', salt).update(ikm).digest();
const infoBitsUpdate = Buffer.concat([
this.infoBits,
Buffer.from(String.fromCharCode(1), 'utf8'),
]);
const hmac = createHmac('sha256', prk).update(infoBitsUpdate).digest();
return hmac.slice(0, 16);
const infoBitsWordArray = CryptoJS.lib.WordArray.create(
Buffer.concat([
this.infoBits,
Buffer.from(String.fromCharCode(1), 'utf8'),
])
);
const ikmWordArray = ikm instanceof Buffer ? CryptoJS.lib.WordArray.create(ikm) : ikm;
const saltWordArray = salt instanceof Buffer ? CryptoJS.lib.WordArray.create(salt) : salt;

const prk = CryptoJS.HmacSHA256(ikmWordArray, saltWordArray);
const hmac = CryptoJS.HmacSHA256(infoBitsWordArray, prk);
return Buffer.from(hmac.toString(), 'hex').slice(0, 16);
}

/**
Expand Down
32 changes: 17 additions & 15 deletions packages/amazon-cognito-identity-js/src/CognitoUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
*/

import { Buffer } from 'buffer/';
//import createHmac from 'create-hmac';
import * as crypto from 'crypto-browserify';
const createHmac = crypto.createHmac;
import * as CryptoJS from 'crypto-js';

import BigInteger from './BigInteger';
import AuthenticationHelper from './AuthenticationHelper';
Expand Down Expand Up @@ -281,14 +279,16 @@ export default class CognitoUser {

const dateNow = dateHelper.getNowString();

const signatureString = createHmac('sha256', hkdf)
.update(Buffer.concat([
const message = CryptoJS.lib.WordArray.create(
Buffer.concat([
Buffer.from(this.pool.getUserPoolId().split('_')[1], 'utf8'),
Buffer.from(this.username, 'utf8'),
Buffer.from(challengeParameters.SECRET_BLOCK, 'base64'),
Buffer.from(dateNow, 'utf8'),
]))
.digest('base64');
])
);
const key = CryptoJS.lib.WordArray.create(hkdf);
const signatureString = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(message, key));

const challengeResponses = {};

Expand Down Expand Up @@ -620,14 +620,16 @@ export default class CognitoUser {

const dateNow = dateHelper.getNowString();

const signatureString = createHmac('sha256', hkdf)
.update(Buffer.concat([
Buffer.from(this.deviceGroupKey, 'utf8'),
Buffer.from(this.deviceKey, 'utf8'),
Buffer.from(challengeParameters.SECRET_BLOCK, 'base64'),
Buffer.from(dateNow, 'utf8'),
]))
.digest('base64');
const message = CryptoJS.lib.WordArray.create(
Buffer.concat([
Buffer.from(this.deviceGroupKey, 'utf8'),
Buffer.from(this.deviceKey, 'utf8'),
Buffer.from(challengeParameters.SECRET_BLOCK, 'base64'),
Buffer.from(dateNow, 'utf8'),
])
);
const key = CryptoJS.lib.WordArray.create(hkdf);
const signatureString = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(message, key));

const challengeResponses = {};

Expand Down