gleam-foundry/apiculture

GitHub: gleam-foundry/apiculture

为 Gleam 语言提供密码学安全的随机密钥生成库,支持多种编码格式、语义前缀和 CRC-32 校验和。

Stars: 0 | Forks: 0

# 养蜂业 ![apiculture banner](https://static.pigsec.cn/wp-content/uploads/repos/cas/b0/b0a8aca54d853868d2e745fc44b90a49cfd297742d250a4d6048ec087992e2f7.png) [![包版本](https://img.shields.io/hexpm/v/apiculture)](https://hex.pm/packages/apiculture) [![Hex 文档](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/apiculture/) 为 Gleam 提供密码学安全的随机密钥生成功能,支持多种编码和校验和。 ## 安装说明 ``` gleam add apiculture ``` ## 依赖项 - **gleam_crypto** - 密码学安全的随机字节(在 Erlang 上使用 `crypto:strong_rand_bytes`,在 JavaScript 上使用 Web Crypto API) - **yabase** - Base 编码/解码(base16、base32、base36、base58、base62、base64) - **crc32** - 用于数据完整性校验的 CRC-32 校验和 ## 快速入门 ``` import apiculture as ab // Generate a random key with a semantic prefix let assert Ok(key) = ab.default_prefixed("sk") ab.key_value(key) // => "sk_3q2Z7x9P2R8LmNkJd4Hf6Y1Bo5VsGcT7EwKu0IqXp3Zs6Ud" // Generate raw random bytes encoded in hex let assert Ok(key) = ab.key_new() |> ab.key_with_random_bytes(16) |> ab.key_with_encoding(ab.hex_lower()) |> ab.key_generate ab.key_value(key) // => "deadbeef12345678abcdef" ``` ## 两种截然不同的操作 本库支持两种在数学上截然不同的操作: ### 1. 随机字节生成 生成密码学安全的随机字节并对其进行编码: ``` import apiculture as ab let assert Ok(key) = ab.key_new() |> ab.key_with_random_bytes(32) |> ab.key_with_encoding(ab.base64()) |> ab.key_generate ab.key_value(key) ``` ### 2. 直接字母表采样 通过直接从字母表中进行采样来生成随机字符,并使用拒绝采样来避免模运算偏差: ``` import apiculture as ab import apiculture/alphabet let assert Ok(key) = ab.key_new() |> ab.key_with_random_chars(24) |> ab.key_with_alphabet(alphabet.base58()) |> ab.key_generate ab.key_value(key) ``` ## 内置编码 | 编码 | 描述 | |----------|-------------| | `hex_lower()` / `hex_upper()` | 十六进制 | | `base32_rfc()` / `base32_rfc_unpadded()` | RFC 4648 Base32 | | `base32_hex()` | Base32hex | | `base32_crockford()` | Crockford 的 Base32 | | `base32_z()` | z-base-32 | | `base36()` | Base36 | | `base58()` | Bitcoin Base58 | | `base62()` | 标准 Base62 | | `base64()` / `base64_url()` | Base64 / URL 安全的 Base64 | ## 内置字母表 ``` import apiculture/alphabet // For direct character sampling alphabet.base58() // Bitcoin-style (excludes 0, O, I, l) alphabet.base62() // Standard (0-9, A-Z, a-z) alphabet.human_safe() // Excludes visually ambiguous chars ``` ## 校验和 为密钥添加完整性校验: ``` import apiculture as ab let assert Ok(key) = ab.key_new() |> ab.key_with_random_bytes(16) |> ab.key_with_encoding(ab.base62()) |> ab.key_with_checksum(ab.crc32()) |> ab.key_generate ``` ## 版本控制策略 - **0.1.x**: 初始开发阶段。可能会发生破坏性的 API 变更。 - **Bug 修复**: Patch 版本号增加 - **新功能**: Minor 版本号增加 - **破坏性变更**: Minor 版本号增加 在 API 经历至少一个实际消费项目的检验后,该包将达到 `1.0.0` 版本。 ## 许可证 MIT 许可证 - 版权所有 (c) 2026 Antonio Ognio 在 🇵🇪 满怀 ❤️ 制作。El Perú es clave 🔑。
标签:Gleam, 密码学, 密钥生成, 开发库, 手动系统调用, 数据可视化, 编码工具, 随机数生成