mirror of
https://github.com/0PandaDEV/Qopy.git
synced 2025-04-21 13:14:04 +02:00
refactor(sync): convert pairing methods to Tauri commands
This commit is contained in:
parent
7c0e83f86a
commit
2ec273173c
1 changed files with 64 additions and 42 deletions
|
@ -1,10 +1,10 @@
|
||||||
use aes_gcm::{ Aes256Gcm, KeyInit };
|
|
||||||
use aes_gcm::aead::Aead;
|
use aes_gcm::aead::Aead;
|
||||||
use base64::{ engine::general_purpose::STANDARD, Engine };
|
use aes_gcm::{Aes256Gcm, KeyInit};
|
||||||
use rand::thread_rng;
|
use base64::{engine::general_purpose::STANDARD, Engine};
|
||||||
use rand::Rng;
|
use rand::rng;
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use serde::{ Deserialize, Serialize };
|
use rand::Rng;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use tauri::State;
|
use tauri::State;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
@ -25,10 +25,10 @@ pub struct PairingManager {
|
||||||
|
|
||||||
impl PairingManager {
|
impl PairingManager {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut rng = thread_rng();
|
let mut rng = rng();
|
||||||
let pairing_key = Self::generate_emoji_sequence(&mut rng);
|
let pairing_key = Self::generate_emoji_sequence(&mut rng);
|
||||||
let encryption_key = rng.gen::<[u8; 32]>();
|
let encryption_key = rng.random::<[u8; 32]>();
|
||||||
let nonce = rng.gen::<[u8; 12]>();
|
let nonce = rng.random::<[u8; 12]>();
|
||||||
PairingManager {
|
PairingManager {
|
||||||
pairing_key,
|
pairing_key,
|
||||||
encryption_key,
|
encryption_key,
|
||||||
|
@ -39,7 +39,12 @@ impl PairingManager {
|
||||||
pub fn generate_emoji_sequence<R: Rng>(rng: &mut R) -> String {
|
pub fn generate_emoji_sequence<R: Rng>(rng: &mut R) -> String {
|
||||||
let mut emojis = EMOJI_POOL.to_vec();
|
let mut emojis = EMOJI_POOL.to_vec();
|
||||||
emojis.shuffle(rng);
|
emojis.shuffle(rng);
|
||||||
emojis.iter().take(PAIRING_KEY_LENGTH).cloned().collect::<Vec<&str>>().join(" ")
|
emojis
|
||||||
|
.iter()
|
||||||
|
.take(PAIRING_KEY_LENGTH)
|
||||||
|
.cloned()
|
||||||
|
.collect::<Vec<&str>>()
|
||||||
|
.join(" ")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_pairing(&self, input_key: &str) -> bool {
|
pub fn validate_pairing(&self, input_key: &str) -> bool {
|
||||||
|
@ -53,52 +58,70 @@ impl PairingManager {
|
||||||
pub fn get_nonce(&self) -> &[u8; 12] {
|
pub fn get_nonce(&self) -> &[u8; 12] {
|
||||||
&self.nonce
|
&self.nonce
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn generate_invitation_code(&self) -> String {
|
#[tauri::command]
|
||||||
Uuid::new_v4().to_string()
|
pub fn generate_invitation_code(_pairing_manager: State<'_, PairingManager>) -> String {
|
||||||
}
|
Uuid::new_v4().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn encrypt_key(&self, key: &[u8; 32]) -> Result<String, String> {
|
#[tauri::command]
|
||||||
let cipher = Aes256Gcm::new(&self.encryption_key.into());
|
pub fn encrypt_key(
|
||||||
let ciphertext = cipher
|
pairing_manager: State<'_, PairingManager>,
|
||||||
.encrypt(&self.nonce.into(), key.as_ref())
|
key: Vec<u8>,
|
||||||
.map_err(|e| e.to_string())?;
|
) -> Result<String, String> {
|
||||||
Ok(STANDARD.encode(ciphertext))
|
let key_array: [u8; 32] = key
|
||||||
}
|
.try_into()
|
||||||
|
.map_err(|_| "Invalid key length".to_string())?;
|
||||||
|
let cipher = Aes256Gcm::new(&pairing_manager.encryption_key.into());
|
||||||
|
let ciphertext = cipher
|
||||||
|
.encrypt(&pairing_manager.nonce.into(), key_array.as_ref())
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
Ok(STANDARD.encode(ciphertext))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn decrypt_key(&self, encrypted_key: &str) -> Result<[u8; 32], String> {
|
#[tauri::command]
|
||||||
let ciphertext = STANDARD.decode(encrypted_key).map_err(|e| e.to_string())?;
|
pub fn decrypt_key(
|
||||||
let cipher = Aes256Gcm::new(&self.encryption_key.into());
|
pairing_manager: State<'_, PairingManager>,
|
||||||
let plaintext = cipher
|
encrypted_key: String,
|
||||||
.decrypt(&self.nonce.into(), ciphertext.as_ref())
|
) -> Result<Vec<u8>, String> {
|
||||||
.map_err(|e| e.to_string())?;
|
let ciphertext = STANDARD.decode(&encrypted_key).map_err(|e| e.to_string())?;
|
||||||
let mut key = [0u8; 32];
|
let cipher = Aes256Gcm::new(&pairing_manager.encryption_key.into());
|
||||||
key.copy_from_slice(&plaintext);
|
let plaintext = cipher
|
||||||
Ok(key)
|
.decrypt(&pairing_manager.nonce.into(), ciphertext.as_ref())
|
||||||
}
|
.map_err(|e| e.to_string())?;
|
||||||
|
Ok(plaintext)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create_pairing_request(&self, inviter_id: String) -> PairingRequest {
|
#[tauri::command]
|
||||||
PairingRequest {
|
pub fn create_pairing_request(
|
||||||
inviter_id,
|
pairing_manager: State<'_, PairingManager>,
|
||||||
invitation_code: self.generate_invitation_code(),
|
inviter_id: String,
|
||||||
}
|
) -> PairingRequest {
|
||||||
}
|
PairingRequest {
|
||||||
|
inviter_id,
|
||||||
pub fn handle_pairing_response(&self, response: PairingRequest) -> bool {
|
invitation_code: generate_invitation_code(pairing_manager),
|
||||||
self.validate_pairing(&response.invitation_code)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn handle_pairing_response(
|
||||||
|
pairing_manager: State<'_, PairingManager>,
|
||||||
|
response: PairingRequest,
|
||||||
|
) -> bool {
|
||||||
|
pairing_manager.validate_pairing(&response.invitation_code)
|
||||||
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn initiate_pairing(_pairing_manager: State<'_, PairingManager>) -> String {
|
pub fn initiate_pairing(_pairing_manager: State<'_, PairingManager>) -> String {
|
||||||
let mut rng = thread_rng();
|
let mut rng = rng();
|
||||||
PairingManager::generate_emoji_sequence(&mut rng)
|
PairingManager::generate_emoji_sequence(&mut rng)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn complete_pairing(
|
pub fn complete_pairing(
|
||||||
input_key: String,
|
input_key: String,
|
||||||
pairing_manager: State<'_, PairingManager>
|
pairing_manager: State<'_, PairingManager>,
|
||||||
) -> Result<String, String> {
|
) -> Result<String, String> {
|
||||||
if pairing_manager.validate_pairing(&input_key) {
|
if pairing_manager.validate_pairing(&input_key) {
|
||||||
let _shared_key = pairing_manager.encryption_key.to_vec();
|
let _shared_key = pairing_manager.encryption_key.to_vec();
|
||||||
|
@ -110,6 +133,5 @@ pub fn complete_pairing(
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn generate_invitation(pairing_manager: State<'_, PairingManager>) -> String {
|
pub fn generate_invitation(pairing_manager: State<'_, PairingManager>) -> String {
|
||||||
pairing_manager.generate_invitation_code()
|
generate_invitation_code(pairing_manager)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue