[.Net] Classe HardwareId, identifiant d’ordinateur

hardwareid

Dans un de mes projets, je suis amené à identifier chaque machine sur laquelle mon logiciel est installé. Pour faire çà j’ai pensé à faire un identifiant spécifique à chaque ordinateur en me basant sur des propriétés matérielles.

La classe permet de générer un Hardware Id, c’est à dire un identifiant spécifique à chaque machine.
Cet identifiant peut ensuite être utilisé pour développer un système de gestion de licences ou bien pour nommer une instance de logiciel.

Cette classe se base sur les informations matérielles de la machine en les récupérant grâce à WMI.

Voici la classe HardwareId:

Imports System.Management
Imports System.Security.Cryptography
Imports System.Text
'Classe qui permet de générer un hardware id (pratique pour la gestion des licenses par exemple)
Public Class HardwareId
Private Shared _Hash() As Byte
#Region "Constructeur"
Shared Sub New()
Try
_Hash = GenerateHash(GenerateHardString())
Catch ex As Exception
Debug.WriteLine(ex.Message & vbCrLf & ex.StackTrace)
End Try
End Sub
#End Region
#Region "Fonctions de generation"
'Fonction qui genere un HardwareId sous forme de Md5
Public Shared Function GenerateMd5() As String
Dim ret As String = vbNullString
Try
ret = GenerateMd5(_Hash)
Catch ex As Exception
Debug.WriteLine(ex.Message & vbCrLf & ex.StackTrace)
End Try
Return ret
End Function
'Fonction qui genere un HardwareId sous forme d'entier
Public Shared Function GenerateInteger() As Integer
Dim ret As Integer = 0
Try
ret = GenerateMd5.GetHashCode
Catch ex As Exception
Debug.WriteLine(ex.Message & vbCrLf & ex.StackTrace)
End Try
Return ret
End Function
'Fonction qui genere un HardwareId sous forme de Guid
Public Shared Function GenerateGuid() As String
Dim ret As String = vbNullString
Try
ret = GenerateGuid(_Hash)
Catch ex As Exception
Debug.WriteLine(ex.Message & vbCrLf & ex.StackTrace)
End Try
Return ret
End Function
#End Region
#Region "Fonctions d'encryption"
'Fonction qui genere la chaine de base
Private Shared Function GenerateHardString() As String
Return "CPU >> " & GetCPUId() & vbCrLf & "BIOS >> " & GetBiosId() & vbCrLf & "MOTHERBOARD >> " & GetMotherboardId() & vbCrLf & "DISK >> " & GetDiskId() & vbCrLf & "GRAPHICS >> " & GetGraphicsId() & vbCrLf & "MACADDRESS >> " & GetMacId()
End Function
'Fonction qui génère le hash
Private Shared Function GenerateHash(ByVal str As String) As Byte()
Dim md5 As New MD5CryptoServiceProvider()
Dim enc As New ASCIIEncoding()
Return md5.ComputeHash(enc.GetBytes(str))
End Function
'Fonction qui formatte la sortie Guid
Private Shared Function GenerateGuid(ByVal b() As Byte) As String
Dim ret As String = vbNullString
Try
For i As Integer = 0 To b.Length - 1
Dim bb As Byte = b(i)
Dim n, n1, n2 As Integer
n = CInt(bb)
n1 = n And 15
n2 = (n >> 4) And 15
If n2 > 9 Then
ret &= Chr(n2 - 10 + Asc("A")).ToString
Else
ret &= n2.ToString
If n1 > 9 Then
ret &= Chr(n1 - 10 + Asc("A")).ToString()
Else
ret &= n1.ToString()
End If
If (((i + 1) <> b.Length) AndAlso ((i + 1) Mod 2 = 0)) Then
ret &= "-"
End If
End If
Next
Catch ex As Exception
Debug.WriteLine(ex.Message & vbCrLf & ex.StackTrace)
End Try
Return ret
End Function
'Fonction qui formatte la sortie Md5
Private Shared Function GenerateMd5(ByVal b() As Byte) As String
Dim ret As String = vbNullString
Try
Dim i As Integer
For i = 0 To b.Length - 1
ret &= b(i).ToString("x2")
Next i
Catch ex As Exception
Debug.WriteLine(ex.Message & vbCrLf & ex.StackTrace)
End Try
Return ret
End Function
#End Region
#Region "Recupération d'informations système"
'Fonction qui récupère les informations WMI
Private Shared Function GetWMI(ByVal wmiClass As String, ByVal wmiProperty As String, Optional ByVal wmiMustBeTrue As String = vbNullString) As String
Dim ret As String = vbNullString
Try
Dim mc As New ManagementClass(wmiClass)
Dim moc As ManagementObjectCollection = mc.GetInstances
For Each mo As ManagementObject In moc
Dim cont As Boolean = True
If wmiMustBeTrue <> vbNullString Then
If mo(wmiMustBeTrue) Is Nothing Or Trim(mo(wmiMustBeTrue).ToString.ToLower) <> "true" Then
cont = False
End If
End If
If cont Then
If ret = vbNullString Then
Try
If mo(wmiProperty) IsNot Nothing Then
ret = mo(wmiProperty).ToString()
Exit For
End If
Catch ex As Exception
End Try
End If
End If
Next
Catch ex As Exception
Debug.WriteLine(ex.Message & vbCrLf & ex.StackTrace)
End Try
Return ret
End Function
'Fonction qui retourne le numero de serie en fonctions de infos wmi recuperees
Private Shared Function GetSerial(ByVal wmiClass As String, ByVal wmiProperties() As String, ByVal stopAfterMatch As Boolean, Optional ByVal wmiMustBeTrue As String = vbNullString) As String
Dim ret As String = vbNullString
Try
For i As Integer = 0 To wmiProperties.Length - 1
Try
ret &= GetWMI(wmiClass, wmiProperties(i), wmiMustBeTrue)
If stopAfterMatch AndAlso ret <> vbNullString Then
Exit For
End If
Catch ex As Exception
End Try
Next
Catch ex As Exception
Debug.WriteLine(ex.Message & vbCrLf & ex.StackTrace)
End Try
Return ret
End Function
'Fonction qui retourne le CPU Id
Private Shared Function GetCPUId() As String
Return GetSerial("Win32_Processor", {"UniqueId", "Name", "Manufacturer", "MaxClockSpeed"}, True)
End Function
'Fonction qui retourne le Bios Id
Private Shared Function GetBiosId() As String
Return GetSerial("Win32_BIOS", {"Manufacturer", "SMBIOSBIOSVersion", "IdentificationCode", "SerialNumber", "ReleaseDate", "Version"}, False)
End Function
'Fonction qui retourne le Disk Id
Private Shared Function GetDiskId() As String
Return GetSerial("Win32_DiskDrive", {"Model", "Manufacturer", "Signature", "TotalHeads"}, False)
End Function
'Fonction qui retourne le Motherboard Id
Private Shared Function GetMotherboardId() As String
Return GetSerial("Win32_BaseBoard", {"Model", "Manufacturer", "Name", "SerialNumber"}, False)
End Function
'Fonction qui retourne le Graphics Id
Private Shared Function GetGraphicsId() As String
Return GetSerial("Win32_VideoController", {"DriverVersion", "Name"}, False)
End Function
'Fonction qui retourne l'addresse mac de la premiere carte reseau active
Private Shared Function GetMacId() As String
Return GetSerial("Win32_NetworkAdapterConfiguration", {"MACAddress"}, False, "IPEnabled")
End Function
#End Region
End Class

Pour utilise la classe HardwareId, il suffit de l’exécuter comme cela:

'Exemple d'utilisation:
Dim k As String = HardwareId.GenerateMd5
Console.WriteLine(k)
k = HardwareId.GenerateGuid
Console.WriteLine(k)
k = HardwareId.GenerateInteger.ToString
Console.WriteLine(k)

Vous pouvez télécharger la solution HardwareId Projet

Leave a Reply

Your email address will not be published. Required fields are marked *