Skip to content

Commit

Permalink
style: format
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdoret committed Jul 22, 2024
1 parent e63d172 commit 716a830
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
25 changes: 8 additions & 17 deletions src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ pub(crate) fn generate_key(size: usize) -> Vec<u8> {
}

pub trait Pseudonymize {

// implementers need to define raw bytes pseudonymization
fn pseudo(&self, input: &[u8]) -> String;
fn pseudo(&self, input: &[u8]) -> String;

// Pseudonymize parts of a triple set by its mask
fn pseudo_triple(&self, triple: &Triple, mask: TripleMask) -> Triple {
Expand All @@ -31,7 +30,7 @@ pub trait Pseudonymize {
subject: Subject::from(pseudo_subject.clone()),
predicate: triple.predicate.clone(),
object: Term::from(pseudo_object.clone()),
}
};
}

fn pseudo_entity(&self, e: &Entity) -> Entity {
Expand All @@ -44,12 +43,12 @@ pub trait Pseudonymize {
// private methods? Blanket implementations
fn pseudo_named_node(&self, t: &NamedNode) -> NamedNode {
// We check for the last fragment or path separator in the IRI
let prefix_end= t.iri.rfind(|c: char| (c == '/') || (c == '#')).unwrap();
let prefix_end = t.iri.rfind(|c: char| (c == '/') || (c == '#')).unwrap();
let prefix = &t.iri[0..=prefix_end];
let crypted = self.pseudo(t.iri.as_bytes()).to_string();
return NamedNode {
iri: format!("{prefix}{crypted}"),
}
};
}

fn pseudo_literal(&self, l: &Literal) -> Literal {
Expand All @@ -59,9 +58,7 @@ pub trait Pseudonymize {
Literal::Simple { value } => value,
};
let crypted = self.pseudo(value.as_bytes());
return Literal::Simple {
value: crypted,
};
return Literal::Simple { value: crypted };
}

fn pseudo_blank_node(&self, u: &BlankNode) -> BlankNode {
Expand All @@ -77,18 +74,17 @@ pub enum Algorithm {

impl Default for Algorithm {
fn default() -> Self {
return Algorithm::Blake3
return Algorithm::Blake3;
}
}

pub enum Hasher {
Blake3(Blake3Hasher),
}


impl Hasher {
pub fn new(algo: Algorithm, key: Option<Vec<u8>>) -> Self {
let salt= key.unwrap_or(generate_key(32));
let salt = key.unwrap_or(generate_key(32));
match algo {
Algorithm::Blake3 => Hasher::Blake3(Blake3Hasher::new(salt)),
}
Expand All @@ -107,8 +103,6 @@ impl Default for Hasher {
}
}



// BLAKE3 hasher

pub struct Blake3Hasher {
Expand All @@ -117,7 +111,6 @@ pub struct Blake3Hasher {

impl Blake3Hasher {
pub fn new(key: Vec<u8>) -> Self {

let mut new_key = [0u8; 32];
if key.len() == 32 {
new_key.copy_from_slice(&key[..32]);
Expand All @@ -127,12 +120,10 @@ impl Blake3Hasher {

return Self { key: new_key };
}

}

impl Pseudonymize for Blake3Hasher {
fn pseudo(&self, data: &[u8]) -> String{
fn pseudo(&self, data: &[u8]) -> String {
return blake3::keyed_hash(&self.key, &data).to_string();
}

}
15 changes: 7 additions & 8 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::rules::Rules;
use rio_turtle::NTriplesParser;
use std::{
fs::File,
io::{self, Read, stdin, stdout, BufRead, BufReader, BufWriter},
io::{self, stdin, stdout, BufRead, BufReader, BufWriter, Read},
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -52,15 +52,14 @@ pub fn parse_config(path: &Path) -> Rules {

// Read cryptographic key from input file.
pub fn get_key(path: &PathBuf) -> Vec<u8> {
let mut key_file = File::open(path).expect("Error opening key file.");
let mut key = Vec::new();
key_file
.read_to_end(&mut key)
.expect("Error reading key file.");
let mut key_file = File::open(path).expect("Error opening key file.");
let mut key = Vec::new();
key_file
.read_to_end(&mut key)
.expect("Error reading key file.");

return key
return key;
}


#[cfg(test)]
mod tests {
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ fn main() {
}
Subcommands::Pseudo(args) => {
info!(log, "Args: {:?}", args);
pseudonymize_graph(&log, &args.input, &args.config, &args.output, &args.index, &args.secret)
pseudonymize_graph(
&log,
&args.input,
&args.config,
&args.output,
&args.index,
&args.secret,
)
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/pass_second.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ fn load_type_map(input: impl BufRead) -> HashMap<String, String> {
return node_to_type;
}

pub fn pseudonymize_graph(_: &Logger, input: &Path, config: &Path, output: &Path, index: &Path, secret: &Option<PathBuf>) {
pub fn pseudonymize_graph(
_: &Logger,
input: &Path,
config: &Path,
output: &Path,
index: &Path,
secret: &Option<PathBuf>,
) {
let buf_input = io::get_reader(input);
let buf_index = io::get_reader(index);
let mut buf_output = io::get_writer(output);
Expand All @@ -91,7 +98,13 @@ pub fn pseudonymize_graph(_: &Logger, input: &Path, config: &Path, output: &Path
while !triples.is_end() {
triples
.parse_step(&mut |t: TripleView| {
process_triple(t.into(), &rules_config, &node_to_type, &mut buf_output, pseudonymizer);
process_triple(
t.into(),
&rules_config,
&node_to_type,
&mut buf_output,
pseudonymizer,
);
Result::<(), TurtleError>::Ok(())
})
.inspect_err(|e| {
Expand Down

0 comments on commit 716a830

Please sign in to comment.