Validate integrity of TreeksLicensingLibrary.dll

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.

public class LicenseLibraryIntegrityValidator
{

    public bool ValidateLicensingLibrary()
    {

        SHA1 sha = new SHA1();
        string path = LibraryDLLPath();

        string current_hash = sha.hashOfFile(path);
        string valid_hash = "8e237b886f21606e9c1a49fafd23ebab380d2697";

        if (valid_hash == current_hash)
        {
            return true;
        }
        else
        {
            return false;
        }

    }

    private string LibraryDLLPath()
    {

        string dirpath = IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location);
        if (!dirpath.EndsWith("\\"))
            dirpath += "\\";

        string path = dirpath + "TreeksLicensingLibrary.dll";

        return path;

    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;

public class SHA1 : IHasher
{

    /// <summary>
    ///  
    /// Returns SHA1 hash of file
    /// </summary>
    /// <param name="fileToHash">Filesystem path to hashed file</param>
    /// <returns></returns>
    public string hashOfFile(string fileToHash)
    {
        FileStream rdr = default(FileStream);
        SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
        byte[] bytes = null;
        string rtn = "";
        if (File.Exists(fileToHash))
        {
            rdr = new FileStream(fileToHash, FileMode.Open, FileAccess.Read);
            bytes = sha.ComputeHash(rdr);
            rtn = ByteArrayToString(bytes);
        }
        return rtn;
    }

    /// <summary>
    /// Returns SHA1 hash of string
    /// </summary>
    /// <param name="stringToHash">String to be hashed</param>
    /// <returns></returns>
    public string hashOfString(string stringToHash)
    {
        SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
        byte[] bytesToHash = System.Text.Encoding.ASCII.GetBytes(stringToHash);
        bytesToHash = sha.ComputeHash(bytesToHash);
        string strResult = "";
        foreach (byte b in bytesToHash)
        {
            strResult += b.ToString("x2");
        }
        return strResult;
    }

    private string ByteArrayToString(byte[] arrInput)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder(arrInput.Length * 2);
        for (int i = 0; i & lt;= arrInput.Length - 1; i++) {
            sb.Append(arrInput(i).ToString("X2"));
        }
        return sb.ToString().ToLower;
    }

    private interface IHasher
    {
        string hashOfString(string stringToHash);
        string hashOfFile(string fileToHash);
    }
}
Public Class LicenseLibraryIntegrityValidator

    Public 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 = "8e237b886f21606e9c1a49fafd23ebab380d2697"

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

    End Function

    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

Imports System.IO
Imports System.Security.Cryptography

Public Class SHA1
    Implements IHasher

    ''' <summary>
    ''' Returns SHA1 hash of file
    ''' </summary>
    ''' <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