diff --git a/src/cookbook/cryptography/encryption.md b/src/cookbook/cryptography/encryption.md index a728da46..becab266 100644 --- a/src/cookbook/cryptography/encryption.md +++ b/src/cookbook/cryptography/encryption.md @@ -1,7 +1,7 @@ # 加密 ### 使用 PBKDF2 对密码进行哈希和加盐( salt ) -[ring::pbkdf2]() 可以对一个加盐密码进行哈希。 +[ring::pbkdf2](https://briansmith.org/rustdoc/ring/pbkdf2/index.html) 可以对一个加盐密码进行哈希。 ```rust,editable diff --git a/src/cookbook/datastructures/bitfield.md b/src/cookbook/datastructures/bitfield.md index 7f16f82f..4c9c29e0 100644 --- a/src/cookbook/datastructures/bitfield.md +++ b/src/cookbook/datastructures/bitfield.md @@ -1 +1,47 @@ # 位字段 + +### 定义和操作位字段 +使用 [`bitflags!`](https://docs.rs/bitflags/1.3.2/bitflags/macro.bitflags.html) 宏可以帮助我们创建安全的位字段类型 `MyFlags`,然后为其实现基本的 `clear` 操作。以下代码展示了基本的位操作和格式化: +```rust,editable +use bitflags::bitflags; +use std::fmt; + +bitflags! { + struct MyFlags: u32 { + const FLAG_A = 0b00000001; + const FLAG_B = 0b00000010; + const FLAG_C = 0b00000100; + const FLAG_ABC = Self::FLAG_A.bits + | Self::FLAG_B.bits + | Self::FLAG_C.bits; + } +} + +impl MyFlags { + pub fn clear(&mut self) -> &mut MyFlags { + self.bits = 0; + self + } +} + +impl fmt::Display for MyFlags { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:032b}", self.bits) + } +} + +fn main() { + let e1 = MyFlags::FLAG_A | MyFlags::FLAG_C; + let e2 = MyFlags::FLAG_B | MyFlags::FLAG_C; + assert_eq!((e1 | e2), MyFlags::FLAG_ABC); + assert_eq!((e1 & e2), MyFlags::FLAG_C); + assert_eq!((e1 - e2), MyFlags::FLAG_A); + assert_eq!(!e2, MyFlags::FLAG_A); + + let mut flags = MyFlags::FLAG_ABC; + assert_eq!(format!("{}", flags), "00000000000000000000000000000111"); + assert_eq!(format!("{}", flags.clear()), "00000000000000000000000000000000"); + assert_eq!(format!("{:?}", MyFlags::FLAG_B), "FLAG_B"); + assert_eq!(format!("{:?}", MyFlags::FLAG_A | MyFlags::FLAG_B), "FLAG_A | FLAG_B"); +} +``` \ No newline at end of file diff --git a/内容变更记录.md b/内容变更记录.md index 4b301d2c..af513259 100644 --- a/内容变更记录.md +++ b/内容变更记录.md @@ -4,6 +4,8 @@ ## 2022-04-04 - 新增章节: [Cookbook - 使用 rayon 并行处理数据](https://course.rs/cookbook/cocurrency/parallel.html) +- 新增章节: [Cookbook - 加密](https://course.rs/cookbook/cryptography/encryption.html) +- 新增章节: [Cookbook - 哈希](https://course.rs/cookbook/cryptography/hashing.html) ## 2022-04-03