20
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Node.jsでUUIDを生成する(crypto.randomUUID)

Last updated at Posted at 2022-08-24

Node.js 14.17.0以降

サードパーティライブラリを使わずとも、標準のCryptoモジュールでUUID v4を生成できる。

const crypto = require("crypto");
console.log(crypto.randomUUID());
// => 49affff9-0216-46ed-902c-b3324fd6d094

Node.js 14.17.0以前

npmのuuidモジュールを使う。
https://www.npmjs.com/package/uuid

ブラウザの場合

ブラウザでも crypto.randomUUID() が使える。

上記いずれも使えない場合

こちらで下記コードが紹介されている。これが実用に耐えるものなのかは分からない。

const crypto = require('crypto');

const UUIDGeneratorNode = () =>
  ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
    (c ^ (crypto.randomBytes(1)[0] & (15 >> (c / 4)))).toString(16)
  );

console.log(UUIDGeneratorNode()); // => 0444ae6e-2226-4ca7-9b66-b8d60dce2333

ざっくり解析すると、
まず [1e7] + -1e3 + -4e3 + -8e3 + -1e11 により'10000000-1000-4000-8000-100000000000'という文字列が生成される。
この文字列のうち、0, 1, 8の部分を1文字ずつランダムに置換する。
4を置換しないのは、この位置はUUIDのバージョンを表し、UUID v4では4で固定だから(参考:wikipedia)。
同様に8の位置はバリアントを表し、c=8ならc ^ (crypto.randomBytes(1)[0] & (15 >> (c / 4)))は8, 9, a, bのいずれかになる。つまりバリアント1を意味する。
1と0の部分で置換結果に差異はないようだ。置換前文字列を 1e71e3 で生成しているから0と1が出てきているだけで、特に意味は無いと思われる。なので、関数の定義を

const UUIDGeneratorNode2 = () =>
  '00000000-0000-4000-8000-000000000000'.replace(/[08]/g, c =>
    (c ^ (crypto.randomBytes(1)[0] & (15 >> (c / 4)))).toString(16)
  );

と変えても結果は同じだと思われる。

20
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?