{"name":"canonicalize","version":"3.0.0","keywords":["json","canonical","canonicalize","signing","crypto"],"license":"Apache-2.0","_id":"canonicalize@3.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":"a8073c48c1835631e1829d759078d2bbf48487fc","tarball":"http://registry-npm.ywwl.com/canonicalize/download/canonicalize-3.0.0.tgz","fileCount":6,"integrity":"sha512-yYLfHyDMIXRyRqsKBRLX023riFLpXY2YOfdtqKXZRZy9qsfOJ9U+4F9YZL7MEzL5+ziN2x2nlBvY/Voi3EBljA==","signatures":[{"sig":"MEQCIC7cekEMq4Y2HaJhFm6/oWqL71xe17UTAXC/t8FFes1EAiAjuFKYI105yoU19D1BjfdYG31B/fVJ8WUnozdY/Xl6Vg==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":16314,"size":6020,"noattachment":false,"key":"/canonicalize/-/canonicalize-3.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":"aba9209d044f2729c51141d8a73b11e80816e42c","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":"10.8.2","description":"JSON canonicalize function","directories":{},"_nodeVersion":"20.20.1","_hasShrinkwrap":false,"devDependencies":{"c8":"^10.0.0","eslint":"^9.0.0","globals":"^15.0.0","@eslint/js":"^9.0.0"},"_npmOperationalInternal":{"tmp":"tmp/canonicalize_3.0.0_1775681432035_0.7688209301595388","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":1775681432230,"_cnpm_publish_time":1775681432230,"dist-tags":{"latest":"5.1.0"}}