기타 [vb.net] MACAddress, HDD 정보, CPU ID, MACAddress 등...
페이지 정보

본문
Imports System
Imports System.Management
Imports System.Security.Cryptography
Imports System.Text
Public Class FingerPrint
    Private Shared fingerPrint As String
    Shared Sub New()
        fingerPrint = String.Empty
    End Sub
    Public Sub New()
        MyBase.New()
    End Sub
    Private Shared Function baseId() As String
        Return String.Concat(identifier("Win32_BaseBoard", "Model"), identifier("Win32_BaseBoard", "Manufacturer"), identifier("Win32_BaseBoard", "Name"), identifier("Win32_BaseBoard", "SerialNumber"))
    End Function
    Private Shared Function biosId() As String
        Return String.Concat(New String() {identifier("Win32_BIOS", "Manufacturer"), identifier("Win32_BIOS", "SMBIOSBIOSVersion"), identifier("Win32_BIOS", "IdentificationCode"), identifier("Win32_BIOS", "SerialNumber"), identifier("Win32_BIOS", "ReleaseDate"), identifier("Win32_BIOS", "Version")})
    End Function
    Private Shared Function cpuId() As String
        Dim str As String = identifier("Win32_Processor", "UniqueId")
        If (str = "") Then
            str = identifier("Win32_Processor", "ProcessorId")
            If (str = "") Then
                str = identifier("Win32_Processor", "Name")
                If (str = "") Then
                    str = identifier("Win32_Processor", "Manufacturer")
                End If
                str = String.Concat(str, identifier("Win32_Processor", "MaxClockSpeed"))
            End If
        End If
        Return str
    End Function
    Private Shared Function diskId() As String
        Return String.Concat(identifier("Win32_DiskDrive", "Model"), identifier("Win32_DiskDrive", "Manufacturer"), identifier("Win32_DiskDrive", "Signature"), identifier("Win32_DiskDrive", "TotalHeads"))
    End Function
    Private Shared Function GetHash(ByVal s As String) As String
        Dim mD5CryptoServiceProvider As System.Security.Cryptography.MD5CryptoServiceProvider = New System.Security.Cryptography.MD5CryptoServiceProvider()
        Dim bytes As Byte() = (New ASCIIEncoding()).GetBytes(s)
        Return GetHexString(mD5CryptoServiceProvider.ComputeHash(bytes))
    End Function
    Private Shared Function GetHexString(ByVal bt As Byte()) As String
        Dim chr As Char
        Dim empty As String = String.Empty
        Dim num As Integer = 0
        Do
            Dim num1 As Byte = bt(num)
            Dim num2 As Integer = num1 And 15
            Dim num3 As Integer = num1 >> 4 And 15
            If (num3 <= 9) Then
                empty = String.Concat(empty, num3.ToString())
            Else
                chr = Convert.ToChar(num3 - 10 + 65)
                empty = String.Concat(empty, chr.ToString())
            End If
            If (num2 <= 9) Then
                empty = String.Concat(empty, num2.ToString())
            Else
                chr = Convert.ToChar(num2 - 10 + 65)
                empty = String.Concat(empty, chr.ToString())
            End If
            If (num + 1 <> CInt(bt.Length) AndAlso (num + 1) Mod 2 = 0) Then
                empty = String.Concat(empty, "-")
            End If
            num = num + 1
        Loop While num < CInt(bt.Length)
        Return empty
    End Function
    Private Shared Function identifier(ByVal wmiClass As String, ByVal wmiProperty As String, ByVal wmiMustBeTrue As String) As String
        Dim str As String = ""
        For Each instance As ManagementObject In (New ManagementClass(wmiClass)).GetInstances()
            If (Not (instance(wmiMustBeTrue).ToString() = "True") OrElse Not (str = "")) Then
                Continue For
            End If
            Try
                str = instance(wmiProperty).ToString()
                Return str
            Catch
            End Try
        Next
        Return str
    End Function
    Private Shared Function identifier(ByVal wmiClass As String, ByVal wmiProperty As String) As String
        Dim str As String = ""
        For Each instance As ManagementObject In (New ManagementClass(wmiClass)).GetInstances()
            If (str <> "") Then
                Continue For
            End If
            Try
                str = instance(wmiProperty).ToString()
                Return str
            Catch
            End Try
        Next
        Return str
    End Function
    Private Shared Function macId() As String
        Return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled")
    End Function
    Public Shared Function Value() As String
        If (String.IsNullOrEmpty(fingerPrint)) Then
            fingerPrint = GetHash(String.Concat(New String() {"CPU >> ", cpuId(), "" & vbCrLf & "BIOS >> ", biosId(), "" & vbCrLf & "BASE >> ", baseId(), "" & vbCrLf & "DISK >> ", diskId(), "" & vbCrLf & "VIDEO >> ", videoId()}))
        End If
        Return fingerPrint
    End Function
    Private Shared Function videoId() As String
        Return String.Concat(identifier("Win32_VideoController", "DriverVersion"), identifier("Win32_VideoController", "Name"))
    End Function
End Class
    Public Shadows ReadOnly Property ToString As String
        Get
            If (String.IsNullOrEmpty(fingerPrint)) Then
                Dim jo As New JObject, j As JObject, t As String
                For Each path As String In {"Win32_BaseBoard", "Win32_BIOS", "Win32_Processor", 
                                                                    "Win32_DiskDrive", "Win32_NetworkAdapterConfiguration", "Win32_VideoController"}
                    For Each instance As ManagementObject In (New ManagementClass(path)).GetInstances()
                        j = New JObject : j.Add("ManagementPath", path)
                        For Each p As PropertyData In instance.Properties
                            If p.Value IsNot Nothing Then t = p.Value.ToString Else t = ""
                            If t = "System.UInt16[]" OrElse t = "System.String[]" Then
                                Dim s As New List(Of String)
                                For Each e In p.Value
                                    s.Add(e.ToString)
                                Next
                                t = Join(s.ToArray, ".")
                            End If
                            j.Add(p.Name, t)
                        Next
                        jo.Add(instance("Caption").ToString(), j)
                    Next
                Next
                fingerPrint = JsonConvert.SerializeObject(jo)
            End If
            Return fingerPrint
        End Get
    End Property
- 이전글Virtual-Key Codes 22.07.21
- 다음글[vb.net] HtmlAgilityPack, Jurassic 을 활용한 JavaScript 활용 22.07.13
댓글목록
등록된 댓글이 없습니다.





