Skip to content

Commit

Permalink
Encrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
itaibo committed Jan 22, 2024
1 parent b8ed7be commit 5af2665
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENCRYPTION_KEY=b5df1f760870a9a150bd25bbe665e0753f481210e4d90eeeacff075e6b51d6c6
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
setupFiles: ["<rootDir>/tests/setup.ts"],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test": "jest --passWithNoTests"
},
"dependencies": {
"cryptr": "^6.3.0",
"next": "14.1.0",
"react": "^18",
"react-dom": "^18",
Expand All @@ -21,6 +22,7 @@
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.0.1",
"dotenv": "^16.3.2",
"eslint": "^8",
"eslint-config-next": "14.1.0",
"jest": "^29.7.0",
Expand Down
10 changes: 10 additions & 0 deletions src/lib/encryption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Cryptr from 'cryptr';
const cryptr = new Cryptr(process.env.ENCRYPTION_KEY || '');

export const encrypt = (string: string): string => {
return cryptr.encrypt(string);
};

export const decrypt = (encryptedString: string): string => {
return cryptr.decrypt(encryptedString);
};
17 changes: 17 additions & 0 deletions tests/lib/encryption.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { encrypt, decrypt } from '@/lib/encryption';

const strings: {plain: string, encrypted: string | null} = {
plain: 'hey',
encrypted: null,
};

describe('Encryption module', () => {
it('Must encrypt string', () => {
strings.encrypted = encrypt(strings.plain);
expect(strings.encrypted).not.toBe(strings.plain);
});

it('Must decrypt string', () => {
expect(decrypt(strings.encrypted as string)).toBe(strings.plain);
});
});
3 changes: 3 additions & 0 deletions tests/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dotenv from 'dotenv';

dotenv.config({ path: '.env.example' });

0 comments on commit 5af2665

Please sign in to comment.