IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
CrypotographicHashProvider.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Collections.Immutable;
4using System.IO;
5using System.Linq;
6using System.Reflection.Metadata;
7using System.Security.Cryptography;
8
10
11#nullable enable
12
14{
15
19 internal class CryptographicHashProvider
20 {
21
22 internal const int Sha1HashSize = 20;
23
24 public static HashAlgorithm? TryGetAlgorithm(AssemblyHashAlgorithm algorithmId)
25 {
26 switch (algorithmId)
27 {
28 case AssemblyHashAlgorithm.None:
29 case AssemblyHashAlgorithm.SHA1:
30 return SHA1.Create();
31 case AssemblyHashAlgorithm.SHA256:
32 return SHA256.Create();
33 case AssemblyHashAlgorithm.SHA384:
34 return SHA384.Create();
35 case AssemblyHashAlgorithm.SHA512:
36 return SHA512.Create();
37 case AssemblyHashAlgorithm.MD5:
38 return MD5.Create();
39 default:
40 return null;
41 }
42 }
43
44 public static bool IsSupportedAlgorithm(AssemblyHashAlgorithm algorithmId)
45 {
46 switch (algorithmId)
47 {
48 case AssemblyHashAlgorithm.None:
49 case AssemblyHashAlgorithm.SHA1:
50 case AssemblyHashAlgorithm.SHA256:
51 case AssemblyHashAlgorithm.SHA384:
52 case AssemblyHashAlgorithm.SHA512:
53 case AssemblyHashAlgorithm.MD5:
54 return true;
55 default:
56 return false;
57 }
58 }
59
60 public static ImmutableArray<byte> ComputeSha1(Stream stream)
61 {
62 if (stream != null)
63 {
64 stream.Seek(0, SeekOrigin.Begin);
65 using (var hashProvider = SHA1.Create())
66 {
67 return ImmutableArray.Create(hashProvider.ComputeHash(stream));
68 }
69 }
70
71 return ImmutableArray<byte>.Empty;
72 }
73
74 public static ImmutableArray<byte> ComputeSha1(ImmutableArray<byte> bytes)
75 {
76 return ComputeSha1(bytes.ToArray());
77 }
78
79 public static ImmutableArray<byte> ComputeSha1(byte[] bytes)
80 {
81 using (var hashProvider = SHA1.Create())
82 {
83 return ImmutableArray.Create(hashProvider.ComputeHash(bytes));
84 }
85 }
86
87 public static ImmutableArray<byte> ComputeHash(HashAlgorithmName algorithmName, IEnumerable<Blob> bytes)
88 {
89 using (var incrementalHash = IncrementalHash.CreateHash(algorithmName))
90 {
91 incrementalHash.AppendData(bytes);
92 return ImmutableArray.Create(incrementalHash.GetHashAndReset());
93 }
94 }
95
96 public static ImmutableArray<byte> ComputeHash(HashAlgorithmName algorithmName, IEnumerable<ArraySegment<byte>> bytes)
97 {
98 using (var incrementalHash = IncrementalHash.CreateHash(algorithmName))
99 {
100 incrementalHash.AppendData(bytes);
101 return ImmutableArray.Create(incrementalHash.GetHashAndReset());
102 }
103 }
104
105 }
106
107}
108
109#nullable restore