-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from sadam-m/feature/encryption
Feature/encryption
- Loading branch information
Showing
6 changed files
with
164 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.