3 ms·
This is very similar to how I generate IDs in a project I'm working on. Example: |-A-|-|------------B--------------| NMSPC-9TWN1-HR7SV-MTX00-0H8VP-YCC
by jszymborski 3y ago
This is very similar to how I generate IDs in a project I'm working on.
Example:
|-A-|-|------------B--------------|
NMSPC-9TWN1-HR7SV-MTX00-0H8VP-YCCJZ
A = Namespace, padded to 5 chars. Max 5 chars. Uppercase.
B = Blake3 hashed microtime with a random key.
I like how it folds in a time component but that it also doesn't reveal the time it was generated.
Here's the snippet:
https://gist.github.com/jszym/d3c7907b7b6e916f68205c99e5e48922 https://gist.github.com/jszym/d3c7907b7b6e916f68205c99e5e489...
- goostavos 3y agoNamespacing identifiers in general is a great idea for handling those class of integration tests which cannot be fully isolated. It makes it easy to write all kinds of garbage from even concurrently running tests all without any of them colliding or accidentally reading each others writes (because they are themselves namespace aware!). It's low effort to get all the pieces of your system to play along (often entirely transparent via DI), but gives a huge power to weight ratio. Basically deletes an entire class of problems which usually plague large, mature test suits
- rowbin 3y agoDo you generate a random key for each id? If so, you loose the time component of your I'd, because two hashes made at a different time can produce the same hash when using a different key (I think). If you use a single key you need to manage it and keep it secret which isn't really ideal for something simple like id's. If you reveal the key you could as well use an unkeyed hash, right? Maybe a salted hash with the same salt for every key to make dictionary attacks on the creation time harder. But then again it's probably easier to just generate random bits for the id, as you don't have sortability in any case.