nacl-blob
Encrypt and decrypt DOM blobs using nacl-stream within service workers. A port of nacl-stream-js
's demo into a consumable module.
Usage
const naclBlob = const nacl = const key = naclboxsecretKeyconst nonce = nacl // Any kind of data inside of a blobconst arr = 10 * 1024 * 1024 + 111for let i = 0; i < arrlength; i++ arri = i & 0xffconst blob = arr const encryptor = naclBlob encryptor { const r1 = r1 { const r2 = r2 { if r1result !== r2result console else console } r2 } r1}
API
encrypt = require('nacl-blob').encrypt
Import the encrypt
function.
decrypt = require('nacl-blob').decrypt
Import the decrypt
function.
⚠️ IMPORTANT ⚠️
This module uses a build-time browserify transform called workerify
. If you are not using browserify, you can import from the transformed version of the module by importing from the nacl-blob/dist
path. e.g:
const encrypt = encryptconst decrypt = decrypt
(Wanted, a workerify transform that writes out the contents to a separate bundle)
encrypt(key, nonce, blob, [opts], cb)
Encrypt a File or Blob, using a key
and nonce
. Returns an event emitter that can be used to display encryption progress. The encrypted data will be returned in the callback as a Blob.
The key
must be a 32-byte Uint8Array or Node.js Buffer (see github.com/dchest/tweetnacl-js#usage for details).
The nonce
must be a 16-byte Uint8Array or Node.js Buffer.
The blob
must be a Blob or File.
Optional opts
include:
chunkSize: 1024 * 1024 mimeType: blobtype
The cb
function will fire when the file/blob has been encrypted and have the the following arguments:
err
: Any error that occurred durning encryption. You should handle this.encryptedBlob
: a Blob containing the encrypted data. This can be securely stored/transmitted along with thenonce
across insecure networks and decrypted with thekey
(assuming secure key exchange is performed elsewhere).
Returns an Event Emitter that you can use to listen for the following events:
progress
: An event that emits the the progress of encryption in the following shape:
progress // in bytes length // total bytes
decrypt(key, nonce, encryptedBlob, [opts], [cb])
Decrypt a Blob that was encrypted using a key
and nonce
. Returns an event emitter that can be used to track progress. The decrypted data will be returned in the callback as a Blob.
The key
must be the same 32-byte Uint8Array or Node.js Buffer used to encrypt the file. (see github.com/dchest/tweetnacl-js#usage)
The nonce
must be the same 16-byte Uint8Array or Node.js Buffer used to encrypt the file.
The encryptedBlob
must be an encrypted Blob or File.
Optional opts
include:
chunkSize: 1024 * 1024 mimeType: encryptedBlobtype
The cb
function will fire when the file/blob has been decrypted and have the the following arguments:
err
: Any error that occurred durning encryption. You should handle this.decryptedBlob
: a Blob containing the decrypted data.
Returns an Event Emitter that you can use to listen for the following events:
progress
: An event that emits the the progress of encryption in the following shape:
progress // in bytes length // total bytes
See Also
- https://github.com/jedisct1/libsodium.js
- https://github.com/jedisct1/libsodium/issues/475
- https://developer.mozilla.org/en-US/docs/Web/API/Body/blob