# @lazy-random/generators-crypto

> 以加密安全亂數來源 (Cryptographically Secure RNG) 實作的亂數產生器 (Random Number Generator)，適合需要較高不可預測性 (Entropy) 的场合

Latest version **2.0.1** (published 2026-10-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install @lazy-random/generators-crypto
pnpm add @lazy-random/generators-crypto
yarn add @lazy-random/generators-crypto
bun add @lazy-random/generators-crypto
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2026-10-05 |
| First published | 2021-12-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 6 |
| Unpacked size | 93.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | bluelovers |
| Maintainers | bluelovers |
| Keywords | crypto, cryptographically-secure, entropy, generator, generators-crypto, prng, random, random-number-generator, rng, secure-random, create-by-yarn-tool, create-by-tsdx |

## Links

- npm: https://www.npmjs.com/package/@lazy-random/generators-crypto
- Repository: https://github.com/bluelovers/ws-random
- Homepage: https://github.com/bluelovers/ws-random/tree/master/packages/@lazy-random/generators-crypto#readme
- Issues: https://github.com/bluelovers/ws-random/issues
- npm.io page: https://npm.io/package/@lazy-random/generators-crypto

## Dependencies (6)

- [@lazy-random/expect](https://npm.io/package/@lazy-random/expect.md) ^2.0.1
- [@lazy-random/shared-lib](https://npm.io/package/@lazy-random/shared-lib.md) ^2.0.1
- [@lazy-random/cross-crypto](https://npm.io/package/@lazy-random/cross-crypto.md) ^2.0.1
- [@lazy-random/rng-abstract](https://npm.io/package/@lazy-random/rng-abstract.md) ^2.0.1
- [@lazy-num/float-from-buffer](https://npm.io/package/@lazy-num/float-from-buffer.md) ^2.0.1
- [@lazy-random/array-rand-index](https://npm.io/package/@lazy-random/array-rand-index.md) ^2.0.1

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 2.0.1 (latest) — 2026-10-05
- 1.0.11 — 2023-11-19
- 1.0.9 — 2022-10-29
- 1.0.8 — 2022-01-04
- 1.0.7 — 2022-01-03
- 1.0.6 — 2021-12-21
- 1.0.4 — 2021-12-12

## README

# @lazy-random/generators-crypto

以加密安全亂數來源 (Cryptographically Secure RNG) 實作的亂數產生器 (Random Number Generator)，繼承 `@lazy-random/rng-abstract` 的 `RNG` 抽象類別，適合需要較高不可預測性 (Entropy) 的场合。

## 特色 (Features)

- 底層透過 `@lazy-random/cross-crypto` 取得跨平台的加密亂數來源 (Crypto-like Source)
- 內建可注入的 `ICryptoLike` 介面，測試時可替換成自訂亂數來源
- 提供 `next()` 取得 0～1 浮點亂數 (Float Random Number)
- 支援自訂緩衝區大小 (Buffer Size)，並夾限 (Clamp) 在 `UINT32_BYTES` ～ `255` 位元組 (Bytes)

## 安裝 (Installation)

```bash
yarn add @lazy-random/generators-crypto
yarn-tool add @lazy-random/generators-crypto
yt add @lazy-random/generators-crypto
```

## 使用方式 (Usage)

```ts
import RNGCrypto from '@lazy-random/generators-crypto';

// 使用預設的 cross-crypto 亂數來源
const rng = new RNGCrypto();

// 取得一個 0～1 的浮點亂數
const value = rng.next();

console.log(value);
```

```ts
import { RNGCrypto } from '@lazy-random/generators-crypto';

// 注入自訂的 crypto-like 物件（需具備 randomBytes(size) 方法）
const rng = new RNGCrypto({
	randomBytes(size: number)
	{
		return Buffer.alloc(size, 7);
	},
});

rng.next();
```

## API 文件 (API Documentation)

### `new RNGCrypto(seed?, opts?, ...argv)`

| 參數 (Parameter) | 型別 (Type) | 說明 (Description) |
| --- | --- | --- |
| `seed` | `ICryptoLike \| any` | 可傳入自訂的 crypto-like 物件；省略時使用 `crossCrypto()` |
| `opts` | `any` | 保留的選項參數 (Options)，目前未使用 |
| `...argv` | `any[]` | 保留的其餘參數，透傳給 `_init` |

### 方法 (Methods)

| 方法 (Method) | 回傳值 (Returns) | 說明 (Description) |
| --- | --- | --- |
| `next()` | `number` | 產生並回傳一個 0～1 的浮點亂數 |
| `seed(seed?, opts?, ...argv)` | `void` | 保留 API；加密亂數來源不支援固定亂數種子 (Seed)，目前為空實作 |

### 屬性 (Properties)

| 屬性 (Property) | 型別 (Type) | 說明 (Description) |
| --- | --- | --- |
| `name` | `string` | 固定回傳 `'crypto'` |

## 設定 (Configuration)

| 欄位 (Field) | 預設值 (Default) | 說明 (Description) |
| --- | --- | --- |
| `_seed_size` | `UINT32_BYTES`（夾限至 255） | 每次取得亂數時要求的緩衝區位元組數 |
| `_seed_size_min` | `UINT32_BYTES`（夾限至 255） | 緩衝區位元組數的下限，低於此值時會被抬高 |
| `_randIndex` | `arrayRandIndexByLength` | 當緩衝區大於下限時，用來決定截取起點的索引函式 (Index Function) |

## 開發 (Development)

```bash
pnpm run build
pnpm run test:jest
```

## 變更日誌 (Changelog)

詳見 [CHANGELOG.md](./CHANGELOG.md)。

## 相關資源 (Related Resources)

- [@lazy-random/rng-abstract](https://www.npmjs.com/package/@lazy-random/rng-abstract)
- [@lazy-random/cross-crypto](https://www.npmjs.com/package/@lazy-random/cross-crypto)

---
_Source: https://npm.io/package/@lazy-random/generators-crypto · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
