{"_id":"canonicalize@5.1.0","bin":{"canonicalize":"bin/canonicalize.js"},"bugs":{"url":"https://github.com/erdtman/canonicalize/issues"},"dist":{"shasum":"4e429a81a542933e8db7647b7e916cbdb60d272c","tarball":"http://registry-npm.ywwl.com/canonicalize/download/canonicalize-5.1.0.tgz","integrity":"sha512-bab9+WefZljqQ8jT0qRwVmTyzAUU63K97QIQD3agynIBrb6sa5M2eAy8Hv8K3p4V9+ONVYZKtGYl4Dg+/5Gumw==","fileCount":6,"unpackedSize":24474,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/canonicalize@5.1.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIAJIFapJnNmSEens1O7RMuHVcQG0KR1avwmtShbt2mUIAiEAwdyEn3AMnGY0SavXfXUqaKOySYnXanfkCZZOluSfDkA="}],"size":8688,"noattachment":false,"key":"/canonicalize/-/canonicalize-5.1.0.tgz"},"main":"lib/canonicalize.js","name":"canonicalize","type":"module","types":"lib/canonicalize.d.ts","engines":{"node":">=22"},"exports":{".":{"types":"./lib/canonicalize.d.ts","import":"./lib/canonicalize.js","default":"./lib/canonicalize.js"}},"gitHead":"31fb117afde3ef305b2e1d99bde539bf6300bc62","license":"Apache-2.0","scripts":{"lint":"eslint .","test":"node --test test/*.js","coverage":"c8 npm test","lint:fix":"eslint --fix .","coveragehtml":"c8 report -r html","prepublishOnly":"npm run lint && npm test","precoveragehtml":"npm run coverage"},"version":"5.1.0","_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:34722c95-a026-4671-b343-a58eac0401e0"},"approver":{"name":"samuelerdtman","email":"samuel@erdtman.se"}},"homepage":"https://github.com/erdtman/canonicalize#readme","keywords":["json","canonical","canonicalize","signing","crypto"],"repository":{"url":"git+https://github.com/erdtman/canonicalize.git","type":"git"},"_npmVersion":"12.0.2","description":"JSON canonicalize function","directories":{},"maintainers":[{"name":"samuelerdtman","email":"samuel@erdtman.se"}],"_nodeVersion":"22.23.2","contributors":[{"name":"Samuel Erdtman","email":"samuel@erdtman.se"},{"name":"Anders Rundgren","email":"anders.rundgren.net@gmail.com"}],"publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"devDependencies":{"c8":"^12.0.0","eslint":"^9.0.0","globals":"^15.0.0","@eslint/js":"^9.0.0"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/canonicalize_5.1.0_1789764404011_0.06727641027281339"},"_hasShrinkwrap":false,"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":1789764404100,"_cnpm_publish_time":1789764404100,"dist-tags":{"latest":"5.1.0"}}