nullprogram.com/blog/2010/05/11/
Yesterday
Luke pointed out an interesting idea by Jeff
Atwood:
mark code snippets you post online with a UUID. That way it's easy
to find other places where code snippets have been used, and it's
easier to find updates. It's a neat idea that I might start using.
So, the next problem is where to get a UUID. One place to get one
is createguid.com, which will
serve you up a
fresh
version 4 UUID each visit. But I don't like relying on the
Internet for something when I don't need to. This is why I sometimes
make wget backups of my favorite sites, in case they ever
disappear. By design it's easy to generate your own UUIDs, and you
have 5
algorithms to choose from, so I'll make my own.
Since I'll always be in Emacs when I need one, I'll just have Emacs do
it. However, it's a bit tricky to do in Emacs. The pseudo-random
number generator isn't very good. It's always initialized to the same
seed when it starts, so it's up to the user to reseed
somewhere. There's no direct way to read
from /dev/urandom
, instead relying on an external process
to feed it in. Relying on /dev/urandom
is also not
portable. There's also no simple, portable way to find out hardware
information about the machine, such as MAC address, required by some
versions of the UUID algorithms. Elisp integers are a few bits smaller
than native integers (29-bits on 32-bit systems), so the UUID has to
be handled in very small pieces.
I was beginning to work on my own when I found
this
great solution by Martin Blais
(mirror). Specifically
the uuid-simple
function, which takes an MD5 hash of
unique data in Emacs and uses it for the UUID. I believe this may
count as a version 3 UUID (or at least close enough that I don't
care). I modified it slightly, adding more unique data and a version
number.
This returns a string containing the UUID. Next, make an interactive
function to wrap it,
I bound this to "C-x !
", which wasn't yet bound to
anything.
Instant UUIDs in Emacs.