【问题标题】:How can I use the new_type! macro to set a more generic type for a sodiumoxide PublicKey and SecretKey?如何使用 new_type!宏为氧化钠 PublicKey 和 SecretKey 设置更通用的类型?
【发布时间】:2020-09-01 20:01:37
【问题描述】:

我正在尝试为氧化钠 PublicKeySecretKey 设置更通用的类型。

这取决于我们使用box_ 还是sign

type PubCryptoSecretKey = sodiumoxide::crypto::box_::curve25519xsalsa20poly1305::SecretKey;

我想设置一个与氧化钠键类型兼容的PublicKey 类型或结构,例如struct PubKey(pub [u8; ?])

SecretKey(或任意键)的来源是:

new_type! {
    /// `SecretKey` for asymmetric authenticated encryption
    ///
    /// When a `SecretKey` goes out of scope its contents
    /// will be zeroed out
    secret SecretKey(SECRETKEYBYTES);
}

其中SECRETKEYBYTES 的类型为usize

我找不到有关此new_type! 宏的任何信息。这里发生了什么,我该如何走得更远?

【问题讨论】:

    标签: rust libsodium


    【解决方案1】:

    这是how the macro defines the structure,但我看不出有什么好方法可以轻松地为它们定义泛型类型。

    一种可能的解决方案是在您的 crate 中定义一个新特征,然后为 PublicKeySecretKey 实现它。该特征应该为您提供足够的功能来与每个结构进行交互。

    【讨论】:

      猜你喜欢
      • 2016-11-10
      • 2017-10-13
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多