crypto-random-string、nanoid、randomstring はいずれもランダム文字列を生成するnpmパッケージですが、その目的と設計思想は大きく異なります。crypto-random-string と nanoid は暗号論的に安全な乱数を基にした文字列生成を提供し、セキュリティが求められる用途(例: トークン、ID)に適しています。一方、randomstring は非暗号的安全な乱数(Math.random())を使用しており、主にテストデータやプレースホルダーなど、セキュリティが不要な場面での利用を想定しています。これらのライブラリは、フロントエンドおよびNode.js環境での使用をサポートしていますが、それぞれの特性を理解して適切に選択することが重要です。
フロントエンド開発において、一意なIDやセキュアなトークン、あるいは単なるプレースホルダー文字列を生成する必要がよくあります。crypto-random-string、nanoid、randomstringはいずれもランダム文字列を生成するnpmパッケージですが、目的・設計思想・使用可能な環境が大きく異なります。この記事では、実際のコード例を交えながら、それぞれの技術的特性と適切な使いどころを詳しく解説します。
crypto-random-string は、Node.jsの crypto.randomBytes() またはブラウザの crypto.getRandomValues() を内部で使用しており、暗号論的に安全な乱数に基づいて文字列を生成します。これはパスワードリセットトークンやCSRFトークンなど、予測不能性が求められる用途に最適です。
// crypto-random-string
import cryptoRandomString from 'crypto-random-string';
const token = cryptoRandomString({ length: 32, type: 'url-safe' });
// 例: "xK9m2pLq8sRvT4wYzA7bNcE5fGhJkM"
nanoid も同様に、環境に応じて crypto.getRandomValues()(ブラウザ)または crypto.randomBytes()(Node.js)を使用し、暗号論的に安全なIDを生成します。さらに、URLセーフで衝突確率が極めて低い短いIDを高速に生成できる点が特徴です。
// nanoid
import { nanoid } from 'nanoid';
const id = nanoid(); // デフォルト長は21
// 例: "V1StGXR8_Z5jdHi6B-myT"
一方、randomstring は 暗号論的に安全ではありません。内部で Math.random() を使用しているため、予測可能な乱数を生成します。これはテスト用のダミーデータやUIのプレースホルダーなど、セキュリティが不要な場面でのみ使用すべきです。
// randomstring
import randomstring from 'randomstring';
const placeholder = randomstring.generate(10);
// 例: "aB3xK9mLpQ" — ただし予測可能
⚠️ 注意:
randomstringはセキュリティ要件のある用途(例: トークン、キー、パスワード)には絶対に使用しないでください。
crypto-random-string は、Node.js とモダンブラウザ(crypto APIをサポートするもの)の両方で動作します。バンドルツール(Webpack、Viteなど)と連携して、環境に応じた実装を自動選択します。
nanoid も同様に、ブラウザとNode.jsの両方で動作します。さらに、軽量かつ高速な設計のため、フロントエンドでの使用に非常に適しています。ReactやVueなどのフレームワークと組み合わせて、コンポーネント内でIDを生成するユースケースで広く使われています。
randomstring は主にNode.js向けに設計されていますが、ブラウザでも動作します(Math.random() に依存しているため)。ただし、前述の通りセキュリティリスクがあるため、フロントエンドでの使用は推奨されません。
crypto-random-string はシンプルで一貫性のあるAPIを提供します。type オプションで事前定義された文字セット(hex, base64, url-safe, distinguishable など)を選べます。カスタム文字セットも指定可能です。
// crypto-random-string: カスタム文字セット
const custom = cryptoRandomString({
length: 10,
characters: 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'
});
nanoid はデフォルトでURLセーフな64文字アルファベット(-_.~ を含む)を使用し、衝突確率を最小限に抑えています。カスタムアルファベットや長さも指定できますが、主なユースケースは「短くて一意なID」の生成です。
// nanoid: カスタム長とアルファベット
import { customAlphabet } from 'nanoid';
const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const generateId = customAlphabet(alphabet, 10);
const id = generateId();
randomstring は非常に豊富なオプションを持ち、長さ、文字種別(英字、数字、記号)、読みやすさ(readable モード)などを細かく制御できます。ただし、すべてが非暗号的安全である点に注意が必要です。
// randomstring: 豊富なオプション
const readable = randomstring.generate({
length: 12,
charset: 'alphabetic',
capitalization: 'lowercase'
});
// 例: "qwertyasdfgh"
nanoid は極めて軽量(1KB未満)で、生成速度も非常に高速です。これは、ID生成を頻繁に行うアプリケーション(例: リアルタイムチャットのメッセージID)に最適です。
crypto-random-string も軽量ですが、nanoid より若干大きい傾向があります。ただし、セキュリティ重視の用途ではこの差は無視できます。
randomstring は機能が多いため、相対的にバンドルサイズが大きくなります。また、Math.random() による生成は高速ですが、セキュリティ上の理由からフロントエンドでの使用は避けるべきです。
crypto-random-stringurl-safe タイプがそのままトークンとして使える。const csrfToken = cryptoRandomString({ length: 32, type: 'url-safe' });
nanoidfunction ListItem({ item }) {
const id = nanoid();
return <div key={id}>{item.name}</div>;
}
randomstringconst username = randomstring.generate({
length: 8,
charset: 'alphabetic'
});
| 特徴 | crypto-random-string | nanoid | randomstring |
|---|---|---|---|
| 暗号的安全性 | ✅ | ✅ | ❌ |
| ブラウザ対応 | ✅ | ✅ | ✅(非推奨) |
| 主な用途 | セキュアなトークン | 短い一意ID | ダミーデータ |
| APIの柔軟性 | 中 | 低(シンプル) | 高 |
| バンドルサイズ | 小 | 極小 | 中〜大 |
crypto-random-string または nanoid を選ぶ。nanoid がベストチョイス。randomstring の柔軟性を活かす。ただし、新しいプロジェクトでセキュリティ要件のある用途に randomstring を使うのは避けてください。予測可能な乱数は深刻な脆弱性につながる可能性があります。
crypto-random-string は、パスワードリセットトークンやCSRFトークンなど、暗号論的に安全な文字列が必要な場面で選ぶべきです。URLセーフや判別しやすい文字セットなど、事前定義されたタイプが豊富で、カスタム文字セットも指定可能です。セキュリティ要件が高いサーバーサイドまたはフロントエンドの両方で信頼性の高い選択肢です。
randomstring は、テスト用のダミーデータやUIのプレースホルダーなど、セキュリティが一切不要な場面でのみ使用してください。Math.random() に依存しているため、トークンやキーなどのセキュリティ用途には絶対に使ってはいけません。読みやすい文字列や柔軟な文字セット指定が必要な非セキュア用途で役立ちます。
nanoid は、Reactコンポーネントのkeyや一時的なDOM IDなど、短くて一意な識別子を高速に生成したいフロントエンド用途に最適です。バンドルサイズが極めて小さく、暗号論的に安全でありながら生成速度も非常に高速です。シンプルなAPIで「短いIDが欲しい」というニーズに素直に応えてくれます。
Generate a cryptographically strong random string
Can be useful for creating an identifier, slug, salt, PIN code, fixture, etc.
Works in Node.js and browsers.
npm install crypto-random-string
import cryptoRandomString from 'crypto-random-string';
cryptoRandomString({length: 10});
//=> '2cf05d94db'
cryptoRandomString({length: 10, type: 'base64'});
//=> 'YMiMbaQl6I'
cryptoRandomString({length: 10, type: 'url-safe'});
//=> 'YN-tqc8pOw'
cryptoRandomString({length: 10, type: 'numeric'});
//=> '8314659141'
cryptoRandomString({length: 6, type: 'distinguishable'});
//=> 'CDEHKM'
cryptoRandomString({length: 10, type: 'ascii-printable'});
//=> '`#Rt8$IK>B'
cryptoRandomString({length: 10, type: 'alphanumeric'});
//=> 'DMuKL8YtE7'
cryptoRandomString({length: 10, characters: 'abc'});
//=> 'abaaccabac'
Returns a randomized string. Hex by default.
Returns a promise which resolves to a randomized string. Hex by default.
For most use-cases, there's really no good reason to use this async version. From the Node.js docs:
The
crypto.randomBytes()method will not complete until there is sufficient entropy available. This should normally never take longer than a few milliseconds. The only time when generating the random bytes may conceivably block for a longer period of time is right after boot, when the whole system is still low on entropy.
In general, anything async comes with some overhead on it's own.
import {cryptoRandomStringAsync} from 'crypto-random-string';
await cryptoRandomStringAsync({length: 10});
//=> '2cf05d94db'
Type: object
Required
Type: number
Length of the returned string.
Type: string
Default: 'hex'
Values: 'hex' | 'base64' | 'url-safe' | 'numeric' | 'distinguishable' | 'ascii-printable' | 'alphanumeric'
Use only characters from a predefined set of allowed characters.
Cannot be set at the same time as the characters option.
The distinguishable set contains only uppercase characters that are not easily confused: CDEHKMPRTUWXY012458. It can be useful if you need to print out a short string that you'd like users to read and type back in with minimal errors. For example, reading a code off of a screen that needs to be typed into a phone to connect two devices.
The ascii-printable set contains all printable ASCII characters: !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ Useful for generating passwords where all possible ASCII characters should be used.
The alphanumeric set contains uppercase letters, lowercase letters, and digits: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789. Useful for generating nonce values.
Type: string
Minimum length: 1
Maximum length: 65536
Use only characters from a custom set of allowed characters.
Cannot be set at the same time as the type option.