{"id":15497499,"url":"https://github.com/bcomnes/nacl-blob","last_synced_at":"2025-10-24T00:35:14.744Z","repository":{"id":48119749,"uuid":"132396617","full_name":"bcomnes/nacl-blob","owner":"bcomnes","description":"🔐Encrypt DOM API blobs using nacl-stream-js","archived":false,"fork":false,"pushed_at":"2020-05-25T04:08:22.000Z","size":9592,"stargazers_count":5,"open_issues_count":2,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-26T13:21:28.460Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://nacl-blob.netlify.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bcomnes.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-07T02:30:09.000Z","updated_at":"2022-09-07T07:10:35.000Z","dependencies_parsed_at":"2022-08-12T19:00:55.311Z","dependency_job_id":null,"html_url":"https://github.com/bcomnes/nacl-blob","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/bcomnes/nacl-blob","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Fnacl-blob","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Fnacl-blob/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Fnacl-blob/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Fnacl-blob/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bcomnes","download_url":"https://codeload.github.com/bcomnes/nacl-blob/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcomnes%2Fnacl-blob/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262098732,"owners_count":23258642,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T08:38:53.404Z","updated_at":"2025-10-24T00:35:14.681Z","avatar_url":"https://github.com/bcomnes.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nacl-blob [![stability][0]][1]\n[![npm version][2]][3] [![build status][4]][5] [![windows status][12]][13]\n[![downloads][8]][9] [![js-standard-style][10]][11]\n\nEncrypt and decrypt DOM blobs using [nacl-stream](https://github.com/dchest/nacl-stream-js) within service workers.  A port of `nacl-stream-js`'s demo into a consumable module.\n\n- [🌎 Example site](https://nacl-blob.netlify.com)\n\n## Usage\n\n```js\nconst naclBlob = require('nacl-blob')\nconst nacl = require('tweetnacl')\n\nconst key = nacl.box.keyPair().secretKey\nconst nonce = nacl.randomBytes(16)\n\n// Any kind of data inside of a blob\nconst arr = new Uint8Array(10 * 1024 * 1024 + 111)\nfor (let i = 0; i \u003c arr.length; i++) arr[i] = i \u0026 0xff\nconst blob = new Blob([arr])\n\nconst encryptor = naclBlob.encrypt(key, nonce, blob, (err, encryptedBlob) =\u003e {\n  if (err) throw (err)\n  // some time later\n  const decryptor = naclBlob.decrypt(key, nonce, encryptedBlob, (err, decryptedBlob) =\u003e {\n    if (err) throw (err)\n    compareBlobs(blob, decryptedBlob)\n  })\n  decryptor.on('progress', ({position, length}) =\u003e { console.log('decrypting %' + (position / length) * 100) })\n})\n\nencryptor.on('progress', ({position, length}) =\u003e { console.log('encrypting %' + (position / length) * 100) })\n\n\nfunction compareBlobs (a, b) {\n  const r1 = new FileReader()\n  r1.onload = function () {\n    const r2 = new FileReader()\n    r2.onload = function () {\n      if (r1.result !== r2.result) {\n        console.error('blobs differ')\n      } else {\n        console.log('blobs are equal')\n      }\n    }\n    r2.readAsBinaryString(b)\n  }\n  r1.readAsBinaryString(a)\n}\n```\n\n## API\n\n### `encrypt = require('nacl-blob').encrypt`\n\nImport the `encrypt` function.\n\n### `decrypt = require('nacl-blob').decrypt`\n\nImport the `decrypt` function.\n\n### ⚠️ IMPORTANT ⚠️\n\nThis module uses a build-time [browserify](http://browserify.org) transform called [`workerify`](https://github.com/shama/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:\n\n```js\nconst encrypt = require('nacl-blob/dist').encrypt\nconst decrypt = require('nacl-blob/dist').decrypt\n```\n\n(Wanted, a workerify transform that writes out the contents to a separate bundle)\n\n### `encrypt(key, nonce, blob, [opts], cb)`\n\nEncrypt a [File](file) or [Blob](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][blob].\n\nThe `key` must be a 32-byte [Uint8Array][uint8] or [Node.js Buffer][nodebuff] (see [github.com/dchest/tweetnacl-js#usage](https://github.com/dchest/tweetnacl-js#usage) for details).\n\nThe `nonce` must be a 16-byte [Uint8Array][uint8] or [Node.js Buffer][nodebuff].  \n\nThe `blob` must be a [Blob][blob] or [File][file].\n\nOptional `opts` include: \n\n```js\n{\n  chunkSize: 1024 * 1024,\n  mimeType: blob.type\n}\n```\n\nThe `cb` function will fire when the file/blob has been encrypted and have the the following arguments:\n\n- `err`: Any error that occurred durning encryption.  You should handle this.\n- `encryptedBlob`: a [Blob][blob] containing the encrypted data.  This can be securely stored/transmitted along with the `nonce` across insecure networks and decrypted with the `key` (assuming secure key exchange is performed elsewhere).\n\nReturns an [Event Emitter][bus] that you can use to listen for the following events:\n\n- `progress`: An event that emits the the progress of encryption in the following shape:\n\n```js\n{\n  progress, // in bytes\n  length // total bytes\n}\n```\n\n### `decrypt(key, nonce, encryptedBlob, [opts], [cb])`\n\nDecrypt a [Blob][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][blob].\n\nThe `key` must be the same 32-byte [Uint8Array][uint8] or [Node.js Buffer][nodebuff] used to encrypt the file. (see [github.com/dchest/tweetnacl-js#usage](https://github.com/dchest/tweetnacl-js#usage))\n\nThe `nonce` must be the same 16-byte [Uint8Array][uint8] or [Node.js Buffer][nodebuff] used to encrypt the file.\n\nThe `encryptedBlob` must be an encrypted [Blob][blob] or [File][file].\n\nOptional `opts` include:\n\n```js\n{\n  chunkSize: 1024 * 1024,\n  mimeType: encryptedBlob.type\n}\n```\n\nThe `cb` function will fire when the file/blob has been decrypted and have the the following arguments:\n\n- `err`: Any error that occurred durning encryption.  You should handle this.\n- `decryptedBlob`: a [Blob][blob] containing the decrypted data.\n\nReturns an [Event Emitter][bus] that you can use to listen for the following events:\n\n- `progress`: An event that emits the the progress of encryption in the following shape:\n\n```js\n{\n  progress, // in bytes\n  length // total bytes\n}\n```\n\n## See Also\n\n- https://github.com/jedisct1/libsodium.js\n- https://github.com/jedisct1/libsodium/issues/475\n- https://developer.mozilla.org/en-US/docs/Web/API/Body/blob\n\n## License\n[MIT](https://tldrlegal.com/license/mit-license)\n\n[0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square\n[1]: https://nodejs.org/api/documentation.html#documentation_stability_index\n[2]: https://img.shields.io/npm/v/nacl-blob.svg?style=flat-square\n[3]: https://npmjs.org/package/nacl-blob\n[4]: https://img.shields.io/travis/bcomnes/nacl-blob/master.svg?style=flat-square\n[5]: https://travis-ci.org/bcomnes/nacl-blob\n[8]: http://img.shields.io/npm/dm/nacl-blob.svg?style=flat-square\n[9]: https://npmjs.org/package/nacl-blob\n[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square\n[11]: https://github.com/feross/standard\n[12]: https://ci.appveyor.com/api/projects/status/roy97n2re724n7rl/branch/master?svg=true\n[13]: https://ci.appveyor.com/project/bcomnes/nacl-blob/branch/master\n[uint8]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array\n[file]: https://developer.mozilla.org/en-US/docs/Web/API/File\n[blob]: https://developer.mozilla.org/en-US/docs/Web/API/Blob\n[bus]: https://github.com/choojs/nanobus\n[nodebuff]: https://nodejs.org/api/buffer.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Fnacl-blob","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbcomnes%2Fnacl-blob","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcomnes%2Fnacl-blob/lists"}