Guest User

Untitled

a guest
Jun 15th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. Sub DemoHash()
  2. Dim d As New Encryption.Data( _
  3. "{ts '2004-10-09 08:10:04'}The world is beautiful and needs caring by its children")
  4.  
  5. Dim hash As New Encryption.Hash(Encryption.Hash.Provider.SHA1)
  6. Dim hash2 As New Encryption.Hash(Encryption.Hash.Provider.SHA256)
  7. Dim hash3 As New Encryption.Hash(Encryption.Hash.Provider.SHA384)
  8. Dim hash4 As New Encryption.Hash(Encryption.Hash.Provider.SHA512)
  9. Dim hash5 As New Encryption.Hash(Encryption.Hash.Provider.MD5)
  10. Dim hash6 As New Encryption.Hash(Encryption.Hash.Provider.CRC32)
  11.  
  12. hash.Calculate(d)
  13. hash2.Calculate(d)
  14. hash3.Calculate(d)
  15. hash4.Calculate(d)
  16. hash5.Calculate(d)
  17.  
  18. Console.WriteLine("SHA1: " & hash.Value.Hex)
  19. Console.WriteLine("SHA256: " & hash2.Value.Hex)
  20. Console.WriteLine("SHA384: " & hash3.Value.Hex)
  21. Console.WriteLine("SHA512: " & hash4.Value.Hex)
  22. Console.WriteLine("MD5: " & hash5.Value.Hex)
  23. Console.WriteLine("CRC32: " & hash6.Calculate(d).Hex)
  24. Console.WriteLine()
  25.  
  26. Dim salt As New Encryption.Data("salty!")
  27. Console.WriteLine("Salted CRC32: " & hash6.Calculate(d, salt).Hex)
  28.  
  29. Console.WriteLine("Press ENTER to continue...")
  30. Console.ReadLine()
  31. End Sub
  32.  
  33. Dim sUserName = "barry"
  34. Dim sPassWord = "fishlegs"
  35. Dim mySalt = "A deliciously salty string! fillED WIth all KindS of Junkk(&^&*(£"
  36. Dim d As New Encryption.Data(mySalt + sUserName + sPassWord)
  37. Dim hash As New Encryption.Hash(Encryption.Hash.Provider.SHA256)
  38. hash.Calculate(d)
  39. Dim sTheSaltedHashedUnPassCombination = hash.Value.Hex;
  40. SavenewPasswordHashToDatabase(sTheSaltedHashedUnPassCombination)
  41.  
  42. Dim sUserName = "barry"
  43. Dim sPassWord = "fishlegs"
  44. Dim mySalt = "A deliciously salty string! fillED WIth all KindS of Junkk(&^&*(£"
  45. Dim d As New Encryption.Data(mySalt + sUserName + sPassWord)
  46. Dim hash As New Encryption.Hash(Encryption.Hash.Provider.SHA256)
  47. hash.Calculate(d)
  48. Dim sTheSaltedHashedUnPassCombination = hash.Value.Hex;
  49. SavenewPasswordHashToDatabase(sTheSaltedHashedUnPassCombination)
  50.  
  51. Dim sThePreviouslyCreatedHashFromTheDatabase = GetHashForUserFromDatabase(usernameProvided)
  52. Dim mySalt = "A deliciously salty string! fillED WIth all KindS of Junkk(&^&*(£"
  53. Dim d As New Encryption.Data(mySalt + usernameProvided+ passwordProvided)
  54. Dim hash As New Encryption.Hash(Encryption.Hash.Provider.SHA256)
  55. hash.Calculate(d)
  56. Dim sTheSaltedHashedUnPassCombination = hash.Value.Hex;
  57. if (sThePreviouslyCreatedHashFromTheDatabase.Equals(sTheSaltedHashedUnPassCombination))
  58. 'UN & Password Valid!
  59. else
  60. 'UN & PW Invalid!
  61. end
  62.  
  63. public static byte[] GetRandomSalt()
  64. {
  65. int minSaltSize = 16;
  66. int maxSaltSize = 32;
  67.  
  68. Random random = new Random();
  69. int saltSize = random.Next(minSaltSize, maxSaltSize);
  70. saltBytes = new byte[saltSize];
  71. RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
  72. rng.GetNonZeroBytes(saltBytes);
  73. return saltBytes;
  74. }
  75.  
  76. public static byte[] ComputeHash(string plainText)
  77. {
  78. byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
  79. HashAlgorithm hash = new SHA256Managed();
  80. return hash.ComputeHash(plainTextWithSaltBytes);
  81. }
  82.  
  83. byte[] passwordHash = ComputeHash(password);
  84. byte[] salt = GetRandomSalt();
  85. byte[] saltHash = ComputeHash(salt);
  86.  
  87. byte[] hashWithSaltBytes = new byte[hashBytes.Length + saltBytes.Length];
  88. for (int i=0; i < hashBytes.Length; i++)
  89. hashWithSaltBytes[i] = hashBytes[i];
  90. for (int i=0; i < saltBytes.Length; i++)
  91. hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];
  92.  
  93. string hashValue = Convert.ToBase64String(hashWithSaltBytes);
Add Comment
Please, Sign In to add comment