|
| 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