Skip to content

Commit dce1f09

Browse files
committed
Use element return instead of tuple
Also remove early null check on Get###Hash methods
1 parent c06d196 commit dce1f09

File tree

1 file changed

+20
-44
lines changed
  • CollapseLauncher/Classes/Helper

1 file changed

+20
-44
lines changed

CollapseLauncher/Classes/Helper/Hash.cs

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,26 @@ public static partial class Hash
3434
{ typeof(HMACSHA3_512).GetHashCode(), key => new HMACSHA3_512(key) }
3535
};
3636

37-
private static readonly Dictionary<int, Tuple<HashAlgorithm?, Lock>> CryptoHashDictShared = new()
37+
private static readonly Dictionary<int, (HashAlgorithm?, Lock)> CryptoHashDictShared = new()
3838
{
39-
{ typeof(MD5).GetHashCode(), new Tuple<HashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(MD5.Create), new Lock()) },
40-
{ typeof(SHA1).GetHashCode(), new Tuple<HashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(SHA1.Create), new Lock()) },
41-
{ typeof(SHA256).GetHashCode(), new Tuple<HashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(SHA256.Create), new Lock()) },
42-
{ typeof(SHA384).GetHashCode(), new Tuple<HashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(SHA384.Create), new Lock()) },
43-
{ typeof(SHA512).GetHashCode(), new Tuple<HashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(SHA512.Create), new Lock()) },
44-
{ typeof(SHA3_256).GetHashCode(), new Tuple<HashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(SHA3_256.Create), new Lock()) },
45-
{ typeof(SHA3_384).GetHashCode(), new Tuple<HashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(SHA3_384.Create), new Lock()) },
46-
{ typeof(SHA3_512).GetHashCode(), new Tuple<HashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(SHA3_512.Create), new Lock()) }
39+
{ typeof(MD5).GetHashCode(), (CreateHashAndNullIfUnsupported(MD5.Create), new Lock()) },
40+
{ typeof(SHA1).GetHashCode(), (CreateHashAndNullIfUnsupported(SHA1.Create), new Lock()) },
41+
{ typeof(SHA256).GetHashCode(), (CreateHashAndNullIfUnsupported(SHA256.Create), new Lock()) },
42+
{ typeof(SHA384).GetHashCode(), (CreateHashAndNullIfUnsupported(SHA384.Create), new Lock()) },
43+
{ typeof(SHA512).GetHashCode(), (CreateHashAndNullIfUnsupported(SHA512.Create), new Lock()) },
44+
{ typeof(SHA3_256).GetHashCode(), (CreateHashAndNullIfUnsupported(SHA3_256.Create), new Lock()) },
45+
{ typeof(SHA3_384).GetHashCode(), (CreateHashAndNullIfUnsupported(SHA3_384.Create), new Lock()) },
46+
{ typeof(SHA3_512).GetHashCode(), (CreateHashAndNullIfUnsupported(SHA3_512.Create), new Lock()) }
4747
};
4848

49-
private static readonly Dictionary<int, Tuple<NonCryptographicHashAlgorithm?, Lock>> HashDictShared = new()
49+
private static readonly Dictionary<int, (NonCryptographicHashAlgorithm?, Lock)> HashDictShared = new()
5050
{
51-
{ typeof(Crc32).GetHashCode(), new Tuple<NonCryptographicHashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(() => new Crc32()), new Lock()) },
52-
{ typeof(Crc64).GetHashCode(), new Tuple<NonCryptographicHashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(() => new Crc64()), new Lock()) },
53-
{ typeof(XxHash3).GetHashCode(), new Tuple<NonCryptographicHashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(() => new XxHash3()), new Lock()) },
54-
{ typeof(XxHash32).GetHashCode(), new Tuple<NonCryptographicHashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(() => new XxHash32()), new Lock()) },
55-
{ typeof(XxHash64).GetHashCode(), new Tuple<NonCryptographicHashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(() => new XxHash64()), new Lock()) },
56-
{ typeof(XxHash128).GetHashCode(), new Tuple<NonCryptographicHashAlgorithm?, Lock>(CreateHashAndNullIfUnsupported(() => new XxHash128()), new Lock()) }
51+
{ typeof(Crc32).GetHashCode(), (CreateHashAndNullIfUnsupported(() => new Crc32()), new Lock()) },
52+
{ typeof(Crc64).GetHashCode(), (CreateHashAndNullIfUnsupported(() => new Crc64()), new Lock()) },
53+
{ typeof(XxHash3).GetHashCode(), (CreateHashAndNullIfUnsupported(() => new XxHash3()), new Lock()) },
54+
{ typeof(XxHash32).GetHashCode(), (CreateHashAndNullIfUnsupported(() => new XxHash32()), new Lock()) },
55+
{ typeof(XxHash64).GetHashCode(), (CreateHashAndNullIfUnsupported(() => new XxHash64()), new Lock()) },
56+
{ typeof(XxHash128).GetHashCode(), (CreateHashAndNullIfUnsupported(() => new XxHash128()), new Lock()) }
5757
};
5858

5959
private static T? CreateHashAndNullIfUnsupported<T>(Func<T> delegateCreate)
@@ -82,12 +82,6 @@ public static HashAlgorithm CreateCryptoHash<T>()
8282
ref Func<HashAlgorithm> createHashDelegate = ref CollectionsMarshal
8383
.GetValueRefOrNullRef(CryptoHashDict, typeof(T).GetHashCode());
8484

85-
// If the delegate is null, then throw an exception
86-
if (createHashDelegate == null)
87-
{
88-
throw new NotSupportedException($"Cannot create hash algorithm instance from {typeof(T)}.");
89-
}
90-
9185
// Create the hash algorithm instance
9286
return createHashDelegate();
9387
}
@@ -106,12 +100,6 @@ public static HashAlgorithm CreateHmacCryptoHash<T>(byte[] key)
106100
ref Func<byte[], HashAlgorithm> createHashDelegate = ref CollectionsMarshal
107101
.GetValueRefOrNullRef(CryptoHmacHashDict, typeof(T).GetHashCode());
108102

109-
// If the delegate is null, then throw an exception
110-
if (createHashDelegate == null)
111-
{
112-
throw new NotSupportedException($"Cannot create HMAC-based hash algorithm instance from {typeof(T)}.");
113-
}
114-
115103
// Create the hash algorithm instance
116104
return createHashDelegate(key);
117105
}
@@ -130,19 +118,13 @@ public static NonCryptographicHashAlgorithm CreateHash<T>()
130118
/// <typeparam name="T">The type of the non-cryptographic hash algorithm to use.</typeparam>
131119
/// <returns>A tuple of the non-cryptographic hash algorithm and the thread <see cref="Lock"/> instance.</returns>
132120
/// <exception cref="NotSupportedException">Thrown when the specified non-cryptographic hash algorithm type is not supported.</exception>
133-
public static ref Tuple<NonCryptographicHashAlgorithm?, Lock> GetSharedHash<T>()
121+
public static ref (NonCryptographicHashAlgorithm? Hash, Lock Lock) GetSharedHash<T>()
134122
where T : NonCryptographicHashAlgorithm
135123
{
136124
// Get reference from the dictionary
137-
ref Tuple<NonCryptographicHashAlgorithm?, Lock> hash = ref CollectionsMarshal
125+
ref (NonCryptographicHashAlgorithm? Hash, Lock Lock) hash = ref CollectionsMarshal
138126
.GetValueRefOrNullRef(HashDictShared, typeof(T).GetHashCode());
139127

140-
// If the tuple is null, then throw an exception
141-
if (hash == null || hash.Item1 == null)
142-
{
143-
throw new NotSupportedException($"Cannot create HMAC-based hash algorithm instance from {typeof(T)}.");
144-
}
145-
146128
// Return the tuple reference
147129
return ref hash;
148130
}
@@ -153,19 +135,13 @@ public static NonCryptographicHashAlgorithm CreateHash<T>()
153135
/// <typeparam name="T">The type of the cryptographic hash algorithm to use.</typeparam>
154136
/// <returns>A tuple of the cryptographic hash algorithm and the thread <see cref="Lock"/> instance.</returns>
155137
/// <exception cref="NotSupportedException">Thrown when the specified cryptographic hash algorithm type is not supported.</exception>
156-
public static ref Tuple<HashAlgorithm?, Lock> GetSharedCryptoHash<T>()
138+
public static ref (HashAlgorithm? Hash, Lock Lock) GetSharedCryptoHash<T>()
157139
where T : HashAlgorithm
158140
{
159141
// Get reference from the dictionary
160-
ref Tuple<HashAlgorithm?, Lock> hash = ref CollectionsMarshal
142+
ref (HashAlgorithm? Hash, Lock Lock) hash = ref CollectionsMarshal
161143
.GetValueRefOrNullRef(CryptoHashDictShared, typeof(T).GetHashCode());
162144

163-
// If the tuple is null, then throw an exception
164-
if (hash == null || hash.Item1 == null)
165-
{
166-
throw new NotSupportedException($"Cannot create HMAC-based hash algorithm instance from {typeof(T)}.");
167-
}
168-
169145
// Return the tuple reference
170146
return ref hash;
171147
}

0 commit comments

Comments
 (0)