Skip to content

Commit

Permalink
Merge pull request #76 from sadam-m/feature/encryption
Browse files Browse the repository at this point in the history
Feature/encryption
  • Loading branch information
SkidGod4444 authored Dec 1, 2024
2 parents 4b3c968 + c262cc2 commit c828219
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 16 deletions.
2 changes: 2 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"@repo/db": "workspace:*",
"@repo/mail": "workspace:*",
"@repo/types": "workspace:*",
"@repo/cache": "workspace:*",
"@repo/crypt":"workspace:*",
"hono": "^4.6.9",
"next": "15.0.3",
"prisma": "^5.22.0",
Expand Down
1 change: 1 addition & 0 deletions packages/crypt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
17 changes: 17 additions & 0 deletions packages/crypt/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@repo/crypt",
"scripts": {
"dev": "tsc --watch",
"build": "tsc"
},
"exports": {
".": "./src/crypt.ts"
},
"devDependencies": {
"@repo/typescript-config": "workspace:*",
"typescript": "5.7.2"
},
"dependencies": {
"crypto": "^1.0.1"
}
}
90 changes: 90 additions & 0 deletions packages/crypt/src/crypt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import * as crypto from "crypto";

import { Buffer } from "buffer";


interface EncryptedData {

initialVector:string,
encrypted:string,
authTag:string
}


export function encrypt(text:string,key:Buffer):EncryptedData{

if(key.length!==32){

throw new Error('Key must be 32 bytes long for AES-256-GCM encryption')
}

try {

const initialVector=crypto.randomBytes(12);

const cipher=crypto.createCipheriv('aes-256-gcm',Buffer.from(key),initialVector);

let encrypted=cipher.update(text,'utf8','hex');

encrypted += cipher.final('hex');

const authTag = cipher.getAuthTag();

return {

initialVector:initialVector.toString('hex'),

encrypted:encrypted,

authTag:authTag.toString('hex')
};
}
catch(error:unknown){

if(error instanceof Error){
throw new Error('Encryption failed: '+error.message);
}
else{
throw new Error('Encryption failed: unknown error')
}
}
}


export function decrypt(encryptedData:EncryptedData,key:Buffer):string{

if(key.length!==32){

throw new Error('Key must be 32 bytes long for AES-256-GCM decryption')
}

try {
const initialVector=Buffer.from(encryptedData.initialVector,'hex');

const encrypted=encryptedData.encrypted;

const authTag=Buffer.from(encryptedData.authTag,'hex');


const decipher=crypto.createDecipheriv('aes-256-gcm',key,initialVector);

decipher.setAuthTag(authTag);

let decrypted=decipher.update(encrypted,'hex','utf8');

decrypted+=decipher.final('utf8');


return decrypted;

}
catch(error:unknown){

if(error instanceof Error){
throw new Error('Decryption failed: '+error.message);
}
else{
throw new Error('Decryption failed: unknown error')
}
}
}
9 changes: 9 additions & 0 deletions packages/crypt/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@repo/typescript-config/base.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
61 changes: 45 additions & 16 deletions pnpm-lock.yaml

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

0 comments on commit c828219

Please sign in to comment.