 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Titan
Joined: 11 Aug 2004 Posts: 5009 Location: imaginationland
|
Posted: Thu Dec 14, 2006 6:42 pm Post subject: Universally Unique Identifier (UUID/GUID) Generator |
|
|
Generates a time-based Universally Unique Identifier based on random clock and node IDs.
e.g. MsgBox, % uuid(y) ; where y is false for random IDs
| Code: | uuid(c = false) { ; v1.1 - by Titan
static n = 0, l, i
f := A_FormatInteger, t := A_Now, s := "-"
SetFormat, Integer, H
t -= 1970, s
t := (t . A_MSec) * 10000 + 122192928000000000
If !i and c {
Loop, HKLM, System\MountedDevices
If i := A_LoopRegName
Break
StringGetPos, c, i, %s%, R2
StringMid, i, i, c + 2, 17
} Else {
Random, x, 0x100, 0xfff
Random, y, 0x10000, 0xfffff
Random, z, 0x100000, 0xffffff
x := 9 . SubStr(x, 3) . s . 1 . SubStr(y, 3). SubStr(z, 3)
} t += n += l = A_Now, l := A_Now
SetFormat, Integer, %f%
Return, SubStr(t, 10) . s . SubStr(t, 6, 4) . s . 1 . SubStr(t, 3, 3). s . (c ? i : x)
} |
Download _________________
RegExReplace("irc.freenode.net/autohotkey", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Last edited by Titan on Fri Dec 15, 2006 3:58 pm; edited 1 time in total |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 3943 Location: Pittsburgh
|
Posted: Thu Dec 14, 2006 10:51 pm Post subject: |
|
|
| Since AHK's (pseudo) random number generator is initialized based on the current time, if thousands of PC's generate these uuid values in the same time, there is a good chance that some of them will be the same. Have you considered to mix in some values, which are different in different PC's? Something like the user name, language, windows version are easy to access, others, like the BIOS version, motherboard type, disk serial number are still possible. These have to be hashed, not to leak personal information. And, of course, you can always access Windows' GUID, which already incorporates these. |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5009 Location: imaginationland
|
Posted: Fri Dec 15, 2006 4:05 pm Post subject: |
|
|
The time based algorithm allows up to 10000 unique identifiers to be made every millisecond (281474976710656 in my version) so the chance of a similarity is very little. In the new version 1.1 I took your idea of parsing Windows GUID in the registry for real clock and node IDs. This value is cached for later calls. _________________
RegExReplace("irc.freenode.net/autohotkey", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2") |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 3943 Location: Pittsburgh
|
Posted: Fri Dec 15, 2006 5:17 pm Post subject: |
|
|
This is better!
About the collisions: How many people need to be in a group, such that there is a 50% chance that at least two of them have the same birthday? Think about it, before reading further.
The answer is 23. It is surprisingly low, is not it? In general, if there are n possible results of a measurement, after performing it sqrt(n) times, there is a near 50% chance that two results are the same. It is called the birthday paradox.
The resolution of the Windows timer is 10..16 ms. It means, in every second there can be as few as 1000/16 = 62.5 different A_MSec values. In a minute there are only 60*62.5 = 3750 different time values. According to the birthday paradox, if sqrt(3750) =~ 61 PC's read the time in this minute, there is a 50% chance that two of them get the same result, and their random number generator will be initialized identically, too. If 500 PC's would execute the script in the same hour, there will almost certainly be two of them getting the same time values: sqrt(60*60*62.5) =~ 474. |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5009 Location: imaginationland
|
Posted: Fri Dec 15, 2006 6:24 pm Post subject: |
|
|
I've read about the birthday paradox, it's an interesting theory. I'm disappointed about the low res. timer; 2/500 is a bad probability for an identifier which "1 trillion have to be created every nanosecond for 10 billion years to exhaust [...]". I've looked into GetSystemTime but that's likely to be the same.
However your calculations (which are impressive btw.) only account for the first part of the UUID - the LSB time group. The last two parts, clock ID (CPU clock) and node ID (MAC address) are there to ensure no collisions can occur with different computers using genuine NICs. Even in quirks mode (param = false) fourteen digits are randomized which yields a probability of 1/14^16 (72057594037927936). _________________
RegExReplace("irc.freenode.net/autohotkey", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2") |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 3943 Location: Pittsburgh
|
Posted: Fri Dec 15, 2006 6:34 pm Post subject: |
|
|
| I wrote about the collision of the time values. Mixing in PC or user dependent information makes the collision much less likely. However, you have to mix (hash) the pieces together, otherwise there is a possible attack: someone can identify unchanging parts of your uuid values, and create valid uuid values in your behalf. In some cases it is bad. |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5009 Location: imaginationland
|
Posted: Fri Dec 15, 2006 6:54 pm Post subject: |
|
|
Uuids are not used in cryptography so it doesn't matter. You can edit any uuid and it will still be valid (with a few exceptions). Also randomized node IDs are prefixed with '1' (RFC 4122) which differentiates it from valid MAC addresses, so there is no security concern here. _________________
RegExReplace("irc.freenode.net/autohotkey", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2") |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5581
|
Posted: Fri Dec 15, 2006 7:06 pm Post subject: |
|
|
Very Nice.. .. On a related note: I think Random command should be available as a function. _________________ SKAN - Suresh Kumar A N |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5009 Location: imaginationland
|
Posted: Sat Dec 16, 2006 11:03 pm Post subject: |
|
|
I've changed my mind, hashing values is not too difficult anyway. Here's what I came up with, is it secure enough? | Code: | SetFormat, Integer, H
Loop, 3 {
y := A_Index, z0 := 1 << x := y * 8, z := (1 << x + 4) - 1
Loop, 8 {
Random, x, %z0%, %z%
y%y% += x >> 2 & z
}
}
s := "-", x := 9 . SubStr(y0x1, 3) . s . 1 . SubStr(y0x2, 3) . SubStr(y0x3, 3, 5)
MsgBox, , UUID, Secure random node: %x% |
| Goyyah wrote: | | I think Random command should be available as a function. | It's not a frequency used command so I don't think it will happen. _________________
RegExReplace("irc.freenode.net/autohotkey", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2") |
|
| Back to top |
|
 |
Dippy46
Joined: 06 Jul 2004 Posts: 171 Location: Manchester, England.
|
Posted: Sun Dec 17, 2006 11:01 am Post subject: |
|
|
@Goyyah
I think this is probably good enough for what you need
| Quote: |
loop
{
tooltip % random(0,255)
sleep 500
}
random(_min,_max)
{
Random,_rand, _Min, _Max
return _rand
}
|
Regards
Dave. _________________ Simple ideas lie within reach, only of complex minds |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5581
|
Posted: Sun Dec 17, 2006 11:24 am Post subject: |
|
|
| Titan wrote: | | It's not a frequency used command so I don't think it will happen. |
It is a less frequently used command because people do not realize its potential. It is one command that should definitely be a function.
@Dippy46: Yes! I know I can write a wrapper function for that command. But I do not prefer my user defined functions to have a dependency on an another UDF. I would rather prefer to do it the same way Titan has done in uuid()
I do not want to hijack this thread, I better put my suggestions in the Wish List.
Regards,  _________________ SKAN - Suresh Kumar A N |
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 3593 Location: Belgrade
|
Posted: Tue Jan 16, 2007 11:42 am Post subject: |
|
|
I tried to do this with API functions like this:
| Code: |
VarSetCapacity(bGuid, 16)
VarSetCapacity(sGuid, 40)
dllcall("ole32.dll\CoCreateGuid", "uint", &bGuid)
x := dllcall("ole32.dll\StringFromGUID2", "uint", &bGuid, "str", sGuid, "int", 40)
;VarSetCapacity(sGuid, -1) --doesn't work
msgbox %x% %bGuid% |
and it returns the string but AHK cuts it at first charahter so only "{" is visible, although x (number of characthers set by API call) is set normaly.
What is the problem here ? _________________
 |
|
| Back to top |
|
 |
tonne
Joined: 06 Jun 2006 Posts: 1143 Location: Denmark
|
Posted: Tue Jan 16, 2007 11:54 am Post subject: |
|
|
Unicode? _________________ there's a dog barking close within the range of my ear
sounds like he wants to escape the chain
he would probably bite me to death if he could
but the chain lets me spit in his face
- Kashmir |
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 3593 Location: Belgrade
|
Posted: Tue Jan 16, 2007 11:59 am Post subject: |
|
|
uh...I forgot that... most definitely... _________________
 |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5581
|
Posted: Tue Jan 16, 2007 12:10 pm Post subject: |
|
|
Yes .. it is Unicode and the string length is 76
| Code: | | VarSetCapacity(sGuid, 76) |
 |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|