port crypto.go
This commit is contained in:
parent
201374fba4
commit
eaf4cee4ee
|
@ -626,6 +626,7 @@ dependencies = [
|
||||||
"base64 0.21.0",
|
"base64 0.21.0",
|
||||||
"base64-serde",
|
"base64-serde",
|
||||||
"log",
|
"log",
|
||||||
|
"rand",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|
|
@ -19,4 +19,5 @@ reqwest = { version = "0.11.16", features = ["blocking", "json"] }
|
||||||
url = "2.3.1"
|
url = "2.3.1"
|
||||||
base64 = "0.21.0"
|
base64 = "0.21.0"
|
||||||
serde_json = "1.0.95"
|
serde_json = "1.0.95"
|
||||||
trifid-pki = { version = "0.1.6", path = "../trifid-pki" }
|
trifid-pki = { version = "0.1.6", path = "../trifid-pki" }
|
||||||
|
rand = "0.8.5"
|
|
@ -0,0 +1,41 @@
|
||||||
|
//! Functions for generating keys and nonces for use in API calls
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
use rand::rngs::OsRng;
|
||||||
|
use trifid_pki::cert::{serialize_x25519_private, serialize_x25519_public};
|
||||||
|
use trifid_pki::ed25519_dalek::{SigningKey, VerifyingKey};
|
||||||
|
use trifid_pki::x25519_dalek::{PublicKey, StaticSecret};
|
||||||
|
|
||||||
|
/// Generate a new random set of Nebula (Diffie-Hellman) and Ed25519 (API calls) keys for use in your client
|
||||||
|
pub fn new_keys() -> (Vec<u8>, Vec<u8>, VerifyingKey, SigningKey) {
|
||||||
|
let (dh_pub, dh_priv) = new_nebula_keypair();
|
||||||
|
let (ed_pub, ed_priv) = new_ed25519_keypair();
|
||||||
|
(dh_pub, dh_priv, ed_pub, ed_priv)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generate a new PEM-encoded Nebula keypair
|
||||||
|
pub fn new_nebula_keypair() -> (Vec<u8>, Vec<u8>) {
|
||||||
|
let (pub_key, priv_key) = new_x25519_keypair();
|
||||||
|
let pub_key_encoded = serialize_x25519_public(&pub_key);
|
||||||
|
let priv_key_encoded = serialize_x25519_private(&priv_key);
|
||||||
|
(pub_key_encoded, priv_key_encoded)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generate a new 32-byte X25519 keypair
|
||||||
|
pub fn new_x25519_keypair() -> ([u8; 32], [u8; 32]) {
|
||||||
|
let priv_key = StaticSecret::new(&mut OsRng);
|
||||||
|
let pub_key = PublicKey::from(&priv_key);
|
||||||
|
(pub_key.to_bytes(), priv_key.to_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generate a new random Ed25519 signing keypair for signing API calls
|
||||||
|
pub fn new_ed25519_keypair() -> (VerifyingKey, SigningKey) {
|
||||||
|
let secret = SigningKey::generate(&mut OsRng);
|
||||||
|
let public = secret.verifying_key();
|
||||||
|
(public, secret)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates a 16-byte random nonce for use in API calls
|
||||||
|
pub fn nonce() -> [u8; 16] {
|
||||||
|
rand::thread_rng().gen()
|
||||||
|
}
|
|
@ -17,4 +17,5 @@
|
||||||
|
|
||||||
pub mod message;
|
pub mod message;
|
||||||
pub mod client;
|
pub mod client;
|
||||||
pub mod credentials;
|
pub mod credentials;
|
||||||
|
pub mod crypto;
|
Loading…
Reference in New Issue