How can I use the new_type! macro to set a more generic type for a sodiumoxide PublicKey and SecretKey?

55 Views Asked by At

I am trying to set a more generic type for a sodiumoxide PublicKey and SecretKey.

This changes depending if we use box_ or sign:

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

I would like to set a PublicKey type or struct which is compatible with sodiumoxide key's type, something like struct PubKey(pub [u8; ?]) .

The source of SecretKey (or any key) is:

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

where SECRETKEYBYTES is of type usize

I can not find any information about this new_type! macro. What is going on here and how can I go further?

1

There are 1 best solutions below

0
On

This is how the macro defines the structure, but I don't see any good way to easily define generic types for both of them.

A possible solution is to define a new trait in your crate and then implement it for both PublicKey and SecretKey. The trait should provide you enough functionality to interact with each structure.