Share My Creation [B4X][UUIDv7] (UPDATE) Class UUID generation for version 7

This class correctly implements UUIDv7, following the recommendations of RFC 4122 and the UUIDv7 proposal.
It generates time-sortable UUIDs with the correct version and variant, and with sufficient randomness to guarantee their uniqueness.
Furthermore, it handles collisions within the same millisecond using monotonic increments, following industry-standard practices for modern UUIDs.

🕒 48-bit timestamp (time-sortable) - It was corrected
🎲 80 random/monotonic bits generated with SecureRandom
🔢 Correct version and variant according to RFC 4122
⚡ Monotonic: prevents duplicates if multiple UUIDs are generated in the same millisecond
💻 Cross-platform: works on B4A, B4J, and B4i
📦 Standard output: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

It's secure, sortable, reliable, and production-ready.

Note:
If no timestamp is passed (ts=0), the current date/time in milliseconds is used.
If the timestamp is shorter than the previous one, lastTimestamp is used to maintain consistency.

Security and Randomness
* In B4A/B4J, use `SecureRandom` → cryptographic randomness.
* In B4i, use `Rnd` → pseudo-random (less secure, but functional).

Guaranteed Time Monotonicity
* Ensures that UUIDs generated at the same instant are not repeated.

B4X:
    Dim UUIDv7 As UUIDv7Generator
    UUIDv7.Initialize

    '1. Generation of UUID lists.
    For i = 1 To count
        Log(UUIDv7.Generate(0))
    Next
   
    '2. Generate a UUID with the current date and time
    Log(UUIDv7.Generate(0))
   
    '3. Generate a UUID with the desired date and time.
    Dim ts As Long = DateTime.Now
    Log(UUIDv7.Generate(ts))
   
    Dim ts As Long = 1773389204833
    Log(UUIDv7.Generate(ts))

B4X:
019ce647-ff70-7d5c-aadc-55ae5d41e306
019ce647-ffcd-7b83-ae2d-a0ac2e0a6f88
019ce647-ffcd-7b83-ae2d-a0ac2e0a6f89
019ce647-ffcd-7b83-ae2d-a0ac2e0a6f8a
019ce647-ffce-7008-a5b5-c5a522dbd274
019ce647-ffce-7008-a5b5-c5a522dbd275
019ce647-ffce-7008-a5b5-c5a522dbd276
019ce647-ffce-7008-a5b5-c5a522dbd277
019ce647-ffcf-70f2-8f07-979ad191b094
019ce647-ffcf-70f2-8f07-979ad191b095
019ce647-ffcf-70f2-8f07-979ad191b096
019ce647-ffcf-70f2-8f07-979ad191b097
019ce647-ffd0-7118-ba9f-b0ddf61b4557
019ce647-ffd0-7118-ba9f-b0ddf61b4558
019ce647-ffd0-7118-ba9f-b0ddf61b4559
019ce647-ffd0-7118-ba9f-b0ddf61b455a
019ce647-ffd1-7fd1-a889-4f3b3c022e99

You can test the results of the class on this page that decodes UUIDv7

The following files are attached: class and example.

Regards
 

Attachments

  • UUIDv7Generator.bas
    2.3 KB · Views: 10
  • UUIDv7.zip
    3.9 KB · Views: 5
Last edited:

Magma

Expert
Licensed User
Longtime User
This class generates cross-platform UUIDv7, without external libraries.

It's a UUID version 7 implementation that combines:

🕒 48-bit timestamp (time-sortable)
🎲 80 random/monotonic bits generated with SecureRandom
🔢 Correct version and variant according to RFC 4122
⚡ Monotonic: prevents duplicates if multiple UUIDs are generated in the same millisecond
💻 Cross-platform: works on B4A, B4J, and B4i
📦 Standard output: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

It's secure, sortable, reliable, and production-ready—much more secure than using Rnd() to generate UUIDs.

Regards

The following files are attached: class and example.
View attachment 170409
Do you believe is secure to use it at ID for records vs ID int autoincrement ?
 
Last edited:

TILogistic

Expert
Licensed User
Longtime User
Do you believe is secure to use it at ID for records vs ID int autoincrement ?
yes.

note:
Usage of UUIDv7 in Databases

1. Unique Primary Key

Can replace traditional auto-increment IDs.
Guarantees global uniqueness without relying on a central server.

2. Time-based Ordering

The first 6 bytes encode the timestamp.
Allows chronological queries without an additional date column.
Ideal for tables with frequent inserts that need to remain time-ordered.

3. Efficient Indexing

Although UUIDs are generally random, the sequential timestamp portion reduces index fragmentation in databases like PostgreSQL, MySQL, or SQL Server.

4. Distributed Environments

IDs can be generated across multiple servers without collision.
Useful for microservices architectures or replicated databases.

5. Security and Privacy

The random portion makes IDs unpredictable, unlike sequential auto-increment IDs.
 

Magma

Expert
Licensed User
Longtime User
yes.

note:
Usage of UUIDv7 in Databases

1. Unique Primary Key

Can replace traditional auto-increment IDs.
Guarantees global uniqueness without relying on a central server.

2. Time-based Ordering

The first 6 bytes encode the timestamp.
Allows chronological queries without an additional date column.
Ideal for tables with frequent inserts that need to remain time-ordered.

3. Efficient Indexing

Although UUIDs are generally random, the sequential timestamp portion reduces index fragmentation in databases like PostgreSQL, MySQL, or SQL Server.

4. Distributed Environments

IDs can be generated across multiple servers without collision.
Useful for microservices architectures or replicated databases.

5. Security and Privacy

The random portion makes IDs unpredictable, unlike sequential auto-increment IDs.
The only problem ... is 36bytes string/text increases the size of db... and may be the speed.... from the other hand if it is a big db the limit of int (4 or 8 bytes? - 8bytes isn't limit) is the problem of INT autoincrement
 

TILogistic

Expert
Licensed User
Longtime User
The only problem ... is 36bytes string/text increases the size of db... and may be the speed.... from the other hand if it is a big db the limit of int (4 or 8 bytes? - 8bytes isn't limit) is the problem of INT autoincrement
This class was extracted from a process in another language that generates millions of transactions, and its use has proven highly efficient for querying and inserting data into tables.

For context, it was implemented in a Terminal Operating System (TOS).
 

TILogistic

Expert
Licensed User
Longtime User
I will soon publish another class that generates ULIDs that were extracted from a process in another language.

1773391936517.png
 
Top