UUID

永井 忠一 2024.10.2


コマンドによる生成

マニュアルページ「$ man uuidgen」

UUIDGEN(1)                       User Commands                      UUIDGEN(1)

NAME
       uuidgen - create a new UUID value

SYNOPSIS
       uuidgen [options]

DESCRIPTION
       The uuidgen program creates (and prints) a new universally unique
       identifier (UUID) using the libuuid(3) library. The new UUID can
       reasonably be considered unique among all UUIDs created on the local
       system, and among UUIDs created on other systems in the past and in the
       future.

       There are three types of UUIDs which uuidgen can generate: time-based
       UUIDs, random-based UUIDs, and hash-based UUIDs. By default uuidgen
       will generate a random-based UUID if a high-quality random number
       generator is present. Otherwise, it will choose a time-based UUID. It
       is possible to force the generation of one of these first two UUID
       types by using the --random or --time options.

       The third type of UUID is generated with the --md5 or --sha1 options,
      
...snip...

Perl の場合

モジュールのインストール

Linux apt
$ sudo apt install libuuid-perl

バージョンの確認(Perl ワンライナー)

$ perl -MUUID -le 'print $UUID::VERSION'
0.31

マニュアルページ「$ man uuid」

UUID(3pm)             User Contributed Perl Documentation            UUID(3pm)

NAME
       UUID - DCE compatible Universally Unique Identifier library for Perl

SYNOPSIS
           use UUID 'uuid';

           $string = uuid();   # generate stringified UUID

           UUID::generate($uuid);               # new binary UUID; prefer random
           UUID::generate_random($uuid);        # new binary UUID; use random
           UUID::generate_time($uuid);          # new binary UUID; use time

           UUID::unparse($uuid, $string);       # stringify $uuid; system casing
           UUID::unparse_lower($uuid, $string); # force lowercase stringify
           UUID::unparse_upper($uuid, $string); # force uppercase stringify

           $rc = UUID::parse($string, $uuid);   # map string to UUID; -1 on error

           UUID::copy($dst, $src);              # copy binary UUID from $src to $dst
...snip...

(Python では、UUID は標準ライブラリに含まれる)

Perl と Python から

UUID を生成

PerlPython
use UUID qw(uuid);

print(uuid() . "\n");
import uuid

print(uuid.uuid4())

どちらも乱数による UUID を生成している(UUID のバージョンは 4)

(私の環境では、Perl の uuid() が呼んでいる generate() 関数は、「/dev/urandom」を利用できている。また、新しいバージョンでは、(Python と同じように、)uuid4() が用意される)

JavaScript の例《追記》

JavaScript
console.log(crypto.randomUUID())

ドキュメント


© 2024 Tadakazu Nagai