|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#nullable enable |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Collections.Immutable; |
| 9 | +using System.IO; |
| 10 | +using System.Linq; |
| 11 | +using System.Reflection; |
| 12 | +using System.Reflection.Metadata; |
| 13 | +using System.Security.Cryptography; |
| 14 | + |
| 15 | +namespace ILCompiler |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Specifies a hash algorithms used for hashing source files. |
| 19 | + /// </summary> |
| 20 | + public enum SourceHashAlgorithm |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// No algorithm specified. |
| 24 | + /// </summary> |
| 25 | + None = 0, |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Secure Hash Algorithm 1. |
| 29 | + /// </summary> |
| 30 | + Sha1 = 1, |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Secure Hash Algorithm 2 with a hash size of 256 bits. |
| 34 | + /// </summary> |
| 35 | + Sha256 = 2, |
| 36 | + } |
| 37 | + |
| 38 | + internal static class SourceHashAlgorithmUtils |
| 39 | + { |
| 40 | + public const SourceHashAlgorithm DefaultContentHashAlgorithm = SourceHashAlgorithm.Sha256; |
| 41 | + } |
| 42 | + |
| 43 | + internal abstract class CryptographicHashProvider |
| 44 | + { |
| 45 | + private ImmutableArray<byte> _lazySHA1Hash; |
| 46 | + private ImmutableArray<byte> _lazySHA256Hash; |
| 47 | + private ImmutableArray<byte> _lazySHA384Hash; |
| 48 | + private ImmutableArray<byte> _lazySHA512Hash; |
| 49 | + private ImmutableArray<byte> _lazyMD5Hash; |
| 50 | + |
| 51 | + internal abstract ImmutableArray<byte> ComputeHash(HashAlgorithm algorithm); |
| 52 | + |
| 53 | + internal ImmutableArray<byte> GetHash(AssemblyHashAlgorithm algorithmId) |
| 54 | + { |
| 55 | + using (HashAlgorithm? algorithm = TryGetAlgorithm(algorithmId)) |
| 56 | + { |
| 57 | + // ERR_CryptoHashFailed has already been reported: |
| 58 | + if (algorithm == null) |
| 59 | + { |
| 60 | + return ImmutableArray.Create<byte>(); |
| 61 | + } |
| 62 | + |
| 63 | + switch (algorithmId) |
| 64 | + { |
| 65 | + case AssemblyHashAlgorithm.None: |
| 66 | + case AssemblyHashAlgorithm.Sha1: |
| 67 | + return GetHash(ref _lazySHA1Hash, algorithm); |
| 68 | + |
| 69 | + case AssemblyHashAlgorithm.Sha256: |
| 70 | + return GetHash(ref _lazySHA256Hash, algorithm); |
| 71 | + |
| 72 | + case AssemblyHashAlgorithm.Sha384: |
| 73 | + return GetHash(ref _lazySHA384Hash, algorithm); |
| 74 | + |
| 75 | + case AssemblyHashAlgorithm.Sha512: |
| 76 | + return GetHash(ref _lazySHA512Hash, algorithm); |
| 77 | + |
| 78 | + case AssemblyHashAlgorithm.MD5: |
| 79 | + return GetHash(ref _lazyMD5Hash, algorithm); |
| 80 | + |
| 81 | + default: |
| 82 | + throw new ArgumentException("algorithmId"); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + internal static int GetHashSize(SourceHashAlgorithm algorithmId) |
| 88 | + { |
| 89 | + switch (algorithmId) |
| 90 | + { |
| 91 | + case SourceHashAlgorithm.Sha1: |
| 92 | + return 160 / 8; |
| 93 | + |
| 94 | + case SourceHashAlgorithm.Sha256: |
| 95 | + return 256 / 8; |
| 96 | + |
| 97 | + default: |
| 98 | + throw new ArgumentException("algorithmId"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + internal static HashAlgorithm? TryGetAlgorithm(SourceHashAlgorithm algorithmId) |
| 103 | + { |
| 104 | + switch (algorithmId) |
| 105 | + { |
| 106 | + case SourceHashAlgorithm.Sha1: |
| 107 | + return SHA1.Create(); |
| 108 | + |
| 109 | + case SourceHashAlgorithm.Sha256: |
| 110 | + return SHA256.Create(); |
| 111 | + |
| 112 | + default: |
| 113 | + return null; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + internal static HashAlgorithmName GetAlgorithmName(SourceHashAlgorithm algorithmId) |
| 118 | + { |
| 119 | + switch (algorithmId) |
| 120 | + { |
| 121 | + case SourceHashAlgorithm.Sha1: |
| 122 | + return HashAlgorithmName.SHA1; |
| 123 | + |
| 124 | + case SourceHashAlgorithm.Sha256: |
| 125 | + return HashAlgorithmName.SHA256; |
| 126 | + |
| 127 | + default: |
| 128 | + throw new ArgumentException("algorithmId"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + internal static HashAlgorithm? TryGetAlgorithm(AssemblyHashAlgorithm algorithmId) |
| 133 | + { |
| 134 | + switch (algorithmId) |
| 135 | + { |
| 136 | + case AssemblyHashAlgorithm.None: |
| 137 | + case AssemblyHashAlgorithm.Sha1: |
| 138 | + return SHA1.Create(); |
| 139 | + |
| 140 | + case AssemblyHashAlgorithm.Sha256: |
| 141 | + return SHA256.Create(); |
| 142 | + |
| 143 | + case AssemblyHashAlgorithm.Sha384: |
| 144 | + return SHA384.Create(); |
| 145 | + |
| 146 | + case AssemblyHashAlgorithm.Sha512: |
| 147 | + return SHA512.Create(); |
| 148 | + |
| 149 | + case AssemblyHashAlgorithm.MD5: |
| 150 | + return MD5.Create(); |
| 151 | + |
| 152 | + default: |
| 153 | + return null; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + internal static bool IsSupportedAlgorithm(AssemblyHashAlgorithm algorithmId) |
| 158 | + { |
| 159 | + switch (algorithmId) |
| 160 | + { |
| 161 | + case AssemblyHashAlgorithm.None: |
| 162 | + case AssemblyHashAlgorithm.Sha1: |
| 163 | + case AssemblyHashAlgorithm.Sha256: |
| 164 | + case AssemblyHashAlgorithm.Sha384: |
| 165 | + case AssemblyHashAlgorithm.Sha512: |
| 166 | + case AssemblyHashAlgorithm.MD5: |
| 167 | + return true; |
| 168 | + |
| 169 | + default: |
| 170 | + return false; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private ImmutableArray<byte> GetHash(ref ImmutableArray<byte> lazyHash, HashAlgorithm algorithm) |
| 175 | + { |
| 176 | + if (lazyHash.IsDefault) |
| 177 | + { |
| 178 | + ImmutableInterlocked.InterlockedCompareExchange(ref lazyHash, ComputeHash(algorithm), default(ImmutableArray<byte>)); |
| 179 | + } |
| 180 | + |
| 181 | + return lazyHash; |
| 182 | + } |
| 183 | + |
| 184 | + internal const int Sha1HashSize = 20; |
| 185 | + |
| 186 | + internal static ImmutableArray<byte> ComputeSha1(Stream stream) |
| 187 | + { |
| 188 | + if (stream != null) |
| 189 | + { |
| 190 | + stream.Seek(0, SeekOrigin.Begin); |
| 191 | + using (var hashProvider = SHA1.Create()) |
| 192 | + { |
| 193 | + return ImmutableArray.Create(hashProvider.ComputeHash(stream)); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + return ImmutableArray<byte>.Empty; |
| 198 | + } |
| 199 | + |
| 200 | + internal static ImmutableArray<byte> ComputeSha1(ImmutableArray<byte> bytes) |
| 201 | + { |
| 202 | + return ComputeSha1(bytes.ToArray()); |
| 203 | + } |
| 204 | + |
| 205 | + internal static ImmutableArray<byte> ComputeSha1(byte[] bytes) |
| 206 | + { |
| 207 | + using (var hashProvider = SHA1.Create()) |
| 208 | + { |
| 209 | + return ImmutableArray.Create(hashProvider.ComputeHash(bytes)); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + internal static ImmutableArray<byte> ComputeHash(HashAlgorithmName algorithmName, IEnumerable<Blob> bytes) |
| 214 | + { |
| 215 | + using (var incrementalHash = IncrementalHash.CreateHash(algorithmName)) |
| 216 | + { |
| 217 | + foreach (var blob in bytes) |
| 218 | + { |
| 219 | + incrementalHash.AppendData(blob.GetBytes()); |
| 220 | + } |
| 221 | + return ImmutableArray.Create(incrementalHash.GetHashAndReset()); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + internal static ImmutableArray<byte> ComputeHash(HashAlgorithmName algorithmName, IEnumerable<ArraySegment<byte>> bytes) |
| 226 | + { |
| 227 | + using (var incrementalHash = IncrementalHash.CreateHash(algorithmName)) |
| 228 | + { |
| 229 | + foreach (var segment in bytes) |
| 230 | + { |
| 231 | + incrementalHash.AppendData(segment); |
| 232 | + } |
| 233 | + return ImmutableArray.Create(incrementalHash.GetHashAndReset()); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + internal static ImmutableArray<byte> ComputeSourceHash(ImmutableArray<byte> bytes, SourceHashAlgorithm hashAlgorithm = SourceHashAlgorithmUtils.DefaultContentHashAlgorithm) |
| 238 | + { |
| 239 | + var algorithmName = GetAlgorithmName(hashAlgorithm); |
| 240 | + using (var incrementalHash = IncrementalHash.CreateHash(algorithmName)) |
| 241 | + { |
| 242 | + incrementalHash.AppendData(bytes.ToArray()); |
| 243 | + return ImmutableArray.Create(incrementalHash.GetHashAndReset()); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + internal static ImmutableArray<byte> ComputeSourceHash(IEnumerable<Blob> bytes, SourceHashAlgorithm hashAlgorithm = SourceHashAlgorithmUtils.DefaultContentHashAlgorithm) |
| 248 | + { |
| 249 | + return ComputeHash(GetAlgorithmName(hashAlgorithm), bytes); |
| 250 | + } |
| 251 | + } |
| 252 | +} |
0 commit comments