Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. Imports System.IO
  2. ' And System.Security.Cryptography for Hashes : MD5, SHA1, SHA256, ...
  3. Imports System.Security
  4. Imports System.Security.Cryptography
  5. Module Hash
  6. Function hash_generator(ByVal hash_type As String, ByVal file_name As String)
  7.  
  8. ' We declare the variable : hash
  9. Dim hash
  10. If hash_type.ToLower = "sha256" Then
  11. ' Initializes a SHA-256 hash object
  12. hash = SHA256.Create()
  13. Else
  14. MsgBox("Unknown type of hash : " & hash_type, MsgBoxStyle.Critical)
  15. Return False
  16. End If
  17.  
  18. ' We declare a variable to be an array of bytes
  19. Dim hashValue() As Byte
  20.  
  21. ' We create a FileStream for the file passed as a parameter
  22. Dim fileStream As FileStream = File.OpenRead(file_name)
  23. ' We position the cursor at the beginning of stream
  24. fileStream.Position = 0
  25. ' We calculate the hash of the file
  26. hashValue = hash.ComputeHash(fileStream)
  27. ' The array of bytes is converted into hexadecimal before it can be read easily
  28. Dim hash_hex = PrintByteArray(hashValue)
  29.  
  30. ' We close the open file
  31. fileStream.Close()
  32.  
  33. ' The hash is returned
  34. Return hash_hex
  35.  
  36. End Function
  37. Public Function PrintByteArray(ByVal array() As Byte)
  38.  
  39. Dim hex_value As String = ""
  40.  
  41. ' We traverse the array of bytes
  42. Dim i As Integer
  43. For i = 0 To array.Length - 1
  44.  
  45. ' We convert each byte in hexadecimal
  46. hex_value += array(i).ToString("X2")
  47.  
  48. Next i
  49.  
  50. ' We return the string in lowercase
  51. Return hex_value.ToLower
  52.  
  53. End Function
  54. Function sha_256(ByVal file_name As String)
  55. Return hash_generator("sha256", file_name)
  56. End Function
  57. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement