rust 写代码真的很复杂 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
RedBeanIce
V2EX    Rust

rust 写代码真的很复杂

  •  
  •   RedBeanIce 2024-07-09 21:08:34 +08:00 3706 次点击
    这是一个建于 525 天前的主题,其中的信息可能已经有所发展或是发生改变。
    ```rust
    use aes_gcm::{aead::{Aead, AeadCore, KeyInit, OsRng}, Aes256Gcm, AesGcm};
    use aes_gcm::aead::consts::U12;
    use aes_gcm::aead::Nonce;
    use aes_gcm::aes::Aes256;

    // aes-gcm = "0.10.3"

    #[test]
    fn test_3_aes256_cbc() {
    // 生成一个随机的 AES256-GCM 加密密钥 key 。
    // The encryption key can be generated randomly:
    let key = Aes256Gcm::generate_key(OsRng);

    // 使用生成的密钥 key 创建 AES256-GCM 加密器 cipher ,然后生成一个 96 位长度的随机 nonce nonce 。
    // 使用 cipher 对 "plaintext message" 进行加密得到 ciphertext ,然后再对 ciphertext 进行解密得到 plaintext 。
    // 最后使用 assert_eq! 宏来验证解密后的 plaintext 是否与原始消息相同。
    let cipher: AesGcm<Aes256, U12> = Aes256Gcm::new(&key);
    // 生成随机 nonce ( 96 bits )
    // let nOnce= GenericArray::from_slice(&Aes256Gcm::generate_nonce(&mut OsRng));
    // let nonce: &GenericArray<u8, <Aes256Gcm as Aead>::NonceSize> = GenericArray::from_slice(&Aes256Gcm::generate_nonce(&mut OsRng));
    // let nonce: GenericArray<u8, <Aes256Gcm as Aead>::NonceSize> = ...;
    let nonce: Nonce<Aes256Gcm> = Aes256Gcm::generate_nonce(&mut OsRng); // 96-bits; unique per message

    // 将明文消息作为字节数组传递给 encrypt 函数。
    let plaintext: &[u8] = b"plaintext message";
    let ciphertext = cipher.encrypt(&nonce, plaintext).unwrap();
    let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref()).unwrap();

    assert_eq!(&plaintext, b"plaintext message");
    }


    ```

    let plaintext: &[u8] = b"plaintext message";

    这一行代码,要自己将手动推导的 &[u8;17] 固定长度数组,,修改为可变数组。。

    以及一些其他的,,整个代码才可以运行
    5 条回复    2024-07-30 11:43:45 +08:00
    chainal7777
        1
    chainal7777  
       2024-07-09 21:24:03 +08:00
    1 ,用点 use as
    2 ,用点 type A=a
    3 ,只写关键注释
    RedBeanIce
        2
    RedBeanIce  
    OP
       2024-07-09 21:46:25 +08:00
    @chainal7777 请问第一点是什么意思呀,,,,‘’第二点我大概理解,就是将泛型定义变成单独的一行代码。
    cppc
        3
    cppc  
       2024-07-09 23:56:28 +08:00 via iPhone
    为了安全是这样啦,我还见过编译时 SQL 检查呢,SQL 写错代码都不能编译
    PTLin
        4
    PTLin  
       2024-07-10 13:41:06 +08:00
    不是 rust 复杂,这个是库复杂,用了太多泛型了
    whoami9894
        5
    whoami9894  
       2024-07-30 11:43:45 +08:00
    有没有可能,不用写那么多类型标注

    另外,函数当然不可能接收 [u8; const N] 参数,难道你能在编译期知道数据长度?

    ```
    let key = Aes256Gcm::generate_key(OsRng);
    let cipher = Aes256Gcm::new(&key);
    let nOnce= Aes256Gcm::generate_nonce(OsRng);

    let plaintext = b"plaintext message";
    let ciphertext = cipher.encrypt(&nonce, plaintext.as_slice()).unwrap();
    let plaintext = cipher.decrypt(&nonce, ciphertext.as_slice()).unwrap();

    assert_eq!(&plaintext, b"plaintext message");
    ```
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3013 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 20ms UTC 12:35 PVG 20:35 LAX 04:35 JFK 07:35
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86