> ## Documentation Index
> Fetch the complete documentation index at: https://fluxon.tabooproject.org/llms.txt
> Use this file to discover all available pages before exploring further.

# 加解密与编码函数

> fs:crypto 命名空间下的 hash/base64/unicode/hex 工具

当前模块聚合了哈希、Base64、Unicode 与十六进制工具。
引用前需在脚本顶部导入命名空间：

```fluxon theme={null}
import 'fs:crypto'
```

导入后即可通过上下文调用（`::`）访问四个对象：

| 对象        | 作用                                  |
| --------- | ----------------------------------- |
| `hash`    | 产出常见指纹（MD5 / SHA 系列），结果均为小写十六进制字符串。 |
| `base64`  | 按指定字符集对文本进行 Base64 编解码，默认 UTF-8。    |
| `unicode` | 把非 ASCII 字符转换为 `\uXXXX`，或从转义文本还原。   |
| `hex`     | 在原始文本与 UTF-8 十六进制串间转换。              |

<Note>所有函数内部都固定使用 UTF-8 处理字节流；如 Base64 提供了可选的自定义字符集参数。</Note>

## 哈希函数

| 函数       | 签名                   | 说明                           |
| -------- | -------------------- | ---------------------------- |
| `md5`    | `hash::md5(text)`    | 计算 UTF-8 字节的 MD5，常用于快速一致性比对。 |
| `sha1`   | `hash::sha1(text)`   | 生成 SHA-1 指纹。                 |
| `sha256` | `hash::sha256(text)` | 256 位 SHA 指纹。                |
| `sha384` | `hash::sha384(text)` | 384 位 SHA 指纹，更适合较高安全性。       |
| `sha512` | `hash::sha512(text)` | 512 位 SHA 指纹。                |
| 示例：      |                      |                              |

```fluxon theme={null}
token = hash::sha256(&userId + ":" + &nonce)
```

## Base64

| 函数       | 签名                              | 说明                                                          |
| -------- | ------------------------------- | ----------------------------------------------------------- |
| `encode` | `base64::encode(text,charset?)` | 将文本编码为 Base64。`charset` 留空时默认 `UTF-8`，若指定字符集不可用会自动退回 UTF-8。 |
| `decode` | `base64::decode(data,charset?)` | 解码 Base64 文本，并以指定字符集返回字符串。输入不是合法 Base64 时会抛出运行时异常。          |

```fluxon theme={null}
secret = base64::encode(&json, 'UTF-8')
original = base64::decode(&secret)
```

## Unicode

| 函数       | 签名                      | 说明                                        |
| -------- | ----------------------- | ----------------------------------------- |
| `encode` | `unicode::encode(text)` | 把所有非 ASCII 字符写成 `\uXXXX` 形式，ASCII 字符保持原样。 |
| `decode` | `unicode::decode(text)` | 将合法的 `\uXXXX` 片段替换为真实字符，遇到无效片段时保持原文。      |

```fluxon theme={null}
escaped = unicode::encode('你好 Fluxon')
restored = unicode::decode(&escaped)
```

## 十六进制 `hex::*`

| 函数       | 签名                       | 说明                                              |
| -------- | ------------------------ | ----------------------------------------------- |
| `encode` | `hex::encode(text)`      | 使用 UTF-8 获取字节并输出小写十六进制。                         |
| `decode` | `hex::decode(hexString)` | 将十六进制文本还原为 UTF-8 字符串；字符串长度必须为偶数，且仅允许 0-9 / A-F。 |

```fluxon theme={null}
payload = hex::encode('OK')       # => 4f4b
hex::decode(&payload)             # => OK
```

## 相关链接

* [函数目录（canonical）](/appendix/function-catalog)
* [系统函数](/language/functions/system)
