Rename content_enc to content

This commit is contained in:
oupson 2022-10-07 20:55:56 +02:00
parent 2dabc7c0c0
commit 965c07df0a
Signed by: oupson
GPG Key ID: 3BD88615552EFCB7
4 changed files with 52 additions and 3 deletions

View File

@ -0,0 +1,49 @@
use crate::error::FilenameDecryptError;
use cipher::{block_padding::Pkcs7, inout::InOutBufReserved, Iv, Key, KeyIvInit};
use super::{EmeCipher, EncodedFilename, IntoDecodable};
pub struct DirFilenameCipher<'a, 'b> {
filename_key: &'a Key<EmeCipher>,
iv: &'b Iv<EmeCipher>,
}
impl<'a, 'b> DirFilenameCipher<'a, 'b> {
pub fn new(filename_key: &'a Key<EmeCipher>, iv: &'b Iv<EmeCipher>) -> Self {
Self { filename_key, iv }
}
pub fn decode_filename<S>(&self, name: S) -> Result<String, FilenameDecryptError>
where
S: IntoDecodable,
{
let cipher = EmeCipher::new(self.filename_key, self.iv);
let mut filename = base64::decode_config(name.to_decodable(), base64::URL_SAFE_NO_PAD)?;
let filename_decoded = cipher
.decrypt_padded_mut::<Pkcs7>(&mut filename)
.map_err(|_| FilenameDecryptError::DecryptError())?;
Ok(String::from_utf8_lossy(filename_decoded).to_string())
}
pub fn encrypt_filename(
&self,
plain_text_name: &str,
) -> Result<EncodedFilename, FilenameDecryptError> {
let mut cipher = EmeCipher::new(self.filename_key, self.iv);
let mut res = [0u8; 2048];
let filename_encrypted = cipher
.encrypt_padded_inout_mut::<Pkcs7>(
InOutBufReserved::from_slices(plain_text_name.as_bytes(), &mut res).unwrap(),
)
.map_err(|_| FilenameDecryptError::DecryptError())?; // TODO RENAME ERROR
// TODO LONG FILENAME
let filename = base64::encode_config(filename_encrypted, base64::URL_SAFE_NO_PAD);
Ok(filename.into())
}
}

View File

@ -2,11 +2,11 @@
use std::{fs::File, io::Read, path::Path}; use std::{fs::File, io::Read, path::Path};
use content_enc::ContentEnc; use content::ContentEnc;
use filename::FilenameCipher; use filename::FilenameCipher;
pub mod config; pub mod config;
pub mod content_enc; pub mod content;
pub mod error; pub mod error;
pub mod filename; pub mod filename;

View File

@ -10,7 +10,7 @@ use std::{
}; };
use fuser::{FileAttr, FileType, Filesystem, FUSE_ROOT_ID}; use fuser::{FileAttr, FileType, Filesystem, FUSE_ROOT_ID};
use rustcryptfs_lib::{content_enc::ContentEnc, GocryptFs}; use rustcryptfs_lib::{content::ContentEnc, GocryptFs};
use crate::{ use crate::{
error::Result, error::Result,