Native UUID Generation

This is the constructor of a UUID class I use

[code]Sub Constructor()
Self.mBytes = Crypto.GenerateRandomBytes(16)
Self.mBytes.LittleEndian = False

Dim Value As Byte = Self.mBytes.UInt8Value(6)
Value = Value And CType(&b00001111, UInt8)
Value = Value Or CType(&b01000000, UInt8)
Self.mBytes.UInt8Value(6) = Value

Value = Self.mBytes.UInt8Value(8)
Value = Value And CType(&b00111111, UInt8)
Value = Value Or CType(&b10000000, UInt8)
Self.mBytes.UInt8Value(8) = Value
End Sub[/code]

Edit: If I recall correctly, Kem originally wrote this code. Or at least inspired it.

[quote=259167:@Kem Tekinay]It’s there.

M_String.GenerateUUID [/quote]

Hmm. Really? The version I downloaded goes from Filter_MTC to HasHighASCII.

Just downloaded it 10 minutes ago…

[quote=259168:@Thom McGrath]This is the constructor of a UUID class I use

[code]Sub Constructor()
Self.mBytes = Crypto.GenerateRandomBytes(16)
Self.mBytes.LittleEndian = False

Dim Value As Byte = Self.mBytes.UInt8Value(6)
Value = Value And CType(&b00001111, UInt8)
Value = Value Or CType(&b01000000, UInt8)
Self.mBytes.UInt8Value(6) = Value

Value = Self.mBytes.UInt8Value(8)
Value = Value And CType(&b00111111, UInt8)
Value = Value Or CType(&b10000000, UInt8)
Self.mBytes.UInt8Value(8) = Value
End Sub[/code]

Edit: If I recall correctly, Kem originally wrote this code. Or at least inspired it.[/quote]

Thanks, Tom. But my question is will GenerateRandomBytes always return the same code for a particular machine? I’m not enough of an expert in Random number generation. But I would expect that to return different results each time when run… No?

Well… yeah. Aren’t you looking to generate a UUID? This will generate a v4 UUID, which is based on Random data.

What I’m looking to generate is a unique device ID that will always be the same on a particular device. So it doesn’t help me if it generates a different ID whenever it’s run.

The way I am currently generating an ID for devices is having problems particularly in Windows as the WindowsWMI service doesn’t always return identifying values correctly.

Oh. That’s often something different.

So will Kem’s code above do what I want?

Maybe I’m getting my acronyms mixed up and being dyslexic or something here! I actually want UDID not UUID…

Yeah, that’s exactly what’s happening. UDID, not UUID.

UGH! :slight_smile:

You were right Jon, the version on my site did not have that code. It does now, and I’ve added a Version constant to avoid confusion like that later.

FYI, when I downloaded the project to check, the external module was still pointing to the original version on my drive, not the one inside the project folder so it looked to me like it was complete.

Add my 2cents for UUID

FUNCTION GenerateUUID as STRING
  Dim result As String
  #If TargetCocoa
    Soft Declare Function NSClassFromString Lib "Cocoa" ( clsName As cfstringref ) As ptr
    Soft Declare Function UUID Lib "Cocoa" selector "UUID" ( clsRef As ptr ) As ptr
    Soft Declare Function UUIDString Lib "Cocoa" selector "UUIDString" ( obj_id As ptr ) As cfstringref
    Dim classPtr As Ptr = NSClassFromString( "NSUUID" )
    Dim NSUUID As ptr = UUID( classPtr )
    result = UUIDString( NSUUID )
  #ElseIf TargetWin32
    Const kLibName = "rpcrt4"
    Soft Declare Function UUIDCreate Lib kLibName alias "UuidCreate" ( ByRef uuid As WindowsUUID ) As Integer
    Soft Declare Function UUIDToString Lib kLibName alias "UuidToStringA" ( ByRef inUUID As WindowsUUID, ByRef outString As CString ) As Integer
    Dim uuid As WindowsUUID
    Dim out As CString
    result = out
    result = result.DefineEncoding( Encodings.UTF8 )
    result = result.Uppercase
  #EndIf
  
  Return result
END FUNCTION

and for good measure [SWIFT]

func getUUID() -> String {
    let uuid = NSUUID().UUIDString
    return uuid
}

There’s several ways you can accomplish this.

  • Read the machine serial number and generate a hash of it.
  • Read some hardware values and generate a hash from it, such as the mac address from network adaptor 0, or the UUID of the boot drive.
  • Generate your own identifier and store it in a file or preferences.

None of these are fool proof, the third one (as you store the value) is completely anonymous as it doesn’t use any hardware values. On the downside, if the settings are reset, it’ll act as if it’s a new computer or user.

Ok… here is what I use to attempt to identify a unique machine… not 100% foolproof…

  Dim sh As New Shell
  Dim s As String
  Dim x As Integer
  
  #If TargetMacOS
    sh.execute "/usr/sbin/ioreg -l | /usr/bin/grep IOPlatformSerialNumber"
    s=sh.Result
    x=InStr(s,"=")
    If x>0 Then s=ReplaceAll(Trim(Mid(s,x+1)),ChrB(34),"")
  #ElseIf TargetWin32
    // use IPCONFIG /all and parse the "Physical Address from "Local Area Connection" Block
    // will be something like C4-2C-03-02-31-26  [C42c03023126 take first 11 char = C42C0302312]
    sh.Execute("ipconfig /all")
    If sh.ErrorCode = 0 Then
      s = sh.Result
      s = Mid(s,InStr(s,"Local Area Connection")+21)
      s = Mid(s,InStr(s,"Physical Address")+16)
      s = Left(s,InStr(s,EndOfLine))
      s= Left(ReplaceAll(Trim(Right(s,19)),"-","")+"Q9734WF",11) // pad it out with a known random string
    End If
  #EndIf
  If s="" Then s="Z98888AAAQ2" // if above methods are not available... just use "something"
  
  Return s
  

there should be another way of getting MAC addresses via DLL on Windows if I remember correctly

I’m not sure relying on the MAC address is too good a plan. I have replaced my one PC’s one NIC three or so times this year because of lightning.

On Windows there are utilities like Technitium Mac Changer that let the user enter whatever he wants in seconds.

I would rather go the C: hard drive SN route, or plug a couple keys in the Register in two different places, a haystack big enough to not find the needles easily.

You can get the hdd s/n quickly ( amongst other ways ) with the shell command:

vol c:

To be complete, the MAC address can also be changed easily in OS X.
http://osxdaily.com/2012/03/01/change-mac-address-os-x/

Just offered as an idea. Remember these solutions are like locks. they keep the honest people out. Those determined to bypass it will.

Agree with Dave here. Don’t spend too much time chasing the foolproof system that doesn’t exist and never will.