fork(2) download
  1. private static string GenerateSaltValue()
  2. {
  3. UnicodeEncoding utf16 = new UnicodeEncoding();
  4.  
  5. if (utf16 != null)
  6. {
  7. // Create a random number object seeded from the value
  8. // of the last random seed value. This is done
  9. // interlocked because it is a static value and we want
  10. // it to roll forward safely.
  11.  
  12. Random random = new Random(unchecked((int)DateTime.Now.Ticks));
  13.  
  14. if (random != null)
  15. {
  16. // Create an array of random values.
  17.  
  18. byte[] saltValue = new byte[SaltValueSize];
  19.  
  20. random.NextBytes(saltValue);
  21.  
  22. // Convert the salt value to a string. Note that the resulting string
  23. // will still be an array of binary values and not a printable string.
  24. // Also it does not convert each byte to a double byte.
  25.  
  26. string saltValueString = utf16.GetString(saltValue);
  27.  
  28. // Return the salt value as a string.
  29.  
  30. return saltValueString;
  31. }
  32. }
  33.  
  34. return null;
  35. }
  36.  
  37. private static string HashPassword(string clearData, string saltValue, HashAlgorithm hash)
  38. {
  39. UnicodeEncoding encoding = new UnicodeEncoding();
  40.  
  41. if (clearData != null && hash != null && encoding != null)
  42. {
  43. // If the salt string is null or the length is invalid then
  44. // create a new valid salt value.
  45.  
  46. if (saltValue == null)
  47. {
  48. // Generate a salt string.
  49. saltValue = GenerateSaltValue();
  50. }
  51.  
  52. // Convert the salt string and the password string to a single
  53. // array of bytes. Note that the password string is Unicode and
  54. // therefore may or may not have a zero in every other byte.
  55.  
  56. byte[] binarySaltValue = new byte[SaltValueSize];
  57.  
  58. binarySaltValue[0] = byte.Parse(saltValue.Substring(0, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
  59. binarySaltValue[1] = byte.Parse(saltValue.Substring(2, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
  60. binarySaltValue[2] = byte.Parse(saltValue.Substring(4, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
  61. binarySaltValue[3] = byte.Parse(saltValue.Substring(6, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
  62.  
  63. byte[] valueToHash = new byte[SaltValueSize + encoding.GetByteCount(clearData)];
  64. byte[] binaryPassword = encoding.GetBytes(clearData);
  65.  
  66. // Copy the salt value and the password to the hash buffer.
  67.  
  68. binarySaltValue.CopyTo(valueToHash, 0);
  69. binaryPassword.CopyTo(valueToHash, SaltValueSize);
  70.  
  71. byte[] hashValue = hash.ComputeHash(valueToHash);
  72.  
  73. // The hashed password is the salt plus the hash value (as a string).
  74.  
  75. string hashedPassword = saltValue;
  76.  
  77. foreach (byte hexdigit in hashValue)
  78. {
  79. hashedPassword += hexdigit.ToString("X2", CultureInfo.InvariantCulture.NumberFormat);
  80. }
  81.  
  82. // Return the hashed password as a string.
  83.  
  84. return hashedPassword;
  85. }
  86.  
  87. return null;
  88. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(1,23): error CS0116: A namespace can only contain types and namespace declarations
prog.cs(37,23): error CS0116: A namespace can only contain types and namespace declarations
Compilation failed: 2 error(s), 0 warnings
stdout
Standard output is empty