在使用 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 算法库推荐?
1 dvorakchen1 OP 已知道原因,这个叼玩意儿依赖的 `rand` 版本为 0.7 ,高于它的都不能用 |