Differenze tra le versioni di "Ottenere un Identificativo Univoco Universale - UUID"
Da Gambas-it.org - Wikipedia.
Riga 7: | Riga 7: | ||
Print RTrim(File.Load("/proc/sys/kernel/random/uuid")) | Print RTrim(File.Load("/proc/sys/kernel/random/uuid")) | ||
+ | |||
+ | End | ||
+ | Un altro esempio usando le sole risorse di Gambas: | ||
+ | Public Sub Main() | ||
+ | |||
+ | Print Hex(Rand32(), 8); "-"; Hex(Rand32() And &ffff&, 4); "-"; Hex((Rand32() And &0fff) Or &4000, 4); "-"; Hex((Rand32() And &3fff) + &8000, 4); "-"; Hex(Rand32() And &ffff&, 4); Hex(Rand32(), 8) | ||
+ | |||
+ | End | ||
+ | |||
+ | Private Function Rand32() As Integer | ||
+ | |||
+ | Dim dd As Integer = DateDiff("01/01/1970", Now, gb.Second) | ||
+ | |||
+ | Return (Lsl(Rand(dd) And &03, 30)) Or (Lsl(Rand(dd) And &7fff, 15)) Or (Rand(dd) And &7fff) | ||
+ | |||
+ | End | ||
+ | |||
+ | ====Usare alcune funzioni della libreria libc.so.6==== | ||
+ | Library "libc:6" | ||
+ | |||
+ | <FONT Color=gray>' ''void srand (unsigned int __seed)'' | ||
+ | ' ''Seed the random number generator with the given number.''</font> | ||
+ | Private Extern srand(__seed As Integer) | ||
+ | |||
+ | <FONT Color=gray>' ''int rand (void)'' | ||
+ | ' ''Return a random integer between 0 and RAND_MAX inclusive.''</font> | ||
+ | Private Extern rand_C() As Integer Exec "rand" | ||
+ | |||
+ | |||
+ | Public Sub Main() | ||
+ | |||
+ | <FONT Color=gray>' ''"01/01/1970" è la data di inizio del "[https://it.wikipedia.org/wiki/Tempo_(Unix) Unix Time Stamp" - Epoch]:''</font> | ||
+ | srand(DateDiff("01/01/1970", Now, gb.Second)) | ||
+ | |||
+ | Print Hex(Rand32(), 8); "-"; Hex(Rand32() And &ffff&, 4); "-"; Hex((Rand32() And &0fff) Or &4000, 4); "-"; Hex((Rand32() And &3fff) + &8000, 4); "-"; Hex(Rand32() And &ffff&, 4); Hex(Rand32(), 8) | ||
+ | |||
+ | End | ||
+ | |||
+ | Private Function Rand32() As Integer | ||
+ | |||
+ | Return (Lsl(rand_C() And &03, 30)) Or (Lsl(rand_C() And &7fff, 15)) Or (rand_C() And &7fff) | ||
+ | |||
+ | End | ||
+ | |||
+ | ====Usare una libreria esterna appositamente creata==== | ||
+ | Si potrà usare anche una libreria esterna, appositamente creata, contenente alcune funzioni del API di "uuid". | ||
+ | |||
+ | Mostriamo un esempio pratico: | ||
+ | <FONT Color=gray>' char * creauuid()</font> | ||
+ | Private Extern creauuid() As Pointer In "/tmp/libcreauuid" | ||
+ | |||
+ | |||
+ | Public Sub Main() | ||
+ | |||
+ | Dim p As Pointer | ||
+ | |||
+ | Creaso() | ||
+ | |||
+ | p = creauuid() | ||
+ | |||
+ | Print String@(p) | ||
+ | |||
+ | End | ||
+ | |||
+ | |||
+ | Private Procedure Creaso() | ||
+ | |||
+ | File.Save("/tmp/libcreauuid.c", "#include <stdlib.h>\n#include <stdio.h>\n#include <uuid/uuid.h>\n\n" & | ||
+ | "char out[UUID_STR_LEN]={0};\n\n" | ||
+ | "char * creauuid() {\n\n" & | ||
+ | "uuid_t i;\n\n" & | ||
+ | " uuid_generate(i);\n\n" & | ||
+ | " uuid_unparse_lower(i, out);\n\n" & | ||
+ | " return out;\n\n}") | ||
+ | |||
+ | Shell "gcc -o /tmp/libcreauuid.so /tmp/libcreauuid.c -luuid -shared -fPIC" Wait | ||
End | End |
Versione attuale delle 15:19, 12 mag 2023
Per ottenere un Identificativo Univoco Universale - UUID [Nota 1] si possono invocare i seguenti file di sistema: [Nota 2]
* /etc/machine-id = ritorna un UUID statico (è sempre lo stesso) durante la sessione.
* /proc/sys/kernel/random/uuid = ritorna un UUID dinamico (varía ogni volta che si invoca il file di sistema che lo contiene).
Vediamo un esempio pratico:
Public Sub Main() Print RTrim(File.Load("/proc/sys/kernel/random/uuid")) End
Un altro esempio usando le sole risorse di Gambas:
Public Sub Main() Print Hex(Rand32(), 8); "-"; Hex(Rand32() And &ffff&, 4); "-"; Hex((Rand32() And &0fff) Or &4000, 4); "-"; Hex((Rand32() And &3fff) + &8000, 4); "-"; Hex(Rand32() And &ffff&, 4); Hex(Rand32(), 8) End Private Function Rand32() As Integer Dim dd As Integer = DateDiff("01/01/1970", Now, gb.Second) Return (Lsl(Rand(dd) And &03, 30)) Or (Lsl(Rand(dd) And &7fff, 15)) Or (Rand(dd) And &7fff) End
Usare alcune funzioni della libreria libc.so.6
Library "libc:6" ' void srand (unsigned int __seed) ' Seed the random number generator with the given number. Private Extern srand(__seed As Integer) ' int rand (void) ' Return a random integer between 0 and RAND_MAX inclusive. Private Extern rand_C() As Integer Exec "rand" Public Sub Main() ' "01/01/1970" è la data di inizio del "Unix Time Stamp" - Epoch: srand(DateDiff("01/01/1970", Now, gb.Second)) Print Hex(Rand32(), 8); "-"; Hex(Rand32() And &ffff&, 4); "-"; Hex((Rand32() And &0fff) Or &4000, 4); "-"; Hex((Rand32() And &3fff) + &8000, 4); "-"; Hex(Rand32() And &ffff&, 4); Hex(Rand32(), 8) End Private Function Rand32() As Integer Return (Lsl(rand_C() And &03, 30)) Or (Lsl(rand_C() And &7fff, 15)) Or (rand_C() And &7fff) End
Usare una libreria esterna appositamente creata
Si potrà usare anche una libreria esterna, appositamente creata, contenente alcune funzioni del API di "uuid".
Mostriamo un esempio pratico:
' char * creauuid() Private Extern creauuid() As Pointer In "/tmp/libcreauuid" Public Sub Main() Dim p As Pointer Creaso() p = creauuid() Print String@(p) End Private Procedure Creaso() File.Save("/tmp/libcreauuid.c", "#include <stdlib.h>\n#include <stdio.h>\n#include <uuid/uuid.h>\n\n" & "char out[UUID_STR_LEN]={0};\n\n" "char * creauuid() {\n\n" & "uuid_t i;\n\n" & " uuid_generate(i);\n\n" & " uuid_unparse_lower(i, out);\n\n" & " return out;\n\n}") Shell "gcc -o /tmp/libcreauuid.so /tmp/libcreauuid.c -luuid -shared -fPIC" Wait End
Note
[1] Vedere anche la seguente pagina: https://digitalbunker.dev/understanding-how-uuids-are-generated/
[2] Vedere anche la seguente pagina: Identificare univocamente un computer