-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use crypto module for private key encryption (#990)
- Loading branch information
1 parent
03d0c10
commit 9b36e82
Showing
8 changed files
with
65 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,19 @@ | ||
import { createCipheriv, createDecipheriv, randomBytes } from 'crypto' | ||
|
||
const algorithm = 'id-aes256-GCM' | ||
const ivLengthInBytes = 96 | ||
|
||
export const encrypt = (text: string, key: string): string => { | ||
const iv = randomBytes(ivLengthInBytes) | ||
const cipher = createCipheriv(algorithm, Buffer.from(key, 'hex'), iv) | ||
const ciphertext = cipher.update(text, 'utf8', 'hex') + cipher.final('hex') | ||
const authTag = cipher.getAuthTag().toString('hex') | ||
return ciphertext + '|' + authTag + '|' + iv.toString('hex') | ||
} | ||
|
||
export const decrypt = (ciphertextWithAuthTagAndIv: string, key: string): string => { | ||
const [ciphertext, authTagHex, ivHex] = ciphertextWithAuthTagAndIv.split('|') | ||
const decipher = createDecipheriv(algorithm, Buffer.from(key, 'hex'), Buffer.from(ivHex, 'hex')) | ||
decipher.setAuthTag(Buffer.from(authTagHex, 'hex')) | ||
return decipher.update(ciphertext, 'hex').toString('utf8') + decipher.final('utf8') | ||
} |
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,8 @@ | ||
declare module 'crypto' { | ||
interface Cipher { | ||
getAuthTag: () => Buffer | ||
} | ||
interface Decipher { | ||
setAuthTag: (authTag: Buffer) => void | ||
} | ||
} |