-
Notifications
You must be signed in to change notification settings - Fork 4
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 #9 from checkr/encryption-upgrade
Use encryption library and fix tests
- Loading branch information
Showing
14 changed files
with
167 additions
and
139 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
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,32 @@ | ||
import sodium from 'libsodium-wrappers' | ||
|
||
const key = process.env.ENCRYPTION_SECRET_KEY | ||
|
||
const encrypt = async plaintext => { | ||
await sodium.ready | ||
const nonce = Buffer.from(sodium.randombytes_buf(24)) | ||
const ciphertext = Buffer.from( | ||
sodium.crypto_secretbox_easy(plaintext, nonce, Buffer.from(key, 'hex')), | ||
) | ||
return {ciphertext: ciphertext.toString('hex'), nonce: nonce.toString('hex')} | ||
} | ||
|
||
const decrypt = async ({ciphertext, nonce}) => { | ||
await sodium.ready | ||
let decrypted = Buffer.from( | ||
sodium.crypto_secretbox_open_easy( | ||
Buffer.from(ciphertext, 'hex'), | ||
Buffer.from(nonce, 'hex'), | ||
Buffer.from(key, 'hex'), | ||
), | ||
) | ||
return sodium.to_string(decrypted) | ||
} | ||
|
||
const keygen = async () => { | ||
await sodium.ready | ||
const key = Buffer.from(sodium.crypto_secretbox_keygen()) | ||
return key.toString('hex') | ||
} | ||
|
||
export {encrypt, decrypt, keygen} |
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 was deleted.
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
Oops, something went wrong.