Class JcsCanonicalizer

java.lang.Object
io.jans.lock.trace.crypto.bc.JcsCanonicalizer

public final class JcsCanonicalizer extends Object
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 \r where defined and lowercase \\uXXXX otherwise. Everything else, including U+007F, U+2028/U+2029 and the slash, is emitted literally.
  • Numbers follow the ECMAScript Number::toString algorithm: 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. long values never reach 1e21, so exponent form never applies to them; a BigIntegerNode at or above 1e21 is still emitted as plain digits.
  • DoubleNode and FloatNode use the ES6 algorithm on their double value.
  • DecimalNode (only produced when USE_BIG_DECIMAL_FOR_FLOATS is enabled) is converted with BigDecimal.doubleValue() first. This is lossy for values with more than 17 significant digits and matches what a double-based parser would have produced.
  • NaN and infinities, lone UTF-16 surrogates and non-JSON node types (POJO, binary, missing) raise JcsException.
The output of canonicalizeToUtf8(JsonNode) is UTF-8 without a byte-order mark.
Author:
Yuriy Movchan
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    canonicalize(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.JsonNode
    withoutField(com.fasterxml.jackson.databind.node.ObjectNode node, String fieldName)
    Returns a deep copy of node without the given top-level property.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • canonicalize

      public static String canonicalize(com.fasterxml.jackson.databind.JsonNode node)
      Canonicalizes the JSON value as a Java string (UTF-16). Use canonicalizeToUtf8(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 of node without the given top-level property. Nested properties with the same name are kept. The input node is not modified. Used to strip the signature property before computing the signature input.