Package io.jans.lock.trace.crypto.bc
Class JcsCanonicalizer
java.lang.Object
io.jans.lock.trace.crypto.bc.JcsCanonicalizer
In-house, dependency-free implementation of RFC 8785 (JSON Canonicalization Scheme, JCS) over
Jackson
JsonNode trees. Used for TRACE signature input, content digests, receipt hashes
and composite keys.
Rules implemented (RFC 8785 section 3.2):
- No whitespace between tokens.
- Object properties are sorted by the UTF-16 code-unit sequence of their raw names
(equivalent to
String.compareTo(String)); arrays keep their order. - Strings escape
"and\and the control characters U+0000-U+001F, using the short forms\b \t \n \f \rwhere defined and lowercase\\uXXXXotherwise. Everything else, including U+007F, U+2028/U+2029 and the slash, is emitted literally. - Numbers follow the ECMAScript
Number::toStringalgorithm: the shortest decimal that round-trips to the same IEEE-754 double, formatted with ES6 exponent rules. - Literals are
true,false,null.
Documented implementation choices (RFC 8785 section 3.2.2.3 lets implementations restrict number handling):
- Integral nodes (
IntNode,ShortNode,LongNode,BigIntegerNode) are serialized as their exact decimal digits, also outside the IEEE-754 safe range (|v| > 2^53-1) where ECMAScript would round to the nearest double.longvalues never reach 1e21, so exponent form never applies to them; aBigIntegerNodeat or above 1e21 is still emitted as plain digits. DoubleNodeandFloatNodeuse the ES6 algorithm on their double value.DecimalNode(only produced whenUSE_BIG_DECIMAL_FOR_FLOATSis enabled) is converted withBigDecimal.doubleValue()first. This is lossy for values with more than 17 significant digits and matches what a double-based parser would have produced.NaNand infinities, lone UTF-16 surrogates and non-JSON node types (POJO, binary, missing) raiseJcsException.
canonicalizeToUtf8(JsonNode) is UTF-8 without a byte-order mark.- Author:
- Yuriy Movchan
-
Method Summary
Modifier and TypeMethodDescriptionstatic Stringcanonicalize(com.fasterxml.jackson.databind.JsonNode node) Canonicalizes the JSON value as a Java string (UTF-16).static byte[]canonicalizeToUtf8(com.fasterxml.jackson.databind.JsonNode node) Canonicalizes the JSON value and encodes the result as UTF-8 (no BOM).static com.fasterxml.jackson.databind.JsonNodewithoutField(com.fasterxml.jackson.databind.node.ObjectNode node, String fieldName) Returns a deep copy ofnodewithout the given top-level property.
-
Method Details
-
canonicalize
Canonicalizes the JSON value as a Java string (UTF-16). UsecanonicalizeToUtf8(JsonNode)for the byte form required by cryptographic operations.- Throws:
JcsException- if the node contains values that JCS cannot represent
-
canonicalizeToUtf8
public static byte[] canonicalizeToUtf8(com.fasterxml.jackson.databind.JsonNode node) Canonicalizes the JSON value and encodes the result as UTF-8 (no BOM).- Throws:
JcsException- if the node contains values that JCS cannot represent
-
withoutField
public static com.fasterxml.jackson.databind.JsonNode withoutField(com.fasterxml.jackson.databind.node.ObjectNode node, String fieldName) Returns a deep copy ofnodewithout the given top-level property. Nested properties with the same name are kept. The input node is not modified. Used to strip thesignatureproperty before computing the signature input.
-