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

''' <summary>
''' Contains functions to work with TLL
''' </summary>
''' <remarks></remarks>
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">Gives you more information if license validation failed</param>
    ''' <param name="LicenseObject">Contains license details</param>
    ''' <returns>Bool: True if license was successfully verified</returns>
    ''' <remarks></remarks>
    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>
    ''' Return actual hardware ID
    ''' </summary>
    ''' <returns>String: Actual hardware ID</returns>
    ''' <remarks></remarks>
    Public Function GetAactualHardwareID() As String
        Return HardwareID.ActualHardwareID
    End Function

    ''' <summary>
    ''' Validates if TLL was not modified by unathorized user
    ''' </summary>
    ''' <returns>Bool: True if file was not modified</returns>
    ''' <remarks></remarks>
    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>
    ''' <remarks></remarks>
    Private Sub SetLicenseInformation()
        TreeksLicensingLibrary.DeveloperLicense.LicenseXML = MyLicenseXML
    End Sub

    ''' <summary>
    ''' Returns path of TLL to verify integrity
    ''' </summary>
    ''' <returns>String: Treek's Licensing Library DLL path</returns>
    ''' <remarks></remarks>
    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>
''' <remarks></remarks>
Public Class SHA1
    Implements IHasher

    ''' <summary>
    ''' Returns SHA1 hash of file
    ''' </summary>
    ''' <param name="fileToHash">Filesystem path to hashed file</param>
    ''' <returns>String</returns>
    ''' <remarks></remarks>
    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>String</returns>
    ''' <remarks></remarks>
    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