Skip to content

Commit

Permalink
tidy up utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mustofa-id committed Oct 25, 2023
1 parent c39f1df commit bf03fde
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/rekam-medis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ export class RekamMedis extends BaseApi<'rekamMedis'> {
* dan KODE PPK. Ini berdasarkan spesifikasi yang telah ditentukan
* pada halaman TrustMark BPJS Kesehatan.
*/
async function preprocess(data: unknown, config: Config): Promise<string> {
async function preprocess<T>(data: Bundle<T>, config: Config): Promise<string> {
try {
const value = JSON.stringify(data);
const compressed = await gzip(value);
return encrypt(compressed.toString('base64'), config);
return encrypt(compressed.toString('base64'), [
config.consId,
config.consSecret,
config.ppkCode
]);
} catch (err) {
// TODO: define custom error
throw new Error(`failed to compress or encrypt data. ${err}`);
Expand Down
10 changes: 4 additions & 6 deletions src/rekam-medis/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import zlib from 'node:zlib';
import { Config } from '../fetcher.js';
import { createCipheriv, createHash } from 'node:crypto';
import zlib from 'node:zlib';

export async function gzip(value: string): Promise<Buffer> {
return new Promise((resolve, reject) => {
Expand All @@ -11,13 +10,12 @@ export async function gzip(value: string): Promise<Buffer> {
});
}

export function encrypt(value: string, config: Config): string {
const { consId, consSecret, ppkCode } = config;
if (!consId || !consSecret || !ppkCode) {
export function encrypt(value: string, keyCombinations: string[]): string {
if (keyCombinations.some((k) => !k)) {
throw new Error(`consId, consSecret, or ppkCode are not set`);
}

const keyPlain = consId + consSecret + ppkCode;
const keyPlain = keyCombinations.join('');
const key = createHash('sha256').update(keyPlain, 'utf8').digest();
const iv = Uint8Array.from(key.subarray(0, 16));
const cipher = createCipheriv('aes-256-cbc', key, iv);
Expand Down
12 changes: 3 additions & 9 deletions test/rekam-medis/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it } from 'vitest';
import { unzipSync } from 'zlib';
import { Config } from '../../src/fetcher';
import { encrypt, gzip } from '../../src/rekam-medis/utils';

describe('utils', () => {
Expand All @@ -14,14 +13,9 @@ describe('utils', () => {
});

it.concurrent('decrypt()', async () => {
const config = {
consId: '12345',
consSecret: '53cREt',
ppkCode: '54321'
};

const result1 = encrypt('data1', <Config>config);
const result2 = encrypt('data2', <Config>config);
const keys = ['a', 'b', 'c'];
const result1 = encrypt('data1', keys);
const result2 = encrypt('data2', keys);

expect(result1.length % 4).toBe(0);
expect(result2.length % 4).toBe(0);
Expand Down

0 comments on commit bf03fde

Please sign in to comment.