mirror of
https://github.com/tonytins/quickdb.git
synced 2025-04-29 16:41:42 -04:00
44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
namespace QuickDB;
|
|
|
|
public static class DemoDB
|
|
{
|
|
public static void CreateFetch(string value)
|
|
{
|
|
// Prompt the user for input
|
|
Console.WriteLine($"Stored value: {value}");
|
|
|
|
// Hash the input string
|
|
string hashedKey = ComputeSHA256Hash(value);
|
|
|
|
// Store the hashed key and original input value in a dictionary
|
|
Dictionary<string, string> hashDictionary = new Dictionary<string, string>();
|
|
hashDictionary[hashedKey] = value;
|
|
|
|
// Display the hashed key
|
|
Console.WriteLine($"Hashed Key: {hashedKey}");
|
|
|
|
// Retrieve the original input value using the hashed key
|
|
if (hashDictionary.TryGetValue(hashedKey, out string retrievedValue))
|
|
Console.WriteLine($"Retrieved Value: {retrievedValue}");
|
|
else
|
|
Console.WriteLine("Key not found in dictionary.");
|
|
}
|
|
|
|
// Method to compute SHA256 hash from a string
|
|
static string ComputeSHA256Hash(string rawData)
|
|
{
|
|
// Create a SHA256 instance.
|
|
using SHA256 sha256Hash = SHA256.Create();
|
|
|
|
// Compute the hash of the input string.
|
|
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
|
|
|
|
// Convert byte array to a string (hexadecimal).
|
|
StringBuilder builder = new StringBuilder();
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
{
|
|
builder.Append(bytes[i].ToString("x2"));
|
|
}
|
|
return builder.ToString();
|
|
}
|
|
}
|