V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
dvorakchen1
V2EX  ›  Rust

Rust x25519-dalek 使用问题

  •  
  •   dvorakchen1 · 274 天前 · 644 次点击
    这是一个创建于 274 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在使用 DH 算法库:x25519-dalek 的时候,遇到的问题,按照官方文档写下如下代码:

    use rand_core::OsRng;
    use x25519_dalek::{EphemeralSecret, PublicKey};
    
    fn main() {
        let alice_secret = EphemeralSecret::new(OsRng);
        let alice_public = PublicKey::from(&alice_secret);
    
        let bob_secret = EphemeralSecret::new(OsRng);
        let bob_public = PublicKey::from(&bob_secret);
    }
    

    依赖如下:

    rand_core = { version = "0.6.4", features = ["getrandom"] }
    x25519-dalek = "1.2.0"
    

    Rust 版本为:rustc 1.71.0 (8ede3aae2 2023-07-12)

    报错信息如下:

    error[E0277]: the trait bound `OsRng: rand_core::RngCore` is not satisfied
      --> src\main.rs:14:43
       |
    14 |     let bob_secret = EphemeralSecret::new(OsRng);
       |                      -------------------- ^^^^^ the trait `rand_core::RngCore` is not implemented for `OsRng`
       |                      |
       |                      required by a bound introduced by this call
       |
       = help: the following other types implement trait `rand_core::RngCore`:
                 &'a mut R
                 Box<R>
                 rand_core::block::BlockRng64<R>
                 rand_core::block::BlockRng<R>
                 rand_core::os::OsRng
    note: required by a bound in `EphemeralSecret::new`
    

    按照错误信息,是 OsRng 没有实现 trait CryptoRng, RngCore。但我看了 rand_core 的源码,是有实现的。

    查看了 Git 上的 Issue ,似乎是因为对 rand_core 的版本支持问题,说将在 2.0 版本修复。但我不是很确定。

    请问各位如何解决这个错误?如果这个库是用不了,有没有其他的 DH 算法库推荐?

    dvorakchen1
        1
    dvorakchen1  
    OP
       273 天前
    已知道原因,这个叼玩意儿依赖的 `rand` 版本为 0.7 ,高于它的都不能用
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5752 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 02:49 · PVG 10:49 · LAX 19:49 · JFK 22:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.