Skip to content

Commit eb55b0a

Browse files
authored
add more zh-cn translation 02-15 (#120)
Close #104
1 parent a0ad7ff commit eb55b0a

33 files changed

+640
-1
lines changed

src/zh-CN/02-01-sha-digest.smd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
.title = "计算文件的 SHA-256 摘要",
3+
.date = "2024-01-01",
4+
.author = "ZigCC",
5+
.layout = "section.shtml",
6+
---
7+
计算文件的 SHA-256 摘要
8+
9+
标准库中实现了许多加密算法,`sha256` 和 `md5` 都是开箱即用的。
10+
11+
[]($code.siteAsset('src/02-01.zig').language('zig'))

src/zh-CN/02-02-pbkdf2.smd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
.title = "使用 PBKDF2 对密码进行加盐和哈希",
3+
.date = "2024-01-01",
4+
.author = "ZigCC",
5+
.layout = "section.shtml",
6+
---
7+
使用 PBKDF2 对密码进行加盐和哈希
8+
9+
使用 [`std.crypto.pwhash.pbkdf2`] 对加盐后的密码进行哈希处理。盐值是使用 [`std.rand.DefaultPrng`] 生成的,它用生成的随机数填充盐值字节数组。
10+
11+
[]($code.siteAsset('src/02-02.zig').language('zig'))
12+
13+
[`std.crypto.pwhash.pbkdf2`]: https://ziglang.org/documentation/0.11.0/std/#A;std:crypto.pwhash.pbkdf2
14+
[`std.rand.defaultprng`]: https://ziglang.org/documentation/0.11.0/std/#A;std:rand.DefaultPrng

src/zh-CN/02-03-argon2.smd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
.title = "使用 Argon2 对密码进行加盐和哈希",
3+
.date = "2025-08-28",
4+
.author = "ZigCC",
5+
.layout = "section.shtml",
6+
---
7+
使用 Argon2 对密码进行加盐和哈希
8+
9+
此 Zig 程序使用 Argon2id 密码哈希算法从密码和盐值派生加密密钥。它使用 [std.crypto.pwhash.argon2] 对加盐后的密码进行哈希处理,其中盐值是使用 [`std.crypto.random`] 生成的。
10+
11+
[]($code.siteAsset('src/02-03.zig').language('zig'))
12+
13+
[`std.crypto.pwhash.argon2`]: https://ziglang.org/documentation/0.14.0/std/#std.crypto.pwhash.argon2
14+
[`std.crypto.random`]: https://ziglang.org/documentation/0.14.0/std/#std.crypto.tlcsprng.interface

src/zh-CN/03-01-elapsed-time.smd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
.title = "测量两个代码段之间的经过时间",
3+
.date = "2024-01-01",
4+
.author = "ZigCC",
5+
.layout = "section.shtml",
6+
---
7+
8+
[`Instant`] 表示相对于当前执行程序的时间戳,即使程序挂起它也会继续计时,可用于记录经过的时间。
9+
10+
调用 [`std.time.Instant.since`] 返回一个表示经过纳秒数的 u64。
11+
12+
这是一个非常常见的任务,为了方便起见,提供了一个 [`Timer`]。
13+
14+
[]($code.siteAsset('src/03-01.zig').language('zig'))
15+
16+
[`instant`]: https://ziglang.org/documentation/0.11.0/std/#A;std:time.Instant
17+
[`timer`]: https://ziglang.org/documentation/0.11.0/std/#A;std:time.Timer
18+
[`std.time.instant.since`]: https://ziglang.org/documentation/0.11.0/std/#A;std:time.Instant.since

src/zh-CN/04-01-tcp-server.smd

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
.title = "监听未使用的 TCP/IP 端口",
3+
.date = "2024-01-01",
4+
.author = "ZigCC",
5+
.layout = "section.shtml",
6+
---
7+
8+
在此示例中,端口显示在控制台上,程序将监听直到收到请求。将端口设置为 0 时,`Ip4Address` 会分配一个随机端口。
9+
10+
[]($code.siteAsset('src/04-01.zig').language('zig'))
11+
12+
启动后,请尝试像这样进行测试:
13+
14+
```bash
15+
echo "hello zig" | nc localhost <port>
16+
```
17+
18+
默认情况下,程序使用 IPv4 进行监听。如果您想要 IPv6,请使用 `::1` 代替 `127.0.0.1`,将 `net.Ip4Address.parse` 替换为 `net.Ip6Address.parse`,并在创建 `net.Address` 时将字段 `.in` 替换为 `.in6`。
19+
20+
(并连接到像 `ip6-localhost` 之类的地址,具体取决于您的机器设置。)
21+
22+
下一节将演示如何使用 Zig 代码连接到此服务器。

src/zh-CN/04-02-tcp-client.smd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
.title = "TCP 客户端",
3+
.date = "2024-01-01",
4+
.author = "ZigCC",
5+
.layout = "section.shtml",
6+
---
7+
8+
在此示例中,我们要创建一个 TCP 客户端来连接上一节中的服务器。
9+
您可以使用 `zig build run-04-02 -- <port>` 运行它。
10+
11+
[]($code.siteAsset('src/04-02.zig').language('zig'))
12+
13+
默认情况下,程序使用 IPv4 连接。如果您想要 IPv6,请使用 `::1` 代替 `127.0.0.1`,将 `net.Address.parseIp4` 替换为 `net.Address.parseIp6`。

src/zh-CN/04-03-udp-echo.smd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
.title = "UDP 回显",
3+
.date = "2024-01-01",
4+
.author = "ZigCC",
5+
.layout = "section.shtml",
6+
---
7+
8+
与 TCP 服务器示例类似,此程序将侦听指定的 IP 地址和端口,但这次是侦听 UDP 数据报。如果接收到数据,它将被回显到发送者的地址。
9+
10+
尽管 `std.net` 主要侧重于 TCP 的抽象(目前为止),我们仍然可以利用套接字编程通过 UDP 进行通信。
11+
12+
[]($code.siteAsset('src/04-03.zig').language('zig'))
13+
14+
启动程序后,使用 `nc` 进行如下测试,使用 `-u` 标志表示 UDP:
15+
16+
```bash
17+
echo "hello zig" | nc -u localhost <port>
18+
```

src/zh-CN/05-01-http-get.smd

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
.title = "GET",
3+
.date = "2024-01-01",
4+
.author = "ZigCC",
5+
.layout = "section.shtml",
6+
---
7+
8+
解析提供的 URL 并使用 [`request`] 发出同步 HTTP GET 请求。打印获取到的 [`Response`] 状态和标头。
9+
10+
> 注意:由于 HTTP 支持尚处于早期阶段,对于任何复杂的任务,建议使用 [libcurl](https://curl.se/libcurl/c/)。
11+
> 并且如果缓冲区不足,它将返回 `error.HttpHeadersOverSize`
12+
13+
[]($code.siteAsset('src/05-01.zig').language('zig'))
14+
15+
[`request`]: https://ziglang.org/documentation/0.11.0/std/src/std/http/Client.zig.html#L992
16+
[`response`]: https://ziglang.org/documentation/0.11.0/std/src/std/http/Client.zig.html#L322

src/zh-CN/05-02-http-post.smd

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
.title = "POST",
3+
.date = "2024-01-01",
4+
.author = "ZigCC",
5+
.layout = "section.shtml",
6+
---
7+
8+
解析提供的 URL 并使用 [`request`] 发出同步 HTTP POST 请求。打印获取到的 [`Response`] 状态以及从服务器接收的数据。
9+
10+
> 注意:由于 HTTP 支持尚处于早期阶段,对于任何复杂的任务,建议使用 [libcurl](https://curl.se/libcurl/c/)。
11+
12+
[]($code.siteAsset('src/05-02.zig').language('zig'))
13+
14+
[`request`]: https://ziglang.org/documentation/0.11.0/std/src/std/http/Client.zig.html#L992
15+
[`response`]: https://ziglang.org/documentation/0.11.0/std/src/std/http/Client.zig.html#L322
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
.title = "http.Server - std",
3+
.date = "2024-01-01",
4+
.author = "ZigCC",
5+
.layout = "section.shtml",
6+
---
7+
8+
自 Zig 0.12.0 起引入了 `http.Server` 的基本实现。
9+
10+
对于每个连接,我们生成一个新线程来处理它,在 `accept` 中它将:
11+
1. 首先,它使用 `defer` 确保返回时关闭连接。
12+
2. 然后初始化 HTTP 服务器以开始解析请求
13+
3. 对于每个请求,我们首先检查它是否可以升级到 WebSocket。
14+
- 如果成功,则调用 `serveWebSocket`,否则调用 `serverHTTP`
15+
16+
[]($code.siteAsset('src/05-03.zig').language('zig'))
17+
18+
## 测试
19+
20+
对于 HTTP,我们可以使用 `curl`:
21+
```bash
22+
curl -v localhost:8080
23+
```
24+
它将输出:
25+
```bash
26+
< HTTP/1.1 200 OK
27+
< content-length: 32
28+
< custom header: custom value
29+
<
30+
Hello World from Zig HTTP server
31+
```
32+
33+
对于 WebSocket,我们可以使用浏览器 `开发者工具` 中的控制台:
34+
35+
```js
36+
var webSocket = new WebSocket('ws://localhost:8080');
37+
webSocket.onmessage = function(data) { console.log(data); }
38+
```
39+
然后我们可以像这样发送消息:
40+
```js
41+
webSocket.send('abc')
42+
```
43+
44+
[websocket-client]($image.siteAsset('images/websocket-client.webp'))
45+
46+
有关详细信息,请参阅 [Writing WebSocket client applications - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications)。
47+
48+
## 注意
49+
标准库实现的性能极差。如果您计划不仅仅是进行基本的实验,请考虑使用替代库,例如:
50+
- <https://github.com/karlseguin/http.zig>
51+
- <https://github.com/zigzap/zap>
52+
- <https://github.com/mookums/zzz>

0 commit comments

Comments
 (0)