Skip to content

Commit

Permalink
refactor: add optional checktype parameter to hash function
Browse files Browse the repository at this point in the history
  • Loading branch information
ehrdi committed Aug 6, 2024
1 parent 30eb90a commit a455859
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/lib/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const createHash = async (
request.storecarddata,
];

if (request.checktype) {
requestValues.splice(2, 0, request.checktype);
}

const dataToHash = requestValues.join('');

const encoder = new TextEncoder();
Expand Down
43 changes: 28 additions & 15 deletions src/tests/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,41 @@ describe('crypto', () => {
});
});
describe('createHash', () => {
it('should create a hash', async () => {
const requestWithoutHash: Omit<Request, 'hash'> = {
request: 'creditcardcheck',
responsetype: 'JSON',
mode: 'test',
mid: '11111',
aid: '22222',
portalid: '3333333',
encoding: 'UTF-8',
storecarddata: 'yes',
api_version: '3.11',
};
const pmiPortalKey = 'wurstbrot';

const requestWithoutHash: Omit<Request, 'hash'> = {
request: 'creditcardcheck',
responsetype: 'JSON',
mode: 'test',
mid: '11111',
aid: '22222',
portalid: '3333333',
encoding: 'UTF-8',
storecarddata: 'yes',
api_version: '3.11',
};
const pmiPortalKey = 'secretTestKey';
it('should create a correct hash', async () => {
const expectedResultForAboveValues =
'ba5c08f0a3ee380214908e1274411227054923d129109e6f4b4460935a64918e5871842655ea8c02e54fcafa9c029bc0';
'72a4a5acd535aa5eb87bee93f288b8016fb44134767a36d323dafe2105f491bc555e47d7e2cdc1ee6f291f0999e30d68';

const hash = await createHash(requestWithoutHash, pmiPortalKey);

expect(hash).toBeTruthy();
expect(hash).toEqual(expectedResultForAboveValues);
});

it('should create a correct hash with checktype', async () => {
const requestWithChecktype: Omit<Request, 'hash'> = {
...requestWithoutHash,
checktype: 'TC',
};
const expectedResultForAboveValues =
'cef6c7de0867328e46adc13da9435a9a37825b9822452fc745b745154a2129d775a820deffcaae022b7259f17aafd36e';

const hash = await createHash(requestWithChecktype, pmiPortalKey);

expect(hash).toBeTruthy();
expect(hash).toEqual(expectedResultForAboveValues);
});
});

describe('encodeToBase64', () => {
Expand Down

0 comments on commit a455859

Please sign in to comment.