diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 463beb8..8cbc82d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -19,4 +19,4 @@ jobs: with: node-version: ${{ matrix.node-version }} - run: npm install - - run: npm test + - run: npm run test diff --git a/package.json b/package.json index f241fe6..95446be 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "node": ">=16" }, "scripts": { - "test": "xo --env=node && ava && pnpm run build && tsd --typings dist/index.d.ts", + "test": "xo --env=node && pnpm run build && mocha -r ts-node/register 'tests/**/*.js'", "build": "del-cli dist && tsc", "prepack": "pnpm run build" }, @@ -31,25 +31,19 @@ ], "devDependencies": { "@sindresorhus/tsconfig": "^5.0.0", - "ava": "^5.3.0", + "@types/chai": "^4.3.11", + "@types/mocha": "^10.0.6", + "chai": "^5.0.0", "del-cli": "^5.1.0", + "mocha": "^10.2.0", "ts-node": "^10.9.2", "tsd": "^0.30.4", - "tsup": "^8.0.1", "xo": "^0.54.2" }, "dependencies": { "@types/node": "^20.11.5", "axios": "^1.6.5" }, - "ava": { - "extensions": { - "ts": "module" - }, - "nodeArguments": [ - "--loader=tsx" - ] - }, "prettier": { "trailingComma": "all", "tabWidth": 4, diff --git a/readme.md b/readme.md index f752074..03c6449 100644 --- a/readme.md +++ b/readme.md @@ -79,7 +79,7 @@ log.Println(message.Code) // 202 You can run the unit tests for this client from the root directory using the command below: ```bash -go test -v +go tests -v ``` ## License diff --git a/src/cipher-service.test.ts b/src/cipher-service.test.ts deleted file mode 100644 index 9a78899..0000000 --- a/src/cipher-service.test.ts +++ /dev/null @@ -1,5 +0,0 @@ -import test from 'ava'; - -test('cipherService', t => { - t.pass(); -}); diff --git a/src/cipher-service.ts b/src/cipher-service.ts index 20f944a..a5e3e6b 100644 --- a/src/cipher-service.ts +++ b/src/cipher-service.ts @@ -9,22 +9,31 @@ import {Buffer} from 'node:buffer'; class CipherService { public encrypt(key: string, message: string): string { const iv = randomBytes(16); - const cipher = createCipheriv('aes-256-cfb', this.hash(key), iv); - return Buffer.from( - Buffer.concat([iv, cipher.update(message, 'utf8'), cipher.final()]), - ).toString('base64'); + const cipher = createCipheriv( + 'aes-256-cfb', + this.hash(key), + iv, + ).setAutoPadding(false); + return Buffer.concat([ + iv, + cipher.update(message, 'utf8'), + cipher.final(), + ]).toString('base64'); } public decrypt(key: string, message: string): string { - const iv = randomBytes(16); - const decipher = createDecipheriv('aes-256-cfb', this.hash(key), iv); - return Buffer.from( - Buffer.concat([ - iv, - decipher.update(message, 'utf8'), - decipher.final(), - ]), - ).toString('utf8'); + const cipherBytes = Buffer.from(message, 'base64'); + const iv = cipherBytes.subarray(0, 16); + const decipher = createDecipheriv( + 'aes-256-cfb', + this.hash(key), + iv, + ).setAutoPadding(false); + + return Buffer.concat([ + decipher.update(cipherBytes.subarray(16, cipherBytes.length)), + decipher.final(), + ]).toString(); } private hash(value: string): Buffer { diff --git a/tests/cipher-service.spec.js b/tests/cipher-service.spec.js new file mode 100644 index 0000000..32c910a --- /dev/null +++ b/tests/cipher-service.spec.js @@ -0,0 +1,19 @@ +import {expect} from 'chai'; +import {describe, it} from 'mocha'; +import CipherService from '../dist/src/cipher-service.js'; + +describe('CipherService', () => { + it('can encrypt and decrypt the same content', () => { + // Arrange + const key = 'Password123'; + const message = 'This is a test text message1'; + const service = new CipherService(); + + // Act + const cipherText = service.encrypt(key, message); + const plainText = service.decrypt(key, cipherText); + + // Assert + expect(plainText).to.equal(message); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index b8dfe5b..1f1a3a7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,12 +2,12 @@ "extends": "@sindresorhus/tsconfig", "compilerOptions": { "outDir": "dist", - "experimentalDecorators": true + "experimentalDecorators": true, + "esModuleInterop": true }, - "files": [ - "index.ts" - ], + "files": ["index.ts"], "ts-node": { - "transpileOnly": true + "transpileOnly": true, + "esm": true } }