|
1 | 1 | # json-canonicalization |
2 | 2 | An implementation of the JSON Canonicalization Scheme for Ruby |
| 3 | + |
| 4 | +Implements version 5 of [draft-rundgren-json-canonicalization-scheme-05](https://tools.ietf.org/html/draft-rundgren-json-canonicalization-scheme-05#page-5). |
| 5 | + |
| 6 | +[](http://badge.fury.io/rb/json-canonicalization) |
| 7 | +[](http://travis-ci.org/dryruby/json-canonicalization) |
| 8 | +[](https://coveralls.io/r/dryruby/json-canonicalization) |
| 9 | + |
| 10 | +# Description |
| 11 | + |
| 12 | +Cryptographic operations like hashing and signing depend on that the target |
| 13 | +data does not change during serialization, transport, or parsing. |
| 14 | +By applying the rules defined by JCS (JSON Canonicalization Scheme), |
| 15 | +data provided in the JSON [[RFC8259](https://tools.ietf.org/html/rfc8259)] |
| 16 | +format can be exchanged "as is", while still being subject to secure cryptographic operations. |
| 17 | +JCS achieves this by building on the serialization formats for JSON |
| 18 | +primitives as defined by ECMAScript [[ES6](https://www.ecma-international.org/ecma-262/6.0/index.html)], |
| 19 | +constraining JSON data to the<br>I-JSON [[RFC7493](https://tools.ietf.org/html//rfc7493)] subset, |
| 20 | +and through a platform independent property sorting scheme. |
| 21 | + |
| 22 | +Working document: https://cyberphone.github.io/ietf-json-canon<br> |
| 23 | +Published IETF Draft: https://tools.ietf.org/html/draft-rundgren-json-canonicalization-scheme-05 |
| 24 | + |
| 25 | +The JSON Canonicalization Scheme concept in a nutshell: |
| 26 | +- Serialization of primitive JSON data types using methods compatible with ECMAScript's `JSON.stringify()` |
| 27 | +- Lexicographic sorting of JSON `Object` properties in a *recursive* process |
| 28 | +- JSON `Array` data is also subject to canonicalization, *but element order remains untouched* |
| 29 | + |
| 30 | +### Sample Input: |
| 31 | +```code |
| 32 | +{ |
| 33 | + "numbers": [333333333.33333329, 1E30, 4.50, 2e-3, 0.000000000000000000000000001], |
| 34 | + "string": "\u20ac$\u000F\u000aA'\u0042\u0022\u005c\\\"\/", |
| 35 | + "literals": [null, true, false] |
| 36 | +} |
| 37 | +``` |
| 38 | +### Expected Output: |
| 39 | +```code |
| 40 | +{"literals":[null,true,false],"numbers":[333333333.3333333,1e+30,4.5,0.002,1e-27],"string":"€$\u000f\nA'B\"\\\\\"/"} |
| 41 | +``` |
| 42 | +## Usage |
| 43 | +The library accepts Ruby input and generates canonical JSON via the `#to_json_c14n` method. This is based on the standard JSON gem's version of `#to_json` with overloads for `Hash`, `String` and `Numeric` |
| 44 | + |
| 45 | +```ruby |
| 46 | +data = { |
| 47 | + "numbers" => [ |
| 48 | + 333333333.3333333, |
| 49 | + 1.0e+30, |
| 50 | + 4.5, |
| 51 | + 0.002, |
| 52 | + 1.0e-27 |
| 53 | + ], |
| 54 | + "string" => "€$\u000F\nA'B\"\\\\\"/", |
| 55 | + "literals" => [nil, true, false] |
| 56 | +} |
| 57 | + |
| 58 | +puts data.to_json_c14n |
| 59 | +=> |
| 60 | +``` |
| 61 | + |
| 62 | +## Documentation |
| 63 | +Full documentation available on [RubyDoc](http://rubydoc.info/gems/json-canonicalization/file/README.md) |
| 64 | + |
| 65 | +### Principal Classes |
| 66 | +* {JSON::Canonicalization} |
| 67 | + |
| 68 | +## Dependencies |
| 69 | +* [Ruby](http://ruby-lang.org/) (>= 2.2.2) |
| 70 | +* [JSON](https://rubygems.org/gems/json) (>= 2.1) |
| 71 | + |
| 72 | +## Author |
| 73 | +* [Gregg Kellogg](http://github.com/gkellogg) - <http://kellogg-assoc.com/> |
| 74 | + |
| 75 | +## Contributing |
| 76 | +* Do your best to adhere to the existing coding conventions and idioms. |
| 77 | +* Don't use hard tabs, and don't leave trailing whitespace on any line. |
| 78 | +* Do document every method you add using [YARD][] annotations. Read the |
| 79 | + [tutorial][YARD-GS] or just look at the existing code for examples. |
| 80 | +* Don't touch the `json-ld.gemspec`, `VERSION` or `AUTHORS` files. If you need to |
| 81 | + change them, do so on your private branch only. |
| 82 | +* Do feel free to add yourself to the `CREDITS` file and the corresponding |
| 83 | + list in the the `README`. Alphabetical order applies. |
| 84 | +* Do note that in order for us to merge any non-trivial changes (as a rule |
| 85 | + of thumb, additions larger than about 15 lines of code), we need an |
| 86 | + explicit [public domain dedication][PDD] on record from you. |
| 87 | + |
| 88 | +##License |
| 89 | + |
| 90 | +This is free and unencumbered public domain software. For more information, |
| 91 | +see <http://unlicense.org/> or the accompanying {file:UNLICENSE} file. |
| 92 | + |
0 commit comments