介绍
# OpenSSL Secure Generation
使用 `openssl rand` 生成加密安全的随机数据。
## 密码/密钥生成
```bash # 32 random bytes as base64 (43 chars, URL-safe with tr) openssl rand -base64 32 | tr '+/' '-_' | tr -d '='
# 24 random bytes as hex (48 chars) openssl rand -hex 24
# alphanumeric password (32 chars) openssl rand -base64 48 | tr -dc 'a-zA-Z0-9' | head -c 32 ```
## 常用长度
| 使用场景 | 命令 | |----------|---------| | 密码(强) | `openssl rand -base64 24` | | API 密钥 | `openssl rand -hex 32` | | 会话令牌 | `openssl rand -base64 48` | | 短 PIN 码(8 位) | `openssl rand -hex 4 | xxd -r -p | od -An -tu4 | tr -d ' ' | head -c 8` |
## 注意事项
- `-base64` 输出的字符数约为字节数的 1.33 倍 - `-hex` 输出的字符数是字节数的 2 倍 - 通过管道传输给 `tr -dc` 来过滤字符集 - 密钥至少应使用 16 字节(128 位)