| | | 1 | | //======================================================================= |
| | | 2 | | // ChronicleHashSerializer.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | |
| | | 10 | | namespace Chronicler.Hashing; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Computes deterministic record hashes by traversing <see cref="IRecordable.RecordData(IChronicler)"/>. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// The hash includes the recorded types, field names, defaults, values, and stable link IDs in schema order. |
| | | 17 | | /// Supported leaf types are Boolean, fixed-width integers up to 64 bits, characters, strings, and enums. |
| | | 18 | | /// </remarks> |
| | | 19 | | public static class ChronicleHashSerializer |
| | | 20 | | { |
| | | 21 | | /// <summary>Computes a record hash with an empty link registry.</summary> |
| | | 22 | | /// <param name="target">The graph whose schema is traversed in saving mode and in recording-call order.</param> |
| | | 23 | | /// <returns>The version-one hash of the declared state.</returns> |
| | | 24 | | /// <exception cref="ArgumentNullException">The target is null.</exception> |
| | | 25 | | /// <exception cref="InvalidOperationException">A non-null link needs a stable ID. Use the overload with a populated |
| | | 26 | | /// <exception cref="NotSupportedException">The schema records a leaf type unsupported by the hash backend.</excepti |
| | | 27 | | public static ChronicleHash Compute(IRecordable target) |
| | | 28 | | { |
| | 255 | 29 | | return Compute(target, new ChronicleContext()); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary>Computes a deterministic hash of the declared state.</summary> |
| | | 33 | | /// <param name="target">The graph whose schema is traversed in saving mode and in recording-call order.</param> |
| | | 34 | | /// <param name="context">The registry context used to look up stable IDs for links.</param> |
| | | 35 | | /// <returns>The hash of the version-one record stream, including its section header.</returns> |
| | | 36 | | /// <exception cref="ArgumentNullException">The target or context is null.</exception> |
| | | 37 | | /// <exception cref="InvalidOperationException">A non-null link has no stable ID in the context.</exception> |
| | | 38 | | /// <exception cref="NotSupportedException">The schema records a leaf type unsupported by the hash backend.</excepti |
| | | 39 | | public static ChronicleHash Compute(IRecordable target, ChronicleContext context) |
| | | 40 | | { |
| | 73997 | 41 | | if (target == null) |
| | 1 | 42 | | throw new ArgumentNullException(nameof(target)); |
| | 73996 | 43 | | if (context == null) |
| | 1 | 44 | | throw new ArgumentNullException(nameof(context)); |
| | | 45 | | |
| | 73995 | 46 | | var writer = new ChronicleHashWriter(); |
| | 73995 | 47 | | writer.WriteSection("chronicler.hash", 1); |
| | 73995 | 48 | | Contribute(target, context, ref writer); |
| | 73990 | 49 | | return writer.ToHash(); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary>Appends a record hash contribution with an empty link registry.</summary> |
| | | 53 | | /// <param name="target">The graph whose schema is traversed in saving mode and in recording-call order.</param> |
| | | 54 | | /// <param name="writer">The caller's running writer. Appends the record on success and preserves the writer if reco |
| | | 55 | | /// <exception cref="ArgumentNullException">The target is null.</exception> |
| | | 56 | | /// <exception cref="InvalidOperationException">A non-null link needs a stable ID. Use the overload with a populated |
| | | 57 | | /// <exception cref="NotSupportedException">The schema records a leaf type unsupported by the hash backend.</excepti |
| | | 58 | | public static void Contribute(IRecordable target, ref ChronicleHashWriter writer) |
| | | 59 | | { |
| | 2 | 60 | | Contribute(target, new ChronicleContext(), ref writer); |
| | 1 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary>Appends a record to a running hash.</summary> |
| | | 64 | | /// <param name="target">The graph whose schema is traversed in saving mode and in recording-call order.</param> |
| | | 65 | | /// <param name="context">The registry context used to look up stable IDs for links.</param> |
| | | 66 | | /// <param name="writer">The caller's running writer. Appends the record on success and preserves the writer if reco |
| | | 67 | | /// <remarks>Use the same contribution order in every caller that needs matching hashes.</remarks> |
| | | 68 | | /// <exception cref="ArgumentNullException">The target or context is null.</exception> |
| | | 69 | | /// <exception cref="InvalidOperationException">A non-null link has no stable ID in the context.</exception> |
| | | 70 | | /// <exception cref="NotSupportedException">The schema records a leaf type unsupported by the hash backend.</excepti |
| | | 71 | | public static void Contribute(IRecordable target, ChronicleContext context, ref ChronicleHashWriter writer) |
| | | 72 | | { |
| | 73998 | 73 | | if (target == null) |
| | 1 | 74 | | throw new ArgumentNullException(nameof(target)); |
| | 73997 | 75 | | if (context == null) |
| | 1 | 76 | | throw new ArgumentNullException(nameof(context)); |
| | | 77 | | |
| | 73996 | 78 | | ChronicleHashChronicler.Contribute(target, context, ref writer); |
| | 73991 | 79 | | } |
| | | 80 | | } |