From cad885a4d9e2dc637dc5c9218c418ea374ca82db Mon Sep 17 00:00:00 2001 From: isaac-asdf <97187398+isaac-asdf@users.noreply.github.com> Date: Sat, 5 Aug 2023 22:52:27 -0500 Subject: [PATCH] encrypt might be working? --- src/lib.rs | 2 +- src/nip04.rs | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ea3afea..1420aa9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -// #![no_std] +#![no_std] //! Implementation of [Nostr](https://nostr.com/) note creation for a #![no_std] environment. //! An example project using an esp32 can be seen [here](https://github.com/isaac-asdf/esp32-nostr-client). //! diff --git a/src/nip04.rs b/src/nip04.rs index 905ac83..50cc718 100644 --- a/src/nip04.rs +++ b/src/nip04.rs @@ -1,6 +1,7 @@ use core::str::FromStr; use aes::cipher::generic_array::{typenum, GenericArray}; +use base64ct::{Base64, Encoding}; use heapless::{String, Vec}; use secp256k1::{ecdh, PublicKey, SecretKey, XOnlyPublicKey}; @@ -35,7 +36,7 @@ pub fn encrypt( let iv: [u8; 16] = [0; 16]; let mut cipher = Aes256CbcEnc::new(&key.into(), &iv.into()); - let mut ciphertext: String = String::new(); + let mut ciphertext = [16_u8; MAX_DM_SIZE]; // fill cipher text from slices of input let total_blocks = text.len() / 16 + 1; @@ -49,19 +50,22 @@ pub fn encrypt( }; let mut block = pad_block(&text[i * 16..end_slice], 16); cipher.encrypt_block_mut(&mut block); - block.iter().for_each(|b| { - ciphertext.push(*b as char).unwrap(); + block.iter().enumerate().for_each(|(j, b)| { + ciphertext[i * 16 + j] = *b; }); } - // Ok(format!( - // "{}?iv={}", - // general_purpose::STANDARD.encode(result), - // general_purpose::STANDARD.encode(iv) - // )) - println!("cipher"); - println!("{ciphertext}"); - Ok(ciphertext) + let encode_this = &ciphertext[0..total_blocks * 16]; + let mut enc_buf = [0u8; MAX_DM_SIZE]; + let encoded = Base64::encode(encode_this, &mut enc_buf).unwrap(); + + let mut enc_buf = [0u8; 32]; + let iv_str = Base64::encode(&iv, &mut enc_buf).unwrap(); + + let mut output = String::from_str(&encoded).unwrap(); + output.push_str("?iv=").unwrap(); + output.push_str(&iv_str).unwrap(); + Ok(output) } // fn pad_blocks(text: T) -> GenericArray