Skip to contents

Returns the SHA-256 digest of the theory's canonical form, as sha256: followed by 64 hexadecimal digits. The canonical form is the theory as JSON with every mapping's keys sorted by their UTF-16 code units, no whitespace, strings escaped only where JSON requires it and numbers written with the fewest digits that read back as the same double, as RFC 8785 specifies (Rundgren et al., 2020). Three conventions make it the same in R and Python: an empty mapping is written as an empty sequence, an infinity or a missing number as Infinity, -Infinity or NaN and a single value in a field that the schema types as an array of strings, such as measurement or derives_from, as a one-element array, since a reader takes derives_from: p1 for [p1]. tf_write() writes such a field as an array, so a theory keeps its hash when R writes it. The theory's own version$content_hash is left out, so that a file can record the hash of its content.

Usage

tf_content_hash(theory)

Arguments

theory

A theory object (named list).

Value

The hash as a single string.

Details

Two theory files with the same content therefore have the same hash whatever their layout, comments, key order or format, and any change to a value changes it. tf_validate() with full = TRUE checks a recorded hash against the content, and tf_preregister() and tf_dossier() give the hash, so that a reader can check that the theory file in hand is the one they describe.

References

Rundgren, A., Jordan, B., & Erdtman, S. (2020). JSON Canonicalization Scheme (JCS) (RFC 8785). RFC Editor. doi:10.17487/RFC8785

Examples

tf_content_hash(tf_read(tf_example_path("modality-switching.theory.yaml")))
#> [1] "sha256:c8109f9b8b7ca5df9c28ea0b42d799a4e9e608cad9cb0641e6030f9cd7bbb630"