编码与哈希一体化接口。单端点支持 Base64 / Base64URL / URL / Hex 四种编解码,以及 MD5 / SHA-1 / SHA-256 / SHA-512 四种哈希算法。
鉴权
所有接口均需在请求头中携带访问令牌:
Authorization: Bearer tf_xxxxxxxxxxxxxxxx
令牌在 用户设置 → 访问令牌 页面创建,创建时需勾选对应权限:
| 权限 | 说明 |
|---|---|
read:tools_codec |
调用编码哈希接口 |
频率限制
| 范围 | 上限 |
|---|---|
| 单用户 / 单分钟 | 60 次 |
| 单用户 / 单日 | 2000 次 |
超限返回 429。
接口列表
1. 编码 / 解码 / 哈希
POST /api/tools/codec/process
所需权限:read:tools_codec
通过 action 字段切换不同处理动作,使用 options 进一步指定编码格式或哈希算法。
请求体(JSON)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
action |
string | 是 | 取值:encode / decode / hash |
input |
string | 是 | 输入字符串。按 UTF-8 处理:encode / hash 将其序列化为字节后再处理,decode 将解码结果按 UTF-8 还原文本 |
options |
object | 是 | 见下方各 action 子参数 |
options 字段
| 字段 | 类型 | 适用 action | 取值 |
|---|---|---|---|
format |
string | encode、decode |
base64 / base64url / url / hex |
algorithm |
string | hash |
md5 / sha1 / sha256 / sha512 |
格式说明
| 格式 | 编码规则 |
|---|---|
base64 |
标准 Base64,输出含 padding |
base64url |
URL-safe 变体:- _ 替换 + /,去 padding |
url |
encodeURIComponent / decodeURIComponent |
hex |
编码输出小写 hex;解码允许 0x 前缀和空白,长度必须为偶数 |
返回值
encode / decode
{
"success": true,
"data": {
"format": "base64",
"output": "aGVsbG8gd29ybGQ="
}
}
| 字段 | 类型 | 说明 |
|---|---|---|
format |
string | 回显请求中的 format |
output |
string | 编码或解码后的字符串 |
hash
{
"success": true,
"data": {
"algorithm": "sha256",
"output": "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
"output_base64": "LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=",
"input_bytes": 5
}
}
| 字段 | 类型 | 说明 |
|---|---|---|
algorithm |
string | 回显请求中的 algorithm |
output |
string | 哈希值(小写 hex) |
output_base64 |
string | 同一哈希的 base64 表示 |
input_bytes |
integer | 输入按 UTF-8 序列化后的字节数 |
示例
# Base64 编码
curl -X POST "https://trendforge.devlive.top/api/tools/codec/process" \
-H "Authorization: Bearer tf_xxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{"action":"encode","input":"hello world","options":{"format":"base64"}}'
# Base64URL 解码
curl -X POST "https://trendforge.devlive.top/api/tools/codec/process" \
-H "Authorization: Bearer tf_xxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{"action":"decode","input":"aGVsbG8gd29ybGQ","options":{"format":"base64url"}}'
# URL 编码
curl -X POST "https://trendforge.devlive.top/api/tools/codec/process" \
-H "Authorization: Bearer tf_xxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{"action":"encode","input":"hello 中文 & =","options":{"format":"url"}}'
# Hex 解码(支持 0x 前缀和空白)
curl -X POST "https://trendforge.devlive.top/api/tools/codec/process" \
-H "Authorization: Bearer tf_xxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{"action":"decode","input":"0x68 65 6c 6c 6f","options":{"format":"hex"}}'
# SHA-256 哈希
curl -X POST "https://trendforge.devlive.top/api/tools/codec/process" \
-H "Authorization: Bearer tf_xxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{"action":"hash","input":"hello","options":{"algorithm":"sha256"}}'
错误响应
所有错误均返回 JSON 格式:
{
"success": false,
"code": 400,
"message": "输入不合法",
"detail": {
"message": "invalid base64"
}
}
| HTTP 状态码 | code |
原因 |
|---|---|---|
400 |
400 | action 不在三个枚举内 / format 或 algorithm 不支持 / Base64 / Base64URL / Hex 输入不合法 |
401 |
401 | 未携带令牌或令牌无效 |
403 |
403 | 令牌缺少 read:tools_codec 权限 |
429 |
429 | 命中分钟级或日级频率限制 |
500 |
500 | 服务器内部错误 |
数据说明
- 单次调用为内存计算,不持久化、不缓存、不上报第三方
- 输入按 UTF-8 文本处理;如需对二进制数据求哈希,请先在客户端转 hex 或 base64,再用
decode+hash两步组合(暂不在同一次请求里完成) decode/base64与decode/base64url严格校验输入字符集与 round-trip 等值,避免错认非法字符串decode/hex自动剔除0x前缀与空白;长度奇数或含非 hex 字符直接拒绝url编解码遵循 RFC 3986(即浏览器encodeURIComponent/decodeURIComponent行为)hash输出 hex 与 base64 双格式,方便不同场景直接使用- 同一令牌的频率配额与同一登录会话共享