jtdowney/kryptos
GitHub: jtdowney/kryptos
kryptos是一个针对Gleam的加密库,支持Erlang和JavaScript。
Stars: 15 | Forks: 0
# kryptos
[](https://hex.pm/packages/kryptos)
[](https://hexdocs.pm/kryptos/)
一个针对 Gleam 的加密库,旨在支持 Erlang 和 JavaScript 运行时。
## 为什么选择 kryptos?
- 在 Erlang 和 JavaScript (Node.js) 上运行,封装每个平台的本地加密 API 以实现一致的行为。
- 所有加密操作都委托给运行时的实现(Erlang 的 `crypto` 模块,Node.js 的 `crypto` 模块)。
- 受 Go 的加密库启发,API 引导您选择安全的默认值,并使危险操作明确化。
- 经 Google 的 [Wycheproof](https://github.com/C2SP/wycheproof) 测试向量验证,以捕获边缘情况漏洞。
## 安装
```
gleam add kryptos
```
## 要求
- Erlang/OTP 27 或更高版本,用于 Erlang 目标
- Node.js 22 或更高版本,用于 JavaScript 目标
## 功能
| 模块 | 算法 |
| -------------------------------------------------------------------- | ------------------------------------------------------- |
| [aead](https://hexdocs.pm/kryptos/kryptos/aead.html) | AES-GCM, AES-CCM, ChaCha20-Poly1305, XChaCha20-Poly1305 |
| [block](https://hexdocs.pm/kryptos/kryptos/block.html) | AES-128, AES-192, AES-256, AES Key Wrap (RFC 3394) |
| [crypto](https://hexdocs.pm/kryptos/kryptos/crypto.html) | HKDF, PBKDF2, 随机字节 |
| [ec](https://hexdocs.pm/kryptos/kryptos/ec.html) | 密钥生成、导入/导出、内省 |
| [ecdh](https://hexdocs.pm/kryptos/kryptos/ecdh.html) | P-256, P-384, P-521, secp256k1 |
| [ecdsa](https://hexdocs.pm/kryptos/kryptos/ecdsa.html) | P-256, P-384, P-521, secp256k1 |
| [eddsa](https://hexdocs.pm/kryptos/kryptos/eddsa.html) | Ed25519, Ed448 |
| [hash](https://hexdocs.pm/kryptos/kryptos/hash.html) | SHA-1, SHA-2, SHA-3, SHAKE, BLAKE2 |
| [hmac](https://hexdocs.pm/kryptos/kryptos/hmac.html) | 所有哈希算法 |
| [rsa](https://hexdocs.pm/kryptos/kryptos/rsa.html) | OAEP, PKCS#1 v1.5, PSS, PKCS#1 v1.5 签名 |
| [x509/certificate](https://hexdocs.pm/kryptos/kryptos/x509/certificate.html) | 证书解析和自签名生成 |
| [x509/csr](https://hexdocs.pm/kryptos/kryptos/x509/csr.html) | 使用 ECDSA/RSA/EdDSA 生成 CSR,SANs |
| [xdh](https://hexdocs.pm/kryptos/kryptos/xdh.html) | X25519, X448 |
## 入门
使用 AES-GCM 加密和解密数据:
```
import kryptos/aead
import kryptos/block
import kryptos/crypto
pub fn main() {
// Generate a random 256-bit key
let assert Ok(cipher) = block.aes_256(crypto.random_bytes(32))
let ctx = aead.gcm(cipher)
// Generate a random nonce (never reuse with the same key!)
let nonce = crypto.random_bytes(aead.nonce_size(ctx))
// Encrypt
let plaintext = <<"hello, world!":utf8>>
let assert Ok(#(ciphertext, tag)) = aead.seal(ctx, nonce:, plaintext:)
// Decrypt
let assert Ok(decrypted) = aead.open(ctx, nonce:, ciphertext:, tag:)
// decrypted == plaintext
}
```
## 安全性
有关选择加密原语的建议,请参阅
[加密正确答案](https://latacora.micro.blog/2018/04/03/cryptographic-right-answers.html).
- 在 Erlang 和 JavaScript (Node.js) 上运行,封装每个平台的本地加密 API 以实现一致的行为。
- 所有加密操作都委托给运行时的实现(Erlang 的 `crypto` 模块,Node.js 的 `crypto` 模块)。
- 受 Go 的加密库启发,API 引导您选择安全的默认值,并使危险操作明确化。
- 经 Google 的 [Wycheproof](https://github.com/C2SP/wycheproof) 测试向量验证,以捕获边缘情况漏洞。
## 安装
```
gleam add kryptos
```
## 要求
- Erlang/OTP 27 或更高版本,用于 Erlang 目标
- Node.js 22 或更高版本,用于 JavaScript 目标
## 功能
| 模块 | 算法 |
| -------------------------------------------------------------------- | ------------------------------------------------------- |
| [aead](https://hexdocs.pm/kryptos/kryptos/aead.html) | AES-GCM, AES-CCM, ChaCha20-Poly1305, XChaCha20-Poly1305 |
| [block](https://hexdocs.pm/kryptos/kryptos/block.html) | AES-128, AES-192, AES-256, AES Key Wrap (RFC 3394) |
| [crypto](https://hexdocs.pm/kryptos/kryptos/crypto.html) | HKDF, PBKDF2, 随机字节 |
| [ec](https://hexdocs.pm/kryptos/kryptos/ec.html) | 密钥生成、导入/导出、内省 |
| [ecdh](https://hexdocs.pm/kryptos/kryptos/ecdh.html) | P-256, P-384, P-521, secp256k1 |
| [ecdsa](https://hexdocs.pm/kryptos/kryptos/ecdsa.html) | P-256, P-384, P-521, secp256k1 |
| [eddsa](https://hexdocs.pm/kryptos/kryptos/eddsa.html) | Ed25519, Ed448 |
| [hash](https://hexdocs.pm/kryptos/kryptos/hash.html) | SHA-1, SHA-2, SHA-3, SHAKE, BLAKE2 |
| [hmac](https://hexdocs.pm/kryptos/kryptos/hmac.html) | 所有哈希算法 |
| [rsa](https://hexdocs.pm/kryptos/kryptos/rsa.html) | OAEP, PKCS#1 v1.5, PSS, PKCS#1 v1.5 签名 |
| [x509/certificate](https://hexdocs.pm/kryptos/kryptos/x509/certificate.html) | 证书解析和自签名生成 |
| [x509/csr](https://hexdocs.pm/kryptos/kryptos/x509/csr.html) | 使用 ECDSA/RSA/EdDSA 生成 CSR,SANs |
| [xdh](https://hexdocs.pm/kryptos/kryptos/xdh.html) | X25519, X448 |
## 入门
使用 AES-GCM 加密和解密数据:
```
import kryptos/aead
import kryptos/block
import kryptos/crypto
pub fn main() {
// Generate a random 256-bit key
let assert Ok(cipher) = block.aes_256(crypto.random_bytes(32))
let ctx = aead.gcm(cipher)
// Generate a random nonce (never reuse with the same key!)
let nonce = crypto.random_bytes(aead.nonce_size(ctx))
// Encrypt
let plaintext = <<"hello, world!":utf8>>
let assert Ok(#(ciphertext, tag)) = aead.seal(ctx, nonce:, plaintext:)
// Decrypt
let assert Ok(decrypted) = aead.open(ctx, nonce:, ciphertext:, tag:)
// decrypted == plaintext
}
```
## 安全性
有关选择加密原语的建议,请参阅
[加密正确答案](https://latacora.micro.blog/2018/04/03/cryptographic-right-answers.html).标签:AES, API设计, ChaCha20, CMS安全, Cryptography, ECDH, Erlang, Gleam, GNU通用公共许可证, HKDF, JavaScript, Node.js, PBKDF2, Wycheproof, 依赖安装, 加密库, 加密算法, 安全测试, 安全默认, 攻击性安全, 数据可视化, 椭圆曲线, 软件包管理, 随机数生成