| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Signing; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents provider-neutral hash metadata for a canonical AsiBackbone payload. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class CanonicalPayloadHash |
| | | 9 | | { |
| | 128 | 10 | | private CanonicalPayloadHash( |
| | 128 | 11 | | string artifactType, |
| | 128 | 12 | | string artifactId, |
| | 128 | 13 | | string payloadSchemaVersion, |
| | 128 | 14 | | string canonicalizationVersion, |
| | 128 | 15 | | string hashAlgorithm, |
| | 128 | 16 | | string hashValue) |
| | | 17 | | { |
| | 128 | 18 | | ArtifactType = artifactType; |
| | 128 | 19 | | ArtifactId = artifactId; |
| | 128 | 20 | | PayloadSchemaVersion = payloadSchemaVersion; |
| | 128 | 21 | | CanonicalizationVersion = canonicalizationVersion; |
| | 128 | 22 | | HashAlgorithm = hashAlgorithm; |
| | 128 | 23 | | HashValue = hashValue; |
| | 128 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the artifact type bound into the hashed payload. |
| | | 28 | | /// </summary> |
| | 248 | 29 | | public string ArtifactType { get; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the artifact identifier bound into the hashed payload. |
| | | 33 | | /// </summary> |
| | 228 | 34 | | public string ArtifactId { get; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the payload schema version bound into the hashed payload. |
| | | 38 | | /// </summary> |
| | 167 | 39 | | public string PayloadSchemaVersion { get; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets the canonicalization version used before hashing. |
| | | 43 | | /// </summary> |
| | 190 | 44 | | public string CanonicalizationVersion { get; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the hash algorithm descriptor. |
| | | 48 | | /// </summary> |
| | 225 | 49 | | public string HashAlgorithm { get; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the lowercase hexadecimal hash value. |
| | | 53 | | /// </summary> |
| | 414 | 54 | | public string HashValue { get; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Creates provider-neutral hash metadata. |
| | | 58 | | /// </summary> |
| | | 59 | | public static CanonicalPayloadHash Create( |
| | | 60 | | string artifactType, |
| | | 61 | | string artifactId, |
| | | 62 | | string payloadSchemaVersion, |
| | | 63 | | string canonicalizationVersion, |
| | | 64 | | string hashAlgorithm, |
| | | 65 | | string hashValue) |
| | | 66 | | { |
| | 128 | 67 | | ArgumentException.ThrowIfNullOrWhiteSpace(artifactType); |
| | 128 | 68 | | ArgumentException.ThrowIfNullOrWhiteSpace(artifactId); |
| | 128 | 69 | | ArgumentException.ThrowIfNullOrWhiteSpace(payloadSchemaVersion); |
| | 128 | 70 | | ArgumentException.ThrowIfNullOrWhiteSpace(canonicalizationVersion); |
| | 128 | 71 | | ArgumentException.ThrowIfNullOrWhiteSpace(hashAlgorithm); |
| | 128 | 72 | | ArgumentException.ThrowIfNullOrWhiteSpace(hashValue); |
| | | 73 | | |
| | 128 | 74 | | return new CanonicalPayloadHash( |
| | 128 | 75 | | artifactType.Trim(), |
| | 128 | 76 | | artifactId.Trim(), |
| | 128 | 77 | | payloadSchemaVersion.Trim(), |
| | 128 | 78 | | canonicalizationVersion.Trim(), |
| | 128 | 79 | | NormalizeHashAlgorithm(hashAlgorithm), |
| | 128 | 80 | | hashValue.Trim().ToLowerInvariant()); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Creates signing metadata that carries the hash and canonical payload descriptors without implying that the artif |
| | | 85 | | /// </summary> |
| | | 86 | | public SigningMetadata ToSigningMetadata(IReadOnlyDictionary<string, string>? metadata = null) |
| | | 87 | | { |
| | 81 | 88 | | Dictionary<string, string> hashMetadata = new(StringComparer.Ordinal) |
| | 81 | 89 | | { |
| | 81 | 90 | | ["artifact_id"] = ArtifactId, |
| | 81 | 91 | | ["artifact_type"] = ArtifactType, |
| | 81 | 92 | | ["canonicalization_version"] = CanonicalizationVersion, |
| | 81 | 93 | | ["payload_schema_version"] = PayloadSchemaVersion |
| | 81 | 94 | | }; |
| | | 95 | | |
| | 81 | 96 | | if (metadata is not null) |
| | | 97 | | { |
| | 4 | 98 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 99 | | { |
| | 1 | 100 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 101 | | { |
| | | 102 | | continue; |
| | | 103 | | } |
| | | 104 | | |
| | 1 | 105 | | hashMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | 81 | 109 | | return SigningMetadata.Create( |
| | 81 | 110 | | signingHash: HashValue, |
| | 81 | 111 | | hashAlgorithm: HashAlgorithm, |
| | 81 | 112 | | metadata: hashMetadata); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | internal static string NormalizeHashAlgorithm(string hashAlgorithm) |
| | | 116 | | { |
| | 300 | 117 | | string normalized = hashAlgorithm.Trim(); |
| | | 118 | | |
| | 300 | 119 | | return normalized.Equals("SHA256", StringComparison.OrdinalIgnoreCase) |
| | 300 | 120 | | ? CanonicalPayloadOptions.DefaultHashAlgorithm |
| | 300 | 121 | | : normalized.Equals(CanonicalPayloadOptions.DefaultHashAlgorithm, StringComparison.OrdinalIgnoreCase) |
| | 300 | 122 | | ? CanonicalPayloadOptions.DefaultHashAlgorithm |
| | 300 | 123 | | : normalized; |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Computes provider-neutral hashes for canonical AsiBackbone payloads. |
| | | 129 | | /// </summary> |
| | | 130 | | public static class CanonicalPayloadHasher |
| | | 131 | | { |
| | | 132 | | /// <summary> |
| | | 133 | | /// Computes a hash over the canonical payload bytes. |
| | | 134 | | /// </summary> |
| | | 135 | | public static CanonicalPayloadHash ComputeHash( |
| | | 136 | | CanonicalPayload payload, |
| | | 137 | | string? hashAlgorithm = null) |
| | | 138 | | { |
| | | 139 | | ArgumentNullException.ThrowIfNull(payload); |
| | | 140 | | |
| | | 141 | | string algorithm = CanonicalPayloadHash.NormalizeHashAlgorithm( |
| | | 142 | | string.IsNullOrWhiteSpace(hashAlgorithm) |
| | | 143 | | ? CanonicalPayloadOptions.DefaultHashAlgorithm |
| | | 144 | | : hashAlgorithm); |
| | | 145 | | |
| | | 146 | | if (!algorithm.Equals(CanonicalPayloadOptions.DefaultHashAlgorithm, StringComparison.Ordinal)) |
| | | 147 | | { |
| | | 148 | | throw new NotSupportedException($"The built-in canonical payload hasher supports {CanonicalPayloadOptions.De |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | byte[] hashBytes = SHA256.HashData(payload.ToUtf8Bytes()); |
| | | 152 | | string hashValue = Convert.ToHexString(hashBytes).ToLowerInvariant(); |
| | | 153 | | |
| | | 154 | | return CanonicalPayloadHash.Create( |
| | | 155 | | payload.ArtifactType, |
| | | 156 | | payload.ArtifactId, |
| | | 157 | | payload.PayloadSchemaVersion, |
| | | 158 | | payload.CanonicalizationVersion, |
| | | 159 | | algorithm, |
| | | 160 | | hashValue); |
| | | 161 | | } |
| | | 162 | | } |