Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cargo replace hex with faster-hex #1227

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ version = "0.5.0-dev"

[dependencies]
env_logger = { version = "0.10", default-features = false, features = ["color", "humantime", "auto-color"] }
faster-hex = "0.9"
futures-util = { version = "0.3", default_features = false }
getopts = "0.2"
hex = "0.4"
log = "0.4"
rpassword = "7.0"
sha1 = "0.10"
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ base64 = "0.21"
byteorder = "1.4"
bytes = "1"
dns-sd = { version = "0.1", optional = true }
faster-hex = "0.9"
form_urlencoded = "1.0"
futures-core = "0.3"
futures-util = { version = "0.3", features = ["alloc", "bilock", "sink", "unstable"] }
governor = { version = "0.6", default-features = false, features = ["std", "jitter"] }
hex = "0.4"
hmac = "0.12"
httparse = "1.7"
http = "0.2"
Expand Down
36 changes: 23 additions & 13 deletions core/src/spclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{

use byteorder::{BigEndian, ByteOrder};
use bytes::Bytes;
use faster_hex::{hex_decode, hex_string_upper};
use futures_util::future::IntoStream;
use http::header::HeaderValue;
use hyper::{
Expand Down Expand Up @@ -269,7 +270,10 @@ impl SpClient {
match ClientTokenResponseType::from_i32(message.response_type.value()) {
// depending on the platform, you're either given a token immediately
// or are presented a hash cash challenge to solve first
Some(ClientTokenResponseType::RESPONSE_GRANTED_TOKEN_RESPONSE) => break message,
Some(ClientTokenResponseType::RESPONSE_GRANTED_TOKEN_RESPONSE) => {
debug!("Received a granted token");
break message;
}
Some(ClientTokenResponseType::RESPONSE_CHALLENGES_RESPONSE) => {
debug!("Received a hash cash challenge, solving...");

Expand All @@ -279,20 +283,23 @@ impl SpClient {
let hash_cash_challenge = challenge.evaluate_hashcash_parameters();

let ctx = vec![];
let prefix = hex::decode(&hash_cash_challenge.prefix).map_err(|e| {
Error::failed_precondition(format!(
let mut prefix = Vec::with_capacity(hash_cash_challenge.prefix.len() / 2);
if let Err(e) =
hex_decode(hash_cash_challenge.prefix.as_bytes(), &mut prefix)
{
return Err(Error::failed_precondition(format!(
"Unable to decode hash cash challenge: {e}"
))
})?;
)));
}
let length = hash_cash_challenge.length;

let mut suffix = vec![0; 0x10];
let mut suffix = [0u8; 0x10];
let answer = Self::solve_hash_cash(&ctx, &prefix, length, &mut suffix);

match answer {
Ok(_) => {
// the suffix must be in uppercase
let suffix = hex::encode(suffix).to_uppercase();
let suffix = hex_string_upper(&suffix);

let mut answer_message = ClientTokenRequest::new();
answer_message.request_type =
Expand All @@ -302,7 +309,7 @@ impl SpClient {
let challenge_answers = answer_message.mut_challenge_answers();

let mut challenge_answer = ChallengeAnswer::new();
challenge_answer.mut_hash_cash().suffix = suffix.to_string();
challenge_answer.mut_hash_cash().suffix = suffix;
challenge_answer.ChallengeType =
ChallengeType::CHALLENGE_HASH_CASH.into();

Expand Down Expand Up @@ -477,11 +484,14 @@ impl SpClient {
HeaderValue::from_str(&format!("{} {}", token.token_type, token.access_token,))?,
);

if let Ok(client_token) = self.client_token().await {
headers_mut.insert(CLIENT_TOKEN, HeaderValue::from_str(&client_token)?);
} else {
// currently these endpoints seem to work fine without it
warn!("Unable to get client token. Trying to continue without...");
match self.client_token().await {
Ok(client_token) => {
let _ = headers_mut.insert(CLIENT_TOKEN, HeaderValue::from_str(&client_token)?);
}
Err(e) => {
// currently these endpoints seem to work fine without it
warn!("Unable to get client token: {e} Trying to continue without...")
}
}

last_response = self.session().http_client().request_body(request).await;
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use faster_hex::hex_string;
use futures_util::StreamExt;
use log::{debug, error, info, trace, warn};
use sha1::{Digest, Sha1};
Expand Down Expand Up @@ -39,7 +40,7 @@ mod player_event_handler;
use player_event_handler::{run_program_on_sink_events, EventHandler};

fn device_id(name: &str) -> String {
hex::encode(Sha1::digest(name.as_bytes()))
hex_string(&Sha1::digest(name.as_bytes()))
}

fn usage(program: &str, opts: &getopts::Options) -> String {
Expand Down