shuttle_core/crypto/
mod.rs

1//! Libsodium wrappers.
2use sodiumoxide;
3use sodiumoxide::randombytes;
4use sodiumoxide::crypto::hash::sha256;
5
6mod keypair;
7mod strkey;
8mod ecdh;
9mod sha;
10
11pub use self::keypair::{KeyPair, PublicKey, SecretKey};
12pub use self::ecdh::{Curve25519Public, Curve25519Secret};
13pub use self::sha::{HmacSha256Key, HmacSha256Mac};
14
15/// Compute sha256 hash of `m`.
16pub fn hash(m: &[u8]) -> Vec<u8> {
17    let digest = sha256::hash(&m);
18    digest.0.to_vec()
19}
20
21/// Generate `size` random bytes.
22pub fn random_bytes(size: usize) -> Vec<u8> {
23    randombytes::randombytes(size)
24}
25
26/// Initialize the sodium library and chooses faster version of the primitives
27/// if possible.
28///
29/// `init` also makes `KeyPair::random()` thread-safe.
30pub fn init() -> bool {
31    sodiumoxide::init()
32}