Skip to content

Commit

Permalink
Adapt to base64 v0.21
Browse files Browse the repository at this point in the history
Fix clippy warnings
  • Loading branch information
EmilSodergren committed Jul 5, 2023
1 parent 7754bea commit f698b0a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ log = "0.4"
md5 = "0.7"
nom = "7.1"
num = "0.4"
num-derive = "0.3"
num-derive = "0.4"
num-traits = "0.2"
openssl = { version = "0.10", features = ["vendored"] }
serde = { version = "1.0", features = ["derive"] }
Expand Down
11 changes: 6 additions & 5 deletions src/cipher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::mesparse:: TuyaVersion;
use crate::mesparse::TuyaVersion;
use crate::Result;
use base64::{engine::general_purpose, Engine as _};
use openssl::symm::{decrypt, encrypt, Cipher};

/// TuyaCipher is a low level api for encrypting and decrypting Vec<u8>'s.
Expand Down Expand Up @@ -32,17 +33,17 @@ impl TuyaCipher {
pub fn encrypt(&self, data: &[u8]) -> Result<Vec<u8>> {
let res = encrypt(self.cipher, &self.key, None, data)?;
match self.version {
TuyaVersion::ThreeOne => Ok(base64::encode(res).as_bytes().to_vec()),
TuyaVersion::ThreeOne => Ok(general_purpose::STANDARD.encode(res).as_bytes().to_vec()),
TuyaVersion::ThreeThree => Ok(res),
}
}

pub fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>> {
// Different header size in version 3.1 and 3.3
let data = maybe_strip_header(&self.version, &data);
let data = maybe_strip_header(&self.version, data);
// 3.1 is base64 encoded, 3.3 is not
let data = match self.version {
TuyaVersion::ThreeOne => base64::decode(&data)?,
TuyaVersion::ThreeOne => general_purpose::STANDARD.decode(&data)?,
TuyaVersion::ThreeThree => data.to_vec(),
};
let res = decrypt(self.cipher, &self.key, None, &data)?;
Expand Down Expand Up @@ -127,7 +128,7 @@ mod tests {
fn decrypt_message_with_version_threethree() {
let cipher = TuyaCipher::create(b"bbe88b3f4106d354", TuyaVersion::ThreeThree);
let message = b"zrA8OK3r3JMiUXpXDWauNppY4Am2c8rZ6sb4Yf15MjM8n5ByDx+QWeCZtcrPqddxLrhm906bSKbQAFtT1uCp+zP5AxlqJf5d0Pp2OxyXyjg=".to_vec();
let message = base64::decode(&message).unwrap();
let message = general_purpose::STANDARD.decode(&message).unwrap();
let expected =
r#"{"devId":"002004265ccf7fb1b659","dps":{"1":false,"2":0},"t":1529442366,"s":8}"#
.as_bytes()
Expand Down
6 changes: 3 additions & 3 deletions src/mesparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl MessageParser {
}
let command = mes.command.clone().ok_or(ErrorKind::CommandTypeMissing)?;
encoded.extend([0, 0, 0, command.to_u8().unwrap()].iter());
let payload = self.create_payload_header(&mes, encrypt)?;
let payload = self.create_payload_header(mes, encrypt)?;
let ret_len = match mes.ret_code {
Some(_) => 4_u32,
None => 0_u32,
Expand Down Expand Up @@ -304,11 +304,11 @@ impl MessageParser {
}
}
Err(_) => {
if let Ok(p) = serde_json::from_slice(&payload) {
if let Ok(p) = serde_json::from_slice(payload) {
Payload::Struct(p)
} else {
Payload::String(
std::str::from_utf8(&payload)
std::str::from_utf8(payload)
.unwrap_or("Payload invalid")
.to_string(),
)
Expand Down
4 changes: 2 additions & 2 deletions src/tuyadevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ impl TuyaDevice {
}

fn send(&self, mes: &Message, seq_id: u32) -> Result<Vec<Message>> {
let mut tcpstream = TcpStream::connect(&self.addr)?;
let mut tcpstream = TcpStream::connect(self.addr)?;
tcpstream.set_nodelay(true)?;
tcpstream.set_read_timeout(Some(Duration::new(2, 0)))?;
tcpstream.set_read_timeout(Some(Duration::new(2, 0)))?;
info!("Writing message to {} ({}):\n{}", self.addr, seq_id, &mes);
let bts = tcpstream.write(self.mp.encode(&mes, true)?.as_ref())?;
let bts = tcpstream.write(self.mp.encode(mes, true)?.as_ref())?;
info!("Wrote {} bytes ({})", bts, seq_id);
let mut buf = [0; 256];
let bts = tcpstream.read(&mut buf)?;
Expand Down

0 comments on commit f698b0a

Please sign in to comment.