This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
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 #727 from 3box/release/v1.17.0
release v1.17.0
- Loading branch information
Showing
14 changed files
with
1,017 additions
and
273 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const nacl = require('tweetnacl') | ||
nacl.util = require('tweetnacl-util') | ||
|
||
const randomNonce = () => { | ||
return nacl.randomBytes(24) | ||
} | ||
|
||
const symEncryptBase = (msg, symKey, nonce) => { | ||
nonce = nonce || randomNonce() | ||
if (typeof msg === 'string') { | ||
msg = nacl.util.decodeUTF8(msg) | ||
} | ||
const ciphertext = nacl.secretbox(msg, nonce, symKey) | ||
return { | ||
nonce: nacl.util.encodeBase64(nonce), | ||
ciphertext: nacl.util.encodeBase64(ciphertext) | ||
} | ||
} | ||
|
||
const symDecryptBase = (ciphertext, symKey, nonce, toBuffer) => { | ||
ciphertext = nacl.util.decodeBase64(ciphertext) | ||
nonce = nacl.util.decodeBase64(nonce) | ||
const cleartext = nacl.secretbox.open(ciphertext, nonce, symKey) | ||
if (toBuffer) { | ||
return cleartext ? Buffer.from(cleartext) : null | ||
} | ||
return cleartext ? nacl.util.encodeUTF8(cleartext) : null | ||
} | ||
|
||
const newSymKey = () => { | ||
return nacl.randomBytes(32) | ||
} | ||
|
||
|
||
module.exports = { randomNonce, symEncryptBase, symDecryptBase, newSymKey } |
Oops, something went wrong.