scsys_crypto/keys/
keypair.rs

1/*
2    Appellation: keypair <module>
3    Contributors: FL03 <jo3mccain@icloud.com>
4    Description:
5        ... Summary ...
6*/
7use crate::keys::generate_random_pkcs8;
8use ring::{pkcs8, rand::SystemRandom, signature::Ed25519KeyPair};
9
10#[derive(Debug)]
11pub struct Keypair(Ed25519KeyPair);
12
13impl Keypair {
14    pub fn new(data: Ed25519KeyPair) -> Self {
15        Self(data)
16    }
17    pub fn generate() -> Self {
18        Self::from(generate_random_pkcs8())
19    }
20    pub fn keypair(&self) -> &Ed25519KeyPair {
21        &self.0
22    }
23}
24
25impl std::convert::From<&[u8]> for Keypair {
26    fn from(data: &[u8]) -> Self {
27        Self::new(Ed25519KeyPair::from_pkcs8(data).expect(""))
28    }
29}
30
31impl std::convert::From<pkcs8::Document> for Keypair {
32    fn from(data: pkcs8::Document) -> Self {
33        Self::from(data.as_ref())
34    }
35}
36
37#[derive(Debug)]
38pub enum Keys {
39    ED25519(Ed25519KeyPair),
40}
41
42impl Keys {
43    pub fn new() -> Self {
44        let rng = SystemRandom::new();
45        let pkcs8_bytes = Ed25519KeyPair::generate_pkcs8(&rng).ok().unwrap();
46        let keypair = Ed25519KeyPair::from_pkcs8(pkcs8_bytes.as_ref())
47            .ok()
48            .unwrap();
49        Self::ED25519(keypair)
50    }
51}
52
53impl Default for Keys {
54    fn default() -> Self {
55        Self::new()
56    }
57}