{"id":15646449,"url":"https://github.com/martinheidegger/subtle-key-codec","last_synced_at":"2025-03-29T23:17:16.320Z","repository":{"id":57702450,"uuid":"501969921","full_name":"martinheidegger/subtle-key-codec","owner":"martinheidegger","description":"Space efficient binary de-/encoder for WebCrypto's CryptoKeys","archived":false,"fork":false,"pushed_at":"2022-06-10T09:32:57.000Z","size":40,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-06T16:16:48.075Z","etag":null,"topics":["binary","codec","decoding","encoding","subtlecrypto","uint8array","webcrypto"],"latest_commit_sha":null,"homepage":"","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/martinheidegger.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2022-06-10T08:52:19.000Z","updated_at":"2022-06-10T21:59:22.000Z","dependencies_parsed_at":"2022-09-26T21:11:19.397Z","dependency_job_id":null,"html_url":"https://github.com/martinheidegger/subtle-key-codec","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinheidegger%2Fsubtle-key-codec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinheidegger%2Fsubtle-key-codec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinheidegger%2Fsubtle-key-codec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinheidegger%2Fsubtle-key-codec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/martinheidegger","download_url":"https://codeload.github.com/martinheidegger/subtle-key-codec/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246254149,"owners_count":20747949,"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":["binary","codec","decoding","encoding","subtlecrypto","uint8array","webcrypto"],"created_at":"2024-10-03T12:12:57.667Z","updated_at":"2025-03-29T23:17:16.291Z","avatar_url":"https://github.com/martinheidegger.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# subtle-key-codec\n\nEncodes/Decodes WebCrypto keys efficiently and easily into `Uint8Array`s.\nGood when you want to safe space (QR Codes).\n\n```js\nimport { codec } from 'subtle-key-codec'\n\nconst key = // ... generated via WebCrypto API\n\nconst uint8Array = await codec.encode(key)\nconst key = await codec.decode(bytes)\n```\n\n### Why use it?\n\n- To get a simple API that just works.\n- To have smaller QR codes.\n- It integrate with other Binary data libraries.\n\n(more in [the Comparison](#comparison))\n\n#### What is in the package?\n\n- An encoder/decoder to turn WebCrypto keys into Uint8Arrays.\n- ESM modules, CommonJS modules and typescript definitions.\n\n## Comparison\n\nThe [WebCrypto][] API _allows_ to `import/export` keys. But the API is rather\ndifficult to use. Not every key supports every input/output format and getting\na space-efficient representation of codec is not available.\n\nAs you can see above the API is simple. :wink:\n\n[WebCrypto]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API\n\nIt _is possible_ to relativey turn any key into a `Uint8Array` with the native\nAPI like this:\n\n```js\nconst bytes = Buffer.from(\n  JSON.stringify(\n    await crypto.subtle.exportKey('jwk', key)\n  )\n).toString('base64')\n```\n\nHowever, the size of this is rather big. In the [tests][] this library creates\nbase64 strings that are `7.7%` to `75%` of the size!\n\nThis can make a big difference when you want to turn keys into QR Codes.\n\n[tests]: https://github.com/martinheidegger/subtle-key-codec/actions\n\n## API\n\nBeside the basic API, there are are few additional features that may help you to\nbe faster or consume less memory.\n\n### BYOA and pre-determined length\n\nBy default the codec will create a new `Uint8Array` for you consume. But you can\nalso pass-in a pre-created Uint8Array to the encoding process.\n\n```js\nawait codec.encode(key, new Uint8Array(1000))\n```\n\nThis can be useful if you just want to add a key to a set of keys. But you may want\nto pass-in also the `offset` place it entirely in an array.\n\n```js\nawait codec.encode(key, new Uint8Array(1000), 2)\n```\n\nThe example has an abritary size of `1000` but you can also figure out in advance\nhow big the key is.\n\n```js\nawait codec.encodingLength(key)\n```\n\n### Specific Codec\n\n`.encode` will usually prefix the resulting Uint8Array with a type identifier.\nA value from `0~255` that specifies which particular key-codec is used to de-/encode\na particular key.\n\n```js\n // this will tell you the constant number of that codec\nconst uint8 = codec.getKeyType(key)\n// You can also get the entire codec\nconst keyCodec = codec.getKeyCodec(key)\n// ... to use wthout the prefix.\nconst withoutIndexPrefix = keyCodec.encode(key)\n// ... in both directions\nkey = keyCodec.decode(withoutIndexPrefix)\n```\n\n### Use with older Node versions\n\nThis library has been tested with the [isomorphic-webcrypto][] library that works\nwith older versions of Node.js. To use it you need to create a crypto object rather\nthan using the default crypto object.\n\n```js\nimport { createCodec } from 'subtle-key-codec'\nimport crypto from 'isomorphic-webcrypto'\n\nconst codec = createCodec(crypto)\n```\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartinheidegger%2Fsubtle-key-codec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmartinheidegger%2Fsubtle-key-codec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartinheidegger%2Fsubtle-key-codec/lists"}