{"name":"canonicalize","version":"4.0.0","keywords":["json","canonical","canonicalize","signing","crypto"],"license":"Apache-2.0","_id":"canonicalize@4.0.0","maintainers":[{"name":"samuelerdtman","email":"samuel@erdtman.se"}],"contributors":[{"name":"Samuel Erdtman","email":"samuel@erdtman.se"},{"name":"Anders Rundgren","email":"anders.rundgren.net@gmail.com"}],"homepage":"https://github.com/erdtman/canonicalize#readme","bugs":{"url":"https://github.com/erdtman/canonicalize/issues"},"bin":{"canonicalize":"bin/canonicalize.js"},"dist":{"shasum":"b13b8e26b9043a56cb751c27bee8e0c2203bb9cc","tarball":"http://registry-npm.ywwl.com/canonicalize/download/canonicalize-4.0.0.tgz","fileCount":6,"integrity":"sha512-FEdXzwWs+N3rZqEqpqleiY9M1A6IAf9oo1zHFABnLW9FcJ/jzsu+G/Ks3Hq3FglmPKe80GeGBw8ZXLEnwPB0vQ==","signatures":[{"sig":"MEUCIE4gbqKrxbKiOwFVR5aOxn4Dj0hYlhqH9ZV/pTowt6CaAiEAzJq5+DYSMkZYvd/qJdFsYnfFWjLQibrOOUUFuBN/5e0=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"sig":"MEYCIQCEG/ZSodfk3BKa56eDVZ2zbS9rZTFKPqjnc+/XNBOWaAIhAK0EjuljclrBRkwgH/wwb3AUc9veoldy8e9sGNMXoAm4","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":16897,"size":6205,"noattachment":false,"key":"/canonicalize/-/canonicalize-4.0.0.tgz"},"main":"lib/canonicalize.js","type":"module","types":"lib/canonicalize.d.ts","engines":{"node":">=18"},"exports":{".":{"types":"./lib/canonicalize.d.ts","import":"./lib/canonicalize.js"}},"gitHead":"c1b08c3771d681c8bd9c4d8765e00f2f717482f8","scripts":{"lint":"eslint .","test":"node --test test/*.js","coverage":"c8 npm test","lint:fix":"eslint --fix .","coveragehtml":"c8 report -r html","precoveragehtml":"npm run coverage"},"_npmUser":{"name":"samuelerdtman","email":"samuel@erdtman.se"},"repository":{"url":"git+https://github.com/erdtman/canonicalize.git","type":"git"},"_npmVersion":"11.13.0","description":"JSON canonicalize function","directories":{},"_nodeVersion":"26.1.0","_hasShrinkwrap":false,"devDependencies":{"c8":"^10.0.0","eslint":"^9.0.0","globals":"^15.0.0","@eslint/js":"^9.0.0"},"_npmOperationalInternal":{"tmp":"tmp/canonicalize_4.0.0_1786534564723_0.8715549599871353","host":"s3://npm-registry-packages-npm-production"},"readme":"[![CI](https://github.com/erdtman/canonicalize/actions/workflows/ci.yml/badge.svg)](https://github.com/erdtman/canonicalize/actions/workflows/ci.yml)\n# canonicalize\nJSON canonicalize function. Creates crypto safe predictable canonicalization of\nJSON as defined by [RFC8785](https://tools.ietf.org/html/rfc8785).\n\nTypeScript type definitions are included.\n\n## Usage\n### Normal Example\n```js\nimport canonicalize from 'canonicalize';\nconst json = {\n\t\"from_account\": \"543 232 625-3\",\n\t\"to_account\": \"321 567 636-4\",\n\t\"amount\": 500,\n\t\"currency\": \"USD\"\n}\nconsole.log(canonicalize(json));\n// output: {\"amount\":500,\"currency\":\"USD\",\"from_account\":\"543 232 625-3\",\"to_account\":\"321 567 636-4\"}\n```\n### Crazy Example\n```js\nimport canonicalize from 'canonicalize';\nconst json = {\n\t\"1\": {\"f\": {\"f\":  \"hi\",\"F\":  5} ,\"\\n\":  56.0},\n\t\"10\": { },\n\t\"\":  \"empty\",\n\t\"a\": { },\n\t\"111\": [ {\"e\":  \"yes\",\"E\":  \"no\" } ],\n\t\"A\": { }\n}\nconsole.log(canonicalize(json));\n// output: {\"\":\"empty\",\"1\":{\"\\n\":56,\"f\":{\"F\":5,\"f\":\"hi\"}},\"10\":{},\"111\":[{\"E\":\"no\",\"e\":\"yes\"}],\"A\":{},\"a\":{}}\n```\n### From CommonJS\nNode 22.12 and later can `require()` an ES module. The module namespace is\nreturned as-is, so the function is reached through its `default` property:\n```js\nconst canonicalize = require('canonicalize').default;\nconsole.log(canonicalize({ b: 123, a: 'string' }));\n// output: {\"a\":\"string\",\"b\":123}\n```\n### Via CLI\nThe function can be executed directly using npx without explicit installation. This allows JSON files and arbitrary input to be canonicalized with standard input/output:\n```bash\n# Input from file\nnpx canonicalize < input.json > output.json\n\n# Input from string\necho '{\n\t\"from_account\": \"543 232 625-3\",\n\t\"to_account\": \"321 567 636-4\",\n\t\"amount\": 500,\n\t\"currency\": \"USD\"\n}' | npx canonicalize > simple-data.json\n\n# Input from web API\ncurl --silent https://pokeapi.co/api/v2/pokemon/pikachu | npx canonicalize > pikachu.json\n```\n## Best practices for signature schemes\nHere follows an example of how untrusted input can be processed and validated\nbefore being treated as safe.\n\n```js\nimport canonicalize from 'canonicalize';\n\n// The received document is untrusted until validateSignature succeeds\nconst untrustedInputText = /* raw document as received */;\nconst inputSignature = /* detached signature as received */;\n\n// Duplicate property names are resolved here, last-one-wins, by JSON.parse —\n// before canonicalize() ever sees the data. To reject documents that carried\n// duplicates on the wire, inspect untrustedInputText before this line.\nconst untrustedInputObject = JSON.parse(untrustedInputText);\n\nconst canonicalText = canonicalize(untrustedInputObject);\nconst isValid = validateSignature(canonicalText, inputSignature);\n\nif (isValid) {\n  // canonicalText is the only artifact the signature covers.\n  //\n  // untrustedInputObject is not that artifact. It was derived from\n  // untrustedInputText and was never validated. It is parsable, but it is not\n  // what was signed — do not treat the two as interchangeable.\n  const validatedInput = JSON.parse(canonicalText);\n\n  // From here on, operate on validatedInput.\n}\n```\n\nForwarding `untrustedInputText` downstream passes on bytes an attacker could\nhave altered while verification still succeeded; a consumer that resolves\nduplicate property names first-one-wins will then read a different document\nthan the one you validated.\n\n## Comparison with other implementations\n[A reproducible comparison of the JavaScript JCS libraries on npm](https://erdtman.github.io/json-canonicalisation-comparison/)\nchecks each one against the RFC 8785 test vectors, 10,000 ES6\nnumber-serialization vectors, and edge cases such as lone surrogates, alongside\nbenchmarks, maximum nesting depth, output validity and bundle size.\n\n## Install\nAs a library:\n```\nnpm install canonicalize --save\n```\nAs a CLI tool:\n```\nnpm install -g canonicalize\ncanonicalize < input.json > output.json\n```\n## Test\n```\nnpm test\n```\n","publish_time":1786534564809,"_cnpm_publish_time":1786534564809,"dist-tags":{"latest":"5.1.0"}}