3ncr/tokencrypt-php
Composer 安装命令:
composer require 3ncr/tokencrypt-php
包简介
Implementation of 3ncr.org V1 token(strings) encryption standard.
README 文档
README
3ncr.org is a tiny encrypted-string format for config values, tokens, and app secrets — encrypt a UTF-8 string in one language, store it as a recognizable value, and decrypt it in another. v1 uses AES-256-GCM for authenticated encryption with a 12-byte random IV:
3ncr.org/1#<base64(iv[12] || ciphertext || tag[16])>
Encrypted values look like
3ncr.org/1#pHRufQld0SajqjHx+FmLMcORfNQi1d674ziOPpG52hqW5+0zfJD91hjXsBsvULVtB017mEghGy3Ohj+GgQY5MQ.
This is the PHP 8.1+ implementation. See github.com/3ncr for implementations in other languages (Go, Node.js, Python, Rust, Java, C#, Ruby).
Install
composer require 3ncr/tokencrypt-php
Requires PHP 8.1+ with ext-openssl, ext-json, and ext-sodium (the last is
used for Argon2id key derivation).
Usage
Pick a constructor based on the entropy of your secret — see the 3ncr.org v1 KDF guidance for the canonical recommendation.
Recommended: raw 32-byte key (high-entropy secrets)
If you already have a 32-byte AES-256 key (random key, API token hashed to 32 bytes via SHA3-256, etc.), skip the KDF and pass it directly.
$key = random_bytes(32); // or: load from env / secret store $tokenCrypt = \ThreeEncr\TokenCrypt::fromRawKey($key);
Recommended: Argon2id (passwords / low-entropy secrets)
For passwords or passphrases, use fromArgon2id. It uses the parameters
recommended by the 3ncr.org v1 spec
(m=19456 KiB, t=2, p=1). The salt must be exactly 16 bytes (libsodium's
crypto_pwhash requirement, which matches the spec's "at least 16 random
bytes" recommendation at its minimum length and is interoperable with the Go
and Node implementations when the same salt is used).
$tokenCrypt = \ThreeEncr\TokenCrypt::fromArgon2id($password, $salt);
Legacy: PBKDF2-SHA3 (existing data only)
The original (secret, salt, iterations) constructor is kept for backward
compatibility with data encrypted by earlier versions. It is deprecated —
prefer fromRawKey or fromArgon2id for new code.
$tokenCrypt = new \ThreeEncr\TokenCrypt($secret, $salt, 1000);
$secret and $salt are inputs to PBKDF2-SHA3 (technically one is the key,
the other is the salt, but you need to store them both somewhere, preferably
in different places). 1000 is the number of PBKDF2 rounds.
Encrypt / decrypt
After constructing an instance, use encrypt3ncr and decrypt3ncr:
$token = '08019215-B205-4416-B2FB-132962F9952F'; // your secret you want to encrypt $encryptedSecretToken = $tokenCrypt->encrypt3ncr($token); // $encryptedSecretToken === '3ncr.org/1#pHRufQld0SajqjHx+FmLMcORfNQi1d674ziOPpG52hqW5+0zfJD91hjXsBsvULVtB017mEghGy3Ohj+GgQY5MQ' // ... some time later in another context ... $decryptedSecretToken = $tokenCrypt->decrypt3ncr($encryptedSecretToken); // $decryptedSecretToken === '08019215-B205-4416-B2FB-132962F9952F'
decrypt3ncr returns the input unchanged when it does not start with the
3ncr.org/1# header, so it is safe to route every configuration value through
it regardless of whether it was encrypted.
For JSON config files you can decrypt all 3ncr-encoded values in one pass:
$encConfig = json_decode(file_get_contents('config.json'), true); $config = $tokenCrypt->decrypt3ncrArray($encConfig);
License
MIT — see LICENSE.
3ncr/tokencrypt-php 适用场景与选型建议
3ncr/tokencrypt-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.96k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 3ncr/tokencrypt-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 3ncr/tokencrypt-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 18.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-02-03