Skip to content

Commit b9b1b98

Browse files
committed
Feat: ToGuidAsHashed
1 parent cecf488 commit b9b1b98

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/CodeOfChaos.Extensions/StringExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// ---------------------------------------------------------------------------------------------------------------------
44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
6+
using System.Security.Cryptography;
7+
using System.Text;
68

79
// ReSharper disable once CheckNamespace
810
namespace System;
@@ -30,4 +32,16 @@ public static Guid ToGuid(this string input) {
3032
return Guid.Parse(input);
3133
#endif
3234
}
35+
36+
/// <summary>
37+
/// Converts a given string into a GUID by hashing it using the SHA256 algorithm.
38+
/// </summary>
39+
/// <param name="input">The input string to hash and convert into a GUID.</param>
40+
/// <returns>A <see cref="Guid"/> created from the hashed value of the input string.</returns>
41+
public static Guid ToGuidAsHashed(this string input) {
42+
byte[] hash = SHA256.HashData(Encoding.UTF8.GetBytes(input));
43+
44+
// Use the first 16 bytes of the hash to create a GUID
45+
return new Guid(hash.Take(16).ToArray());
46+
}
3347
}

0 commit comments

Comments
 (0)