Client Information and Convert to MD5

Imports System
Imports System.Management
Imports System.Security.Cryptography
Imports System.Text

Public Class Form1

    Private Function CpuId() As String
        Dim computer As String = "."
        Dim wmi As Object = GetObject("WinMgmts:")
        Dim processors As Object = wmi.InstancesOf("Win32_Processor")

        Dim cpu_ids As String = ""
        For Each cpu As Object In processors
            cpu_ids = cpu_ids & ", " & cpu.ProcessorId
        Next cpu
        If cpu_ids.Length > 0 Then cpu_ids = cpu_ids.Substring(2)

        Return cpu_ids
    End Function

    Private Function SystemSerialNumber() As String
        ' Get the Windows Management Instrumentation object.
        Dim wmi As Object = GetObject("WinMgmts:")

        ' Get the "base boards" (mother boards).
        Dim serial_numbers As String = ""
        Dim mother_boards As Object = wmi.InstancesOf("Win32_BaseBoard")
        For Each board As Object In mother_boards
            serial_numbers &= ", " & board.SerialNumber
        Next board
        If serial_numbers.Length > 0 Then serial_numbers = serial_numbers.Substring(2)

        Return serial_numbers
    End Function

    Public Function GetVolumeSerialNumber() As String
        Dim mobjSearcher As New ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk WHERE Name = 'C:'")
        Dim hdd_sn As String = ""
        For Each obj As ManagementObject In mobjSearcher.Get
            hdd_sn = hdd_sn & ", " & obj("VolumeSerialNumber")
            'Return obj("VolumeSerialNumber")
        Next
        If hdd_sn.Length > 0 Then hdd_sn = hdd_sn.Substring(2)
        Return hdd_sn
    End Function

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim md5Hash As MD5 = MD5.Create()
        lbl_cpuid_md5.Text = UCase(GetMd5Hash(md5Hash, CpuId()))
        lbl_board_md5.Text = UCase(GetMd5Hash(md5Hash, SystemSerialNumber()))
        lbl_hdd_md5.Text = UCase(GetMd5Hash(md5Hash, GetVolumeSerialNumber()))
    End Sub

    Public Function GetMd5Hash(ByVal md5Hash As MD5, ByVal input As String) As String
        ' Convert the input string to a byte array and compute the hash.
        Dim data As Byte() = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes
        ' and create a string.
        Dim sBuilder As New StringBuilder()

        ' Loop through each byte of the hashed data
        ' and format each one as a hexadecimal string.
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i

        ' Return the hexadecimal string.
        Return sBuilder.ToString()
    End Function 'GetMd5Hash
End Class

Comments

Popular posts from this blog

การตั้งเวลาระหว่าง Server และ Client

วิธีตั้งค่า NTP บน Primary Domain Controller

Installation and Run Node.JS on IIS