diff --git a/server/domain/user/service/srp/calcSrpA.ts b/server/domain/user/service/srp/calcSrpA.ts new file mode 100644 index 0000000..152627c --- /dev/null +++ b/server/domain/user/service/srp/calcSrpA.ts @@ -0,0 +1,15 @@ +import { Buffer } from 'buffer'; +import crypto from 'crypto'; +import { N, g } from 'domain/user/service/srp/constants'; +import { fromBuffer, toBuffer } from 'domain/user/service/srp/util'; +import { BigInteger } from 'jsbn'; + +export const calculateSrpA = (): { a: Buffer; A: Buffer } => { + let a = Buffer.from([0]); + let AInt = BigInteger.ZERO; + while (AInt === BigInteger.ZERO) { + a = crypto.randomBytes(32); + AInt = g.modPow(fromBuffer(a), N); + } + return { a, A: toBuffer(AInt) }; +}; diff --git a/server/tests/api/changePassword.test.ts b/server/tests/api/changePassword.test.ts index b0e6a29..87d96ec 100644 --- a/server/tests/api/changePassword.test.ts +++ b/server/tests/api/changePassword.test.ts @@ -1,8 +1,7 @@ import assert from 'assert'; -import crypto from 'crypto'; import { calcClientSignature } from 'domain/user/service/srp/calcClientSignature'; -import { N, g } from 'domain/user/service/srp/constants'; -import { fromBuffer, toBuffer } from 'domain/user/service/srp/util'; +import { calculateSrpA } from 'domain/user/service/srp/calcSrpA'; +import { fromBuffer } from 'domain/user/service/srp/util'; import { DEFAULT_USER_POOL_CLIENT_ID } from 'service/envValues'; import { test } from 'vitest'; import { @@ -15,8 +14,7 @@ import { test('changePassword', async () => { await createUserClient(); - const a = crypto.randomBytes(32); - const A = toBuffer(g.modPow(fromBuffer(a), N)); + const { a, A } = calculateSrpA(); const res1 = await noCookieClient.$post({ headers: { 'x-amz-target': 'AWSCognitoIdentityProviderService.InitiateAuth' }, body: { diff --git a/server/tests/api/signIn.test.ts b/server/tests/api/signIn.test.ts index 63f362c..ea5fbee 100644 --- a/server/tests/api/signIn.test.ts +++ b/server/tests/api/signIn.test.ts @@ -1,8 +1,7 @@ import assert from 'assert'; -import crypto from 'crypto'; import { calcClientSignature } from 'domain/user/service/srp/calcClientSignature'; -import { N, g } from 'domain/user/service/srp/constants'; -import { fromBuffer, toBuffer } from 'domain/user/service/srp/util'; +import { calculateSrpA } from 'domain/user/service/srp/calcSrpA'; +import { fromBuffer } from 'domain/user/service/srp/util'; import { DEFAULT_USER_POOL_CLIENT_ID } from 'service/envValues'; import { expect, test } from 'vitest'; import { @@ -16,8 +15,7 @@ import { test('signIn', async () => { await createUserClient(); - const a = crypto.randomBytes(32); - const A = toBuffer(g.modPow(fromBuffer(a), N)); + const { a, A } = calculateSrpA(); const res1 = await noCookieClient.$post({ headers: { 'x-amz-target': 'AWSCognitoIdentityProviderService.InitiateAuth' }, body: {