Skip to content

Commit

Permalink
Make the hashing run on spawn_blocking.
Browse files Browse the repository at this point in the history
  • Loading branch information
FireMasterK committed Nov 17, 2023
1 parent 6f1cf2a commit a69bc32
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,32 +151,39 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {

let qhash = qhash.unwrap();

// check that qhash is valid
if qhash.len() != 8 {
return Err("Invalid qhash provided".into());
}

// store sorted key-value pairs
// Store sorted key-value pairs
let mut set = BTreeSet::new();

query.to_pairs().iter().for_each(|(key, value)| {
if matches!(*key, "qhash" | "range" | "rewrite") {
return;
{
let pairs = query.to_pairs();
for (key, value) in &pairs {
if matches!(*key, "qhash" | "range" | "rewrite") {
continue;
}
set.insert((key.as_bytes().to_owned(), value.as_bytes().to_owned()));
}
set.insert((key.as_bytes(), value.as_bytes()));
});
}

let mut hasher = blake3::Hasher::new();
let (tx, rx) = oneshot::channel::<String>();
spawn_blocking(move || {
let mut hasher = blake3::Hasher::new();

for (key, value) in set {
hasher.update(key);
hasher.update(value);
}
for (key, value) in set {
hasher.update(&key);
hasher.update(&value);
}

hasher.update(secret.as_bytes());
hasher.update(secret.as_bytes());

let hash = hasher.finalize().to_hex();
let hash = hash[..8].to_owned();
tx.send(hash).unwrap();
});

let hash = hasher.finalize().to_hex();
let hash = &hash[..8];
let hash = rx.await.unwrap();

if hash != qhash {
return Err("Invalid qhash provided".into());
Expand Down

0 comments on commit a69bc32

Please sign in to comment.