Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HashProviderDispenser.cs
Go to the documentation of this file.
1using System;
5
7
8internal static class HashProviderDispenser
9{
10 public static class OneShotHashProvider
11 {
12 public static int MacData(string hashAlgorithmId, ReadOnlySpan<byte> key, ReadOnlySpan<byte> source, Span<byte> destination)
13 {
14 int hashSizeInBytes;
15 if (global::Interop.BCrypt.PseudoHandlesSupported)
16 {
17 HashDataUsingPseudoHandle(hashAlgorithmId, source, key, isHmac: true, destination, out hashSizeInBytes);
18 return hashSizeInBytes;
19 }
20 SafeBCryptAlgorithmHandle cachedBCryptAlgorithmHandle = global::Interop.BCrypt.BCryptAlgorithmCache.GetCachedBCryptAlgorithmHandle(hashAlgorithmId, global::Interop.BCrypt.BCryptOpenAlgorithmProviderFlags.BCRYPT_ALG_HANDLE_HMAC_FLAG, out hashSizeInBytes);
21 if (destination.Length < hashSizeInBytes)
22 {
23 throw new CryptographicException();
24 }
25 HashUpdateAndFinish(cachedBCryptAlgorithmHandle, hashSizeInBytes, key, source, destination);
26 return hashSizeInBytes;
27 }
28
29 public static int HashData(string hashAlgorithmId, ReadOnlySpan<byte> source, Span<byte> destination)
30 {
31 int hashSizeInBytes;
32 if (global::Interop.BCrypt.PseudoHandlesSupported)
33 {
34 HashDataUsingPseudoHandle(hashAlgorithmId, source, default(ReadOnlySpan<byte>), isHmac: false, destination, out hashSizeInBytes);
35 return hashSizeInBytes;
36 }
37 SafeBCryptAlgorithmHandle cachedBCryptAlgorithmHandle = global::Interop.BCrypt.BCryptAlgorithmCache.GetCachedBCryptAlgorithmHandle(hashAlgorithmId, global::Interop.BCrypt.BCryptOpenAlgorithmProviderFlags.None, out hashSizeInBytes);
38 if (destination.Length < hashSizeInBytes)
39 {
40 throw new CryptographicException();
41 }
42 HashUpdateAndFinish(cachedBCryptAlgorithmHandle, hashSizeInBytes, default(ReadOnlySpan<byte>), source, destination);
43 return hashSizeInBytes;
44 }
45
46 private unsafe static void HashDataUsingPseudoHandle(string hashAlgorithmId, ReadOnlySpan<byte> source, ReadOnlySpan<byte> key, bool isHmac, Span<byte> destination, out int hashSize)
47 {
48 hashSize = 0;
49 global::Interop.BCrypt.BCryptAlgPseudoHandle bCryptAlgPseudoHandle;
50 int num;
51 switch (hashAlgorithmId)
52 {
53 case "MD5":
54 bCryptAlgPseudoHandle = (isHmac ? global::Interop.BCrypt.BCryptAlgPseudoHandle.BCRYPT_HMAC_MD5_ALG_HANDLE : global::Interop.BCrypt.BCryptAlgPseudoHandle.BCRYPT_MD5_ALG_HANDLE);
55 num = 16;
56 break;
57 case "SHA1":
58 bCryptAlgPseudoHandle = (isHmac ? global::Interop.BCrypt.BCryptAlgPseudoHandle.BCRYPT_HMAC_SHA1_ALG_HANDLE : global::Interop.BCrypt.BCryptAlgPseudoHandle.BCRYPT_SHA1_ALG_HANDLE);
59 num = 20;
60 break;
61 case "SHA256":
62 bCryptAlgPseudoHandle = (isHmac ? global::Interop.BCrypt.BCryptAlgPseudoHandle.BCRYPT_HMAC_SHA256_ALG_HANDLE : global::Interop.BCrypt.BCryptAlgPseudoHandle.BCRYPT_SHA256_ALG_HANDLE);
63 num = 32;
64 break;
65 case "SHA384":
66 bCryptAlgPseudoHandle = (isHmac ? global::Interop.BCrypt.BCryptAlgPseudoHandle.BCRYPT_HMAC_SHA384_ALG_HANDLE : global::Interop.BCrypt.BCryptAlgPseudoHandle.BCRYPT_SHA384_ALG_HANDLE);
67 num = 48;
68 break;
69 case "SHA512":
70 bCryptAlgPseudoHandle = (isHmac ? global::Interop.BCrypt.BCryptAlgPseudoHandle.BCRYPT_HMAC_SHA512_ALG_HANDLE : global::Interop.BCrypt.BCryptAlgPseudoHandle.BCRYPT_SHA512_ALG_HANDLE);
71 num = 64;
72 break;
73 default:
74 throw new CryptographicException();
75 }
76 if (destination.Length < num)
77 {
78 throw new CryptographicException();
79 }
80 fixed (byte* pbSecret = &MemoryMarshal.GetReference(key))
81 {
82 fixed (byte* pbInput = &MemoryMarshal.GetReference(source))
83 {
84 fixed (byte* pbOutput = &MemoryMarshal.GetReference(destination))
85 {
86 global::Interop.BCrypt.NTSTATUS nTSTATUS = global::Interop.BCrypt.BCryptHash((nuint)bCryptAlgPseudoHandle, pbSecret, key.Length, pbInput, source.Length, pbOutput, num);
87 if (nTSTATUS != 0)
88 {
89 throw global::Interop.BCrypt.CreateCryptographicException(nTSTATUS);
90 }
91 }
92 }
93 }
94 hashSize = num;
95 }
96
98 {
99 global::Interop.BCrypt.NTSTATUS nTSTATUS = global::Interop.BCrypt.BCryptCreateHash(algHandle, out var phHash, IntPtr.Zero, 0, key, key.Length, global::Interop.BCrypt.BCryptCreateHashFlags.None);
100 if (nTSTATUS != 0)
101 {
102 phHash.Dispose();
103 throw global::Interop.BCrypt.CreateCryptographicException(nTSTATUS);
104 }
105 using (phHash)
106 {
107 nTSTATUS = global::Interop.BCrypt.BCryptHashData(phHash, source, source.Length, 0);
108 if (nTSTATUS != 0)
109 {
110 throw global::Interop.BCrypt.CreateCryptographicException(nTSTATUS);
111 }
112 global::Interop.BCrypt.BCryptFinishHash(phHash, destination, hashSize, 0);
113 }
114 }
115 }
116
117 public static HashProvider CreateHashProvider(string hashAlgorithmId)
118 {
119 return new HashProviderCng(hashAlgorithmId, null);
120 }
121
122 public static HashProvider CreateMacProvider(string hashAlgorithmId, ReadOnlySpan<byte> key)
123 {
124 return new HashProviderCng(hashAlgorithmId, key, isHmac: true);
125 }
126}
static int HashData(string hashAlgorithmId, ReadOnlySpan< byte > source, Span< byte > destination)
static int MacData(string hashAlgorithmId, ReadOnlySpan< byte > key, ReadOnlySpan< byte > source, Span< byte > destination)
static unsafe void HashDataUsingPseudoHandle(string hashAlgorithmId, ReadOnlySpan< byte > source, ReadOnlySpan< byte > key, bool isHmac, Span< byte > destination, out int hashSize)
static void HashUpdateAndFinish(SafeBCryptAlgorithmHandle algHandle, int hashSize, ReadOnlySpan< byte > key, ReadOnlySpan< byte > source, Span< byte > destination)
static HashProvider CreateMacProvider(string hashAlgorithmId, ReadOnlySpan< byte > key)
static HashProvider CreateHashProvider(string hashAlgorithmId)
static readonly IntPtr Zero
Definition IntPtr.cs:18