VB.NET complete implementation

To use following example, simply create new class in your VB.NET project and paste following code.

You can also download this example.

Imports System.IO
Imports System.Security.Cryptography
Imports TreeksLicensingLibrary


''' <summary>
''' Contains functions to work with TLL
''' </summary>
Public Class TLLInterface

    Private Const MyPublicKey As String = "Put your PUBLIC key XML here." 'Just paste from toolbox
    Private Const MyLicenseXML As String = "Put your license from us here"
    Private verification As New LicenseVerification(MyPublicKey)

    ''' <summary>
    ''' Call this function to verify user's license. Optionally supply string and License object to get more information (error message, license details)
    ''' </summary>
    ''' <param name="ErrorMessage">Output parameter describing what failed during license validation</param>
    ''' <param name="LicenseObject">Output parameter containing validated and structured license object</param>
    ''' <returns></returns>
    Public Function IsLicenseValid(Optional ByRef ErrorMessage As String = "", Optional ByRef LicenseObject As TreeksLicensingLibrary.License = Nothing) As Boolean

        Dim LicenseFilePath As String = IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location) & "\registration.lic"

        If ValidateLicensingLibrary() Then
            If IO.File.Exists(LicenseFilePath) Then
                If verification.VerifyLicenseFile(LicenseFilePath, ErrorMessage, LicenseObject) Then
                    Return True
                Else
                    Return False
                End If
            Else
                ErrorMessage = "License file not present."
                Return False
            End If
        Else
            ErrorMessage = "Library integrity check failed."
            Return False
        End If
    End Function

    ''' <summary>
    ''' Returns actual hardware ID
    ''' </summary>
    ''' <returns></returns>
    Public Function GetAactualHardwareID() As String
        Return HardwareID.ActualHardwareID
    End Function

    ''' <summary>
    ''' Validates if TLL was not modified by unathorized user
    ''' </summary>
    ''' <returns></returns>
    Private Function ValidateLicensingLibrary() As Boolean

        Dim sha As New SHA1
        Dim path As String = LibraryDLLPath()

        Dim current_hash As String = sha.hashOfFile(path)
        Dim valid_hash As String = "7e4b6c0c073853205dbb92bf0798e977a36dbf75"

        If valid_hash = current_hash Then
            Return True
        Else
            Return False
        End If

    End Function

    ''' <summary>
    ''' Disables TLL demo message
    ''' </summary>
    Private Sub SetLicenseInformation()
        TreeksLicensingLibrary.DeveloperLicense.LicenseXML = MyLicenseXML
    End Sub

    ''' <summary>
    ''' Returns path of TLL to verify integrity
    ''' </summary>
    ''' <returns></returns>
    Private Function LibraryDLLPath() As String

        Dim dirpath As String = IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location)
        If Not dirpath.EndsWith("\") Then dirpath &= "\"

        Dim path As String = dirpath & "TreeksLicensingLibrary.dll"

        Return path

    End Function

End Class


''' <summary>
''' Contains interface to work with SHA1
''' </summary>
Public Class SHA1
    Implements IHasher

    ''' <summary>
    ''' Returns SHA1 hash of file
    ''' <param name="fileToHash">Filesystem path to hashed file </param>
    ''' <returns></returns>
    Public Function hashOfFile(ByVal fileToHash As String) As String Implements IHasher.hashOfFile
        Dim rdr As FileStream
        Dim sha As New SHA1CryptoServiceProvider
        Dim bytes() As Byte
        Dim rtn As String = ""
        If File.Exists(fileToHash) Then
            rdr = New FileStream(fileToHash, FileMode.Open, FileAccess.Read)
            bytes = sha.ComputeHash(rdr)
            rtn = ByteArrayToString(bytes)
        End If
        Return rtn
    End Function

    ''' <summary>
    ''' Returns SHA1 hash of string
    ''' </summary>
    ''' <param name="stringToHash">String to be hashed</param>
    ''' <returns></returns>
    Public Function hashOfString(ByVal stringToHash As String) As String Implements IHasher.hashOfString
        Dim sha As New SHA1CryptoServiceProvider
        Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(stringToHash)
        bytesToHash = sha.ComputeHash(bytesToHash)
        Dim strResult As String = ""
        For Each b As Byte In bytesToHash
            strResult += b.ToString("x2")
        Next
        Return strResult
    End Function

    Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
        Dim sb As New System.Text.StringBuilder(arrInput.Length * 2)
        For i As Integer = 0 To arrInput.Length - 1
            sb.Append(arrInput(i).ToString("X2"))
        Next
        Return sb.ToString().ToLower
    End Function

    Private Interface IHasher
        Function hashOfString(ByVal stringToHash As String) As String
        Function hashOfFile(ByVal fileToHash As String) As String
    End Interface
End Class