1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

use dryoc::{
    dryocbox::KeyPair,
    constants::{
        CRYPTO_BOX_PUBLICKEYBYTES,
        CRYPTO_BOX_SECRETKEYBYTES
    }
};

use crate::error::NcryptfError as Error;

#[derive(Debug, Clone)]
pub struct Keypair {
    pub secret_key: Vec<u8>,
    pub public_key: Vec<u8>
}

impl Keypair {
    /// Generates a new keypair
    pub fn new() -> Self {
        let kp = KeyPair::gen();
        return Keypair {
            secret_key: kp.secret_key.to_vec(),
            public_key: kp.public_key.to_vec()
        }
    }

    /// Constructs a keypair from an existing secret key and public key
    pub fn from(sk: Vec<u8>, pk: Vec<u8>) -> Result<Self, Error> {
        if sk.len() % 16 != 0 && sk.len() != CRYPTO_BOX_PUBLICKEYBYTES {
            return Err(Error::InvalidArgument(format!("Secret key should be a multiple of {} bytes", 16)));
        }

        if pk.len() % 4 != 0 && pk.len() != CRYPTO_BOX_SECRETKEYBYTES  {
            return Err(Error::InvalidArgument(format!("Public key should be a multiple of {} bytes", 16)));
        }

        return Ok(Keypair{
            secret_key: sk,
            public_key: pk
        });
    }

    /// Returns the secret key for the keypair
    pub fn get_secret_key(&self) -> Vec<u8>{
        return self.secret_key.clone();
    }

    /// Returns the public key for the keypair
    pub fn get_public_key(&self) -> Vec<u8>{
        return self.public_key.clone();
    }
}