-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringHashing.cs
More file actions
59 lines (53 loc) · 2.52 KB
/
StringHashing.cs
File metadata and controls
59 lines (53 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Security.Cryptography;
using System.Text;
namespace DKNet.Fw.Extensions.Encryption;
/// <summary>
/// Provides string hashing utilities for common security operations.
/// </summary>
/// <remarks>
/// Purpose: To provide a set of utility methods for hashing strings.
/// Rationale: Ensures data integrity and security by providing hashing algorithms.
/// Functionality:
/// - Generates HMAC-SHA256 hashes with a specified key
/// - Generates SHA256 hashes
/// Integration:
/// - Can be used as an extension method for string manipulation
/// - Works with other encryption utilities in the framework
/// Best Practices:
/// - Use strong, randomly generated keys for HMAC-SHA256
/// - Store hashes securely, not the original data
/// - Use SHA256 for general-purpose hashing
/// - Validate input strings before hashing
/// </remarks>
public static class StringHashing
{
/// <summary>
/// Generates an HMAC-SHA256 hash for the provided value using the specified key.
/// </summary>
/// <param name="value">The input string to be hashed.</param>
/// <param name="key">The secret key used for hashing.</param>
/// <returns>A hexadecimal string representing the HMAC-SHA256 hash of the input value.</returns>
/// <exception cref="ArgumentException">Thrown when value is null or empty.</exception>
public static string ToCmd5(this string value, string key)
{
ArgumentException.ThrowIfNullOrWhiteSpace(value);
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(value));
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
/// <summary>
/// Generates a SHA256 hash for the provided value.
/// </summary>
/// <param name="value">The input string to be hashed.</param>
/// <returns>A hexadecimal string representing the SHA256 hash of the input value.</returns>
/// <exception cref="ArgumentException">Thrown when value is null or empty.</exception>
public static string ToSha256(this string value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(value);
// Convert the input string to a byte array and compute the hash using SHA256.HashData
var inputBytes = Encoding.UTF8.GetBytes(value);
var hashBytes = SHA256.HashData(inputBytes);
// Use built-in .NET framework method for hex conversion
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
}
}