To use this example in your C# application, simply create new class and paste following code.

You can also download this example.

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Security.Cryptography;
  8. using System.Reflection;
  9. using TreeksLicensingLibrary;
  10.  
  11. /// <summary>
  12. /// Contains functions to work with TLL
  13. /// </summary>
  14. /// <remarks></remarks>
  15. public class TLLInterface
  16. {
  17.  
  18. //Just paste from toolbox
  19. private const string MyPublicKey = "Put your PUBLIC key XML here.";
  20.  
  21. private const string MyLicenseXML = "Put your license from us here";
  22.  
  23. private LicenseVerification verification = new LicenseVerification(MyPublicKey);
  24. /// <summary>
  25. /// Call this function to verify user's license. Optionally supply string and License object to get more information (error message, license details)
  26. /// </summary>
  27. /// <param name="ErrorMessage">Gives you more information if license validation failed</param>
  28. /// <param name="LicenseObject">Contains license details</param>
  29. /// <returns>Bool: True if license was successfully verified</returns>
  30. /// <remarks></remarks>
  31. public bool IsLicenseValid(ref string ErrorMessage, ref TreeksLicensingLibrary.License LicenseObject)
  32. {
  33.  
  34. string LicenseFilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\registration.lic";
  35.  
  36. if (ValidateLicensingLibrary()) {
  37. if (File.Exists(LicenseFilePath)) {
  38. if (verification.VerifyLicenseFile(LicenseFilePath, ref ErrorMessage, ref LicenseObject)) {
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. } else {
  44. ErrorMessage = "License file not present.";
  45. return false;
  46. }
  47. } else {
  48. ErrorMessage = "Library integrity check failed.";
  49. return false;
  50. }
  51. }
  52.  
  53. /// <summary>
  54. /// Return actual hardware ID
  55. /// </summary>
  56. /// <returns>String: Actual hardware ID</returns>
  57. /// <remarks></remarks>
  58. public string GetAactualHardwareID()
  59. {
  60. return HardwareID.ActualHardwareID;
  61. }
  62.  
  63. /// <summary>
  64. /// Validates if TLL was not modified by unathorized user
  65. /// </summary>
  66. /// <returns>Bool: True if file was not modified</returns>
  67. /// <remarks></remarks>
  68. private bool ValidateLicensingLibrary()
  69. {
  70.  
  71. SHA1 sha = new SHA1();
  72. string path = LibraryDLLPath();
  73.  
  74. string current_hash = sha.hashOfFile(path);
  75. string valid_hash = "7e4b6c0c073853205dbb92bf0798e977a36dbf75";
  76.  
  77. if (valid_hash == current_hash) {
  78. return true;
  79. } else {
  80. return false;
  81. }
  82.  
  83. }
  84.  
  85. /// <summary>
  86. /// Disables TLL demo message
  87. /// </summary>
  88. /// <remarks></remarks>
  89. private void SetLicenseInformation()
  90. {
  91. TreeksLicensingLibrary.DeveloperLicense.LicenseXML = MyLicenseXML;
  92. }
  93.  
  94. /// <summary>
  95. /// Returns path of TLL to verify integrity
  96. /// </summary>
  97. /// <returns>String: Treek's Licensing Library DLL path</returns>
  98. /// <remarks></remarks>
  99. private string LibraryDLLPath()
  100. {
  101.  
  102. string dirpath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  103. if (!dirpath.EndsWith("\\"))
  104. dirpath += "\\";
  105.  
  106. string path = dirpath + "TreeksLicensingLibrary.dll";
  107.  
  108. return path;
  109.  
  110. }
  111.  
  112. }
  113.  
  114. /// <summary>
  115. /// Contains interface to work with SHA1
  116. /// </summary>
  117. /// <remarks></remarks>
  118. public class SHA1
  119. {
  120.  
  121. /// <summary>
  122. /// Returns SHA1 hash of file
  123. /// </summary>
  124. /// <param name="fileToHash">Filesystem path to hashed file</param>
  125. /// <returns>String</returns>
  126. /// <remarks></remarks>
  127. public string hashOfFile(string fileToHash)
  128. {
  129. FileStream rdr = default(FileStream);
  130. SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
  131. byte[] bytes = null;
  132. string rtn = "";
  133. if (File.Exists(fileToHash)) {
  134. rdr = new FileStream(fileToHash, FileMode.Open, FileAccess.Read);
  135. bytes = sha.ComputeHash(rdr);
  136. rtn = ByteArrayToString(bytes);
  137. }
  138. return rtn;
  139. }
  140.  
  141. /// <summary>
  142. /// Returns SHA1 hash of string
  143. /// </summary>
  144. /// <param name="stringToHash">String to be hashed</param>
  145. /// <returns>String</returns>
  146. /// <remarks></remarks>
  147. public string hashOfString(string stringToHash)
  148. {
  149. SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
  150. byte[] bytesToHash = System.Text.Encoding.ASCII.GetBytes(stringToHash);
  151. bytesToHash = sha.ComputeHash(bytesToHash);
  152. string strResult = "";
  153. foreach (byte b in bytesToHash) {
  154. strResult += b.ToString("x2");
  155. }
  156. return strResult;
  157. }
  158.  
  159. private string ByteArrayToString(byte[] arrInput)
  160. {
  161. System.Text.StringBuilder sb = new System.Text.StringBuilder(arrInput.Length * 2);
  162. for (int i = 0; i <= arrInput.Length - 1; i++) {
  163. sb.Append(arrInput[i].ToString("X2"));
  164. }
  165.  
  166. return sb.ToString().ToLower();
  167. }
  168.  
  169. }