fork download
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4.  
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. string input = "password";
  10.  
  11. // Generate SHA-256 hash without salt
  12. string hashWithoutSalt = ComputeSha256Hash(input);
  13. Console.WriteLine($"SHA-256 Hash (No Salt): {hashWithoutSalt}");
  14.  
  15. // Generate salt
  16. byte[] salt = GenerateSalt(32); // 32 bytes = 256 bits
  17. Console.WriteLine($"Generated Salt (Base64): {Convert.ToBase64String(salt)}");
  18.  
  19. // Generate SHA-256 hash with salt
  20. string hashWithSalt = ComputeSha256HashWithSalt(input, salt);
  21. Console.WriteLine($"SHA-256 Hash (With Salt): {hashWithSalt}");
  22. }
  23.  
  24. // SHA-256 hash without salt
  25. static string ComputeSha256Hash(string rawData)
  26. {
  27. using SHA256 sha256Hash = SHA256.Create();
  28. byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
  29. return Convert.ToHexString(bytes);
  30. }
  31.  
  32. // SHA-256 hash with salt
  33. static string ComputeSha256HashWithSalt(string rawData, byte[] salt)
  34. {
  35. using SHA256 sha256Hash = SHA256.Create();
  36. byte[] combined = Combine(Encoding.UTF8.GetBytes(rawData), salt);
  37. byte[] hashBytes = sha256Hash.ComputeHash(combined);
  38. return Convert.ToHexString(hashBytes);
  39. }
  40.  
  41. // Generate a cryptographically secure salt
  42. static byte[] GenerateSalt(int size)
  43. {
  44. byte[] salt = new byte[size];
  45. using RandomNumberGenerator rng = RandomNumberGenerator.Create();
  46. rng.GetBytes(salt);
  47. return salt;
  48. }
  49.  
  50. // Combine byte arrays
  51. static byte[] Combine(byte[] first, byte[] second)
  52. {
  53. byte[] combined = new byte[first.Length + second.Length];
  54. Buffer.BlockCopy(first, 0, combined, 0, first.Length);
  55. Buffer.BlockCopy(second, 0, combined, first.Length, second.Length);
  56. return combined;
  57. }
  58. }
  59.  
Success #stdin #stdout 0.06s 32620KB
stdin
2
aa1
bbb
aa1
1
stdout
SHA-256 Hash (No Salt): 5E884898DA28047151D0E56F8DC6292773603D0D6AABBDD62A11EF721D1542D8
Generated Salt (Base64): s/nuZEos2dJ9V1qBQOb2iKtPL2gdrbxHN2RN3Eaj0Io=
SHA-256 Hash (With Salt): 228A2700DD63C08DD7FA1A42BDF38D435AC7F6630328CA050A61A3490E9CA65D