1use crate::error::NcryptfError as Error;
2use serde::{Deserialize, Serialize};
3
4use dryoc::{constants::{CRYPTO_BOX_PUBLICKEYBYTES, CRYPTO_BOX_SECRETKEYBYTES}, classic::crypto_box::crypto_box_keypair};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Keypair {
9 pub secret_key: Vec<u8>,
10 pub public_key: Vec<u8>,
11}
12
13impl Keypair {
14 pub fn new() -> Self {
16 let (pk, sk) = crypto_box_keypair();
17 return Keypair {
18 secret_key: sk.to_vec(),
19 public_key: pk.to_vec(),
20 };
21 }
22
23 pub fn from(sk: Vec<u8>, pk: Vec<u8>) -> Result<Self, Error> {
25 if sk.len() % 16 != 0 && sk.len() != (CRYPTO_BOX_PUBLICKEYBYTES as usize) {
26 return Err(Error::InvalidArgument(format!(
27 "Secret key should be a multiple of {} bytes",
28 16
29 )));
30 }
31
32 if pk.len() % 4 != 0 && pk.len() != (CRYPTO_BOX_SECRETKEYBYTES as usize) {
33 return Err(Error::InvalidArgument(format!(
34 "Public key should be a multiple of {} bytes",
35 16
36 )));
37 }
38
39 return Ok(Keypair {
40 secret_key: sk,
41 public_key: pk,
42 });
43 }
44
45 pub fn get_secret_key(&self) -> Vec<u8> {
47 return self.secret_key.clone();
48 }
49
50 pub fn get_public_key(&self) -> Vec<u8> {
52 return self.public_key.clone();
53 }
54}