Note: You’ll have to change valid_hash variable, when you upgrade to new version of Treek’s Licensing Library. You can find actual SHA1 checksum of library in toolbox.

  1. public class LicenseLibraryIntegrityValidator{
  2.  
  3. public bool ValidateLicensingLibrary()
  4. {
  5.  
  6. SHA1 sha = new SHA1();
  7. string path = LibraryDLLPath();
  8.  
  9. string current_hash = sha.hashOfFile(path);
  10. string valid_hash = "8e237b886f21606e9c1a49fafd23ebab380d2697";
  11.  
  12. if (valid_hash == current_hash) {
  13. return true;
  14. } else {
  15. return false;
  16. }
  17.  
  18. }
  19.  
  20. private string LibraryDLLPath()
  21. {
  22.  
  23. string dirpath = IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location);
  24. if (!dirpath.EndsWith("\\"))
  25. dirpath += "\\";
  26.  
  27. string path = dirpath + "TreeksLicensingLibrary.dll";
  28.  
  29. return path;
  30.  
  31. }
  32. }
  33.  
  34. using System;
  35. using System.Collections;
  36. using System.Collections.Generic;
  37. using System.Data;
  38. using System.Diagnostics;
  39. using System.IO;
  40. using System.Security.Cryptography;
  41.  
  42. public class SHA1 : IHasher
  43. {
  44.  
  45. ///
  46. /// Returns SHA1 hash of file
  47. ///
  48.  
  49. ///Filesystem path to hashed file /// String
  50. ///
  51. public string hashOfFile(string fileToHash)
  52. {
  53. FileStream rdr = default(FileStream);
  54. SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
  55. byte[] bytes = null;
  56. string rtn = "";
  57. if (File.Exists(fileToHash)) {
  58. rdr = new FileStream(fileToHash, FileMode.Open, FileAccess.Read);
  59. bytes = sha.ComputeHash(rdr);
  60. rtn = ByteArrayToString(bytes);
  61. }
  62. return rtn;
  63. }
  64.  
  65. ///
  66. /// Returns SHA1 hash of string
  67. ///
  68.  
  69. ///String to be hashed /// String
  70. ///
  71. public string hashOfString(string stringToHash)
  72. {
  73. SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
  74. byte[] bytesToHash = System.Text.Encoding.ASCII.GetBytes(stringToHash);
  75. bytesToHash = sha.ComputeHash(bytesToHash);
  76. string strResult = "";
  77. foreach (byte b in bytesToHash) {
  78. strResult += b.ToString("x2");
  79. }
  80. return strResult;
  81. }
  82.  
  83. private string ByteArrayToString(byte[] arrInput)
  84. {
  85. System.Text.StringBuilder sb = new System.Text.StringBuilder(arrInput.Length * 2);
  86. for (int i = 0; i <= arrInput.Length - 1; i++) {
  87. sb.Append(arrInput(i).ToString("X2"));
  88. }
  89. return sb.ToString().ToLower;
  90. }
  91.  
  92. private interface IHasher
  93. {
  94. string hashOfString(string stringToHash);
  95. string hashOfFile(string fileToHash);
  96. }
  97. }
  98. //End C# example
  1. Public Class LicenseLibraryIntegrityValidator
  2.  
  3. Public Function ValidateLicensingLibrary as Boolean
  4.  
  5. Dim sha As New SHA1
  6. Dim path As String = LibraryDLLPath()
  7.  
  8. Dim current_hash As String = sha.hashOfFile(path)
  9. Dim valid_hash as String = "8e237b886f21606e9c1a49fafd23ebab380d2697"
  10.  
  11. If valid_hash = current_hash Then
  12. Return True
  13. Else
  14. Return False
  15. End If
  16.  
  17. End Function
  18.  
  19. Private Function LibraryDLLPath() As String
  20.  
  21. Dim dirpath As String = IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location)
  22. If Not dirpath.EndsWith("\") Then dirpath &= "\"
  23.  
  24. Dim path As String = dirpath & "TreeksLicensingLibrary.dll"
  25.  
  26. Return path
  27.  
  28. End Function
  29.  
  30. End Class
  31.  
  32. Imports System.IO
  33. Imports System.Security.Cryptography
  34.  
  35. Public Class SHA1
  36. Implements IHasher
  37.  
  38. '''
  39. ''' Returns SHA1 hash of file
  40. '''
  41.  
  42. '''Filesystem path to hashed file ''' String
  43. '''
  44. Public Function hashOfFile(ByVal fileToHash As String) As String Implements IHasher.hashOfFile
  45. Dim rdr As FileStream
  46. Dim sha As New SHA1CryptoServiceProvider
  47. Dim bytes() As Byte
  48. Dim rtn As String = ""
  49. If File.Exists(fileToHash) Then
  50. rdr = New FileStream(fileToHash, FileMode.Open, FileAccess.Read)
  51. bytes = sha.ComputeHash(rdr)
  52. rtn = ByteArrayToString(bytes)
  53. End If
  54. Return rtn
  55. End Function
  56.  
  57. '''
  58. ''' Returns SHA1 hash of string
  59. '''
  60.  
  61. '''String to be hashed ''' String
  62. '''
  63. Public Function hashOfString(ByVal stringToHash As String) As String Implements IHasher.hashOfString
  64. Dim sha As New SHA1CryptoServiceProvider
  65. Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(stringToHash)
  66. bytesToHash = sha.ComputeHash(bytesToHash)
  67. Dim strResult As String = ""
  68. For Each b As Byte In bytesToHash
  69. strResult += b.ToString("x2")
  70. Next
  71. Return strResult
  72. End Function
  73.  
  74. Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
  75. Dim sb As New System.Text.StringBuilder(arrInput.Length * 2)
  76. For i As Integer = 0 To arrInput.Length - 1
  77. sb.Append(arrInput(i).ToString("X2"))
  78. Next
  79. Return sb.ToString().ToLower
  80. End Function
  81.  
  82. Private Interface IHasher
  83. Function hashOfString(ByVal stringToHash As String) As String
  84. Function hashOfFile(ByVal fileToHash As String) As String
  85. End Interface
  86. End Class