Skip to content

Commit 59af762

Browse files
committed
feat: Add a new string hash generator tool and its navigation link to the navigation menu.
1 parent e290461 commit 59af762

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

src/BlazorApps/Layout/NavMenu.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<!-- <FluentNavLink Href="weather" Icon="@(new Icons.Regular.Size20.WeatherPartlyCloudyDay())" IconColor="Color.Accent">Weather</FluentNavLink> -->
99
<FluentNavLink Href="tools/guid" Icon="@(new Icons.Regular.Size20.Key())" IconColor="Color.Accent">GUID Generator</FluentNavLink>
1010
<FluentNavLink Href="tools/base64" Icon="@(new Icons.Regular.Size20.ArrowSwap())" IconColor="Color.Accent">Base64 Converter</FluentNavLink>
11+
<FluentNavLink Href="tools/string-hash" Icon="@(new Icons.Regular.Size20.LockClosed())" IconColor="Color.Accent">String Hash</FluentNavLink>
1112
</FluentNavMenu>
1213
</nav>
1314
</div>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
@page "/tools/string-hash"
2+
@using System.Security.Cryptography
3+
@using System.Text
4+
@using BlazorApps.Services
5+
@inject IClipboardService ClipboardService
6+
7+
<PageTitle>String Hash Generator</PageTitle>
8+
9+
<h1>String Hash Generator</h1>
10+
11+
<p>Generate cryptographic hash from a string.</p>
12+
13+
<FluentStack Orientation="Orientation.Vertical" VerticalGap="20">
14+
<FluentCard>
15+
<FluentStack Orientation="Orientation.Vertical" VerticalGap="15">
16+
<FluentLabel>Input Text</FluentLabel>
17+
<FluentTextArea @bind-Value="inputText" Rows="5" Style="width: 100%; font-family: monospace;" Placeholder="Enter text to hash..." />
18+
19+
<FluentSelect Items="@algorithms"
20+
OptionText="@(i => i)"
21+
@bind-Value="@selectedAlgorithm"
22+
Label="Hash Algorithm"
23+
Style="min-width: 200px;" />
24+
25+
<FluentStack Orientation="Orientation.Horizontal" VerticalGap="10">
26+
<FluentButton Appearance="Appearance.Accent" OnClick="GenerateHash">Generate Hash</FluentButton>
27+
@if (!string.IsNullOrEmpty(outputHash))
28+
{
29+
<FluentButton Appearance="Appearance.Neutral" OnClick="CopyToClipboard" IconStart="@(new Icons.Regular.Size16.Copy())">Copy Hash</FluentButton>
30+
}
31+
</FluentStack>
32+
</FluentStack>
33+
</FluentCard>
34+
35+
@if (!string.IsNullOrEmpty(outputHash))
36+
{
37+
<FluentLabel>Output Hash</FluentLabel>
38+
<FluentTextArea Value="@outputHash" ReadOnly="true" Rows="3" Style="width: 100%; font-family: monospace;" />
39+
}
40+
</FluentStack>
41+
42+
@code {
43+
private string inputText = "";
44+
private string outputHash = "";
45+
private string selectedAlgorithm = "SHA-256";
46+
47+
private List<string> algorithms = new() { "SHA-1", "SHA-256", "SHA-384", "SHA-512" };
48+
49+
private void GenerateHash()
50+
{
51+
if (string.IsNullOrEmpty(inputText))
52+
{
53+
outputHash = "";
54+
return;
55+
}
56+
57+
byte[] inputBytes = Encoding.UTF8.GetBytes(inputText);
58+
byte[] hashBytes;
59+
60+
using (HashAlgorithm algorithm = selectedAlgorithm switch
61+
{
62+
"MD5" => MD5.Create(),
63+
"SHA-1" => SHA1.Create(),
64+
"SHA-256" => SHA256.Create(),
65+
"SHA-384" => SHA384.Create(),
66+
"SHA-512" => SHA512.Create(),
67+
_ => throw new NotImplementedException()
68+
})
69+
{
70+
hashBytes = algorithm.ComputeHash(inputBytes);
71+
}
72+
73+
outputHash = Convert.ToHexString(hashBytes).ToLowerInvariant();
74+
}
75+
76+
private async Task CopyToClipboard()
77+
{
78+
if (!string.IsNullOrEmpty(outputHash))
79+
{
80+
await ClipboardService.WriteTextAsync(outputHash);
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)