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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use super::bytes_key_from_str;
use crate::error::CryptoError;
use crypto_box::{PublicKey, SecretKey};
use hex::ToHex;
#[derive(Debug)]
pub struct EncryptingKeypair {
pub secret: SecretKey,
pub public: PublicKey,
}
impl EncryptingKeypair {
#[tracing::instrument(level = "debug", ret)]
pub fn from_bytes_slices(pubkey: [u8; 32], seckey: [u8; 32]) -> Self {
Self {
secret: SecretKey::from(seckey),
public: PublicKey::from(pubkey),
}
}
#[tracing::instrument(level = "debug", ret)]
pub fn from_str(public_key: &str, secret_key: &str) -> Result<Self, CryptoError> {
let pubkey = bytes_key_from_str(public_key)?;
let seckey = bytes_key_from_str(secret_key)?;
Ok(Self::from_bytes_slices(pubkey, seckey))
}
#[tracing::instrument(level = "debug", ret)]
pub fn new() -> Self {
let mut rng = rand_core::OsRng;
let secret = SecretKey::generate(&mut rng);
let public = secret.public_key();
Self { secret, public }
}
#[tracing::instrument(level = "debug", ret, skip(self))]
pub fn encode_pub(&self) -> String {
self.public.as_bytes().encode_hex::<String>()
}
#[tracing::instrument(level = "debug", ret, skip(self))]
pub fn decrypt(&self, cipher_text: Vec<u8>) -> Result<Vec<u8>, CryptoError> {
Ok(hex::encode(
r#"{
"id": "21f527a0-5909-4b00-9494-2de8cfb6ace1",
"credentialID": "7b20c5c2fa565ee9797d58f788169630d57c36ec8d618456728be7353c943ee8",
"credentialSecret": "ff5ea13d0e881aa1a1e909a37bf02073934eacbda663508613910e1d86ecd406"
}"#,
)
.as_bytes()
.to_vec())
}
}
impl Default for EncryptingKeypair {
#[tracing::instrument(level = "debug", ret)]
fn default() -> Self {
Self::new()
}
}