Skip to content

Commit

Permalink
feat: ✨ new feat to encrypt strings
Browse files Browse the repository at this point in the history
  • Loading branch information
viniieng committed May 8, 2024
1 parent e30d09a commit 3fe4a10
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@aws-sdk/client-ses": "^3.496.0",
"@aws-sdk/lib-dynamodb": "^3.496.0",
"@aws-sdk/s3-request-presigner": "^3.515.0",
"crypto": "^1.0.1",
"json-bigint": "^1.0.0",
"rimraf": "^3.0.2"
},
Expand Down
21 changes: 21 additions & 0 deletions src/string/encrypt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import crypto from 'crypto'

export function encryptPassword (passwordToEncrypt, encryptionKey) {
const salt = crypto.randomBytes(16).toString('hex')
const key = crypto.scryptSync(encryptionKey, salt, 24)
const iv = Buffer.alloc(16, 0)
const cipher = crypto.createCipheriv('aes-192-cbc', key, iv)
let encryptedPassword = cipher.update(passwordToEncrypt, 'utf8', 'hex')
encryptedPassword += cipher.final('hex')
return salt + ':' + encryptedPassword
}

export function decryptPassword (storedPassword, encryptionKey) {
const [salt, encryptedPassword] = storedPassword.split(':')
const key = crypto.scryptSync(encryptionKey, salt, 24)
const iv = Buffer.alloc(16, 0)
const decipher = crypto.createDecipheriv('aes-192-cbc', key, iv)
let decryptedPassword = decipher.update(encryptedPassword, 'hex', 'utf8')
decryptedPassword += decipher.final('utf8')
return decryptedPassword
}

0 comments on commit 3fe4a10

Please sign in to comment.