Skip to content

Commit

Permalink
Added tests for the cipher service
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Jan 21, 2024
1 parent 461d09d commit a890424
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- run: npm run test
16 changes: 5 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions src/cipher-service.test.ts

This file was deleted.

35 changes: 22 additions & 13 deletions src/cipher-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions tests/cipher-service.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
10 changes: 5 additions & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit a890424

Please sign in to comment.