# 01-rcli **Repository Path**: czlan91/01-rcli ## Basic Information - **Project Name**: 01-rcli - **Description**: rust 第一周 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-04-21 - **Last Updated**: 2024-05-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # RCLI rcli is a rust tool. # 作业一 阅读 chacha20poly1305 文档,了解其使用方法并构建 CLI 对输入文本进行加密 / 解密 要求: ```shell # 加密并输出 base64 rcli text encrypt --input "-" --key "xxx" --nonce "xxx" --format "chaCha20Poly1305" # 解密文本 >base64 > binary> rcli text decrypt --input "-" --key "xxx" --nonce "xxx" --format "chaCha20Poly1305" ``` ```shell cargo add chacha20poly1305 ``` 运行测试 ```shell cargo run --bin rcli text encrypt --key "fixtures/ChaChaPoly1305.txt" --nonce "fixtures/nonce.txt" > fixtures/plaintext.txt cargo run --bin rcli text decrypt --key "fixtures/ChaChaPoly1305.txt" --nonce "fixtures/nonce.txt" --input "fixtures/plaintext.txt" cargo run --bin rcli text encrypt --key "fixtures/ChaChaPoly1305.txt" --nonce "fixtures/xnonce.txt" > fixtures/plaintext.txt --format "xChaCha20Pol y1305" cargo run --bin rcli text decrypt --key "fixtures/ChaChaPoly1305.txt" --nonce "fixtures/xnonce.txt" --input "fixtures/plaintext.txt" --format "xChaCha20Poly1305" cargo nextest run --bin rcli test_chacha20poly1305 cargo nextest run --bin rcli test_xchacha20poly1305 ``` # 作业二 json web token(jwt) 在用户验证领域经常被用到。请构建一个 CLI 来为给定 sub/aud/exp/… 生成一个 jwt。要求生成的 jwt 可以通过 http://jwt.io 的验证。 CLI: rcli jwt sign --sub acme --aud device1 --exp 14d rcli jwt verify -t sub:acme - 公司名 aud:device1 - ["http://com.newtouch","http://com.czlan"]; exp:14d - 14 天 --- jwt的组成: header, payload, signature header: 头部通常由两部分组成,令牌的类型(即 JWT)和所使用的签名算法,例如 HMAC SHA256 或 RSA。头部会经过 Base64 编码组成 JWT 的第一部分。 payload: 负载通常由三部分组成, iss(发行人)、 aud(接收方)和 exp(过期时间)。 signature: 签名是 JWT 的最后一部分,用于验证 JWT 的完整性。 sub:存储用户的唯一标识符,比如用户 ID。 aud(Audience)指定了 JWT 的接收者,可以是单个系统或者多个系统。 exp(Expiration Time)是一个时间戳,用于指定 JWT 的过期时间。1970年1月1日 00:00:00 UTC 以来的秒数来表示 所以 exp 需要一个解析函数,用于解析秒数,jwt 如果是 14d,14h,14w, 都要是当前时间的秒值 + exp的秒值 crate: jsonwebtoken ```shell cargo add jsonwebtoken --features use_pem cargo add ring ``` 1. sign的时候,是需要指定算法的。 2. 但是 verify 的时候,一般都是不知道算法的,所以需要先 解密header。通过header 拿到具体的算法。如果没有算法实现,则报错。 iat(Issued At)记录JWT的创建时间,用于追踪令牌的历史信息,不影响其有效性。 nbf(Not Before)定义JWT生效的最早时间,确保在某个时间点之前JWT不能被使用。 exp(Expiration Time)规定JWT的最晚有效时间,超过这个时间后JWT不再有效。 运行测试 ```shell cargo run --bin rcli jwt sign --key "fixtures/blake3.txt" --sub "acme" --aud "com.czlan" --exp "14d" > fixtures/token.txt cargo run --bin rcli jwt verify --key "fixtures/blake3.txt" -t "fixtures/token.txt" --aud "com.czlan" cargo run --bin rcli jwt sign --key "fixtures/base64.txt" --sub "acme" --aud "com.czlan" --exp "14d" --format "HS512Base64" > fixtures/token.txt cargo run --bin rcli jwt verify --key "fixtures/base64.txt" -t "fixtures/token.txt" --aud "com.czlan" --format "HS512Base64" cargo run --bin rcli jwt generator --output "fixtures" ``` jsonwebtoken 的时间处理 e.g. https://github.com/Keats/jsonwebtoken/blob/master/examples/custom_time.rs ## 对于 EdDSA 算法的测试 ```shell cargo run --bin rcli jwt generator --format "EdDSA" --output "fixtures" cargo run --bin rcli jwt sign --key "fixtures/EdDSA.ek" --sub "acme" --aud "com.czlan" --exp "14d" --format "EdDSA" > fixtures/token.txt cargo run --bin rcli jwt verify --key "fixtures/EdDSA.dk" -t "fixtures/token.txt" --aud "com.czlan" --format "EdDSA" ``` ## 对于RSA算法的测试 ```shell cargo add openssl cargo run --bin rcli jwt generator --format "RS256" --output "fixtures" cargo run --bin rcli jwt sign --key "fixtures/private_key.pem" --sub "acme" --aud "com.czlan" --exp "14d" --format "RS256" > fixtures/token.txt cargo run --bin rcli jwt verify --key "fixtures/public_key.pem" -t "fixtures/token.txt" --aud "com.czlan" --format "RS256" ``` ## 多bin 用法 1. 在src下创建目录 examples 2. 按照 https://rustwiki.org/zh-CN/edition-guide/rust-2018/cargo-and-crates-io/multi-file-examples.html 编写代码 3. 配置 Cargo.toml ```toml [[bin]] name = "validation" path = "src/examples/validation.rs" ``` 4. 运行 ```shell cargo run --bin validation cargo run --bin rcli text decrypt --key "fixtures/ChaChaPoly1305.txt" --nonce "fixtures/xnonce.txt" --input "fixtures/plaintext.txt" --format "xChaCha20Poly1305" ``` ## examples 用法 examples 目录 与 src 目录平级 ```shell cargo run --example validation ``` # 作业三 给课程里的 HTTP 文件服务器添加对 directory index 的支持。 作业说明 以上 3 个作业是选做作业,可根据自身情况提交。 作业在结课之前都可以提交。 ```shell RUST_LOG=info cargo run --bin rcli http serve ``` 测试请使用 test.rest 文件。 ## 使用模版库 ```shell cargo add tera cargo add lazy_static ``` String::with_capacity(1024); 模版使用方式 类似 jinjia2. 采用 懒加载的方式,加载 templates 保留文本元格式 展示在html中,使用 `
{{ content }}
` # 使用 once_cell 代替 lazy_static ```shell cargo add once_cell ``` 可以消除宏