Skip to content

Commit

Permalink
Fix clippy warnings (dbrgn#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrgn committed Feb 21, 2022
1 parent ce14f82 commit 705c9de
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
37 changes: 19 additions & 18 deletions src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
//! Encrypt and decrypt messages.
use std::convert::Into;
use std::io::Write;
use std::iter::repeat;
use std::str::FromStr;
use std::{convert::Into, io::Write, iter::repeat, str::FromStr};

use byteorder::{LittleEndian, WriteBytesExt};
use data_encoding::{HEXLOWER, HEXLOWER_PERMISSIVE};
use serde_json as json;
use sodiumoxide::crypto::{box_, secretbox};
use sodiumoxide::randombytes::randombytes_into;
use sodiumoxide::{
crypto::{box_, secretbox},
randombytes::randombytes_into,
};

use crate::errors::CryptoError;
use crate::types::{BlobId, FileMessage, MessageType};
use crate::{PublicKey, SecretKey};
use crate::{
errors::CryptoError,
types::{BlobId, FileMessage, MessageType},
PublicKey, SecretKey,
};

/// Return a random number in the range `[1, 255]`.
fn random_padding_amount() -> u8 {
Expand Down Expand Up @@ -89,7 +90,7 @@ pub fn encrypt_raw(
) -> EncryptedMessage {
sodiumoxide::init().expect("Could not initialize sodiumoxide library.");
let nonce = box_::gen_nonce();
let ciphertext = box_::seal(&data, &nonce, public_key, private_key);
let ciphertext = box_::seal(data, &nonce, public_key, private_key);
EncryptedMessage {
ciphertext,
nonce: nonce.0,
Expand All @@ -115,7 +116,7 @@ pub fn encrypt(
.collect();

// Encrypt
encrypt_raw(&padded_plaintext, &public_key, &private_key)
encrypt_raw(&padded_plaintext, public_key, private_key)
}

/// Encrypt an image message for the recipient.
Expand Down Expand Up @@ -150,7 +151,7 @@ pub fn encrypt_file_msg(
) -> EncryptedMessage {
let data = json::to_string(msg).unwrap();
let msgtype = MessageType::File;
encrypt(&data.as_bytes(), msgtype, &public_key, &private_key)
encrypt(data.as_bytes(), msgtype, public_key, private_key)
}

static FILE_NONCE: secretbox::Nonce = secretbox::Nonce([
Expand All @@ -176,8 +177,8 @@ pub fn encrypt_file_data(

// Encrypt data
// Note: Since we generate a random key, we can safely re-use constant nonces.
let encrypted_file = secretbox::seal(&file_data, &FILE_NONCE, &key);
let encrypted_thumb = thumb_data.map(|t| secretbox::seal(&t, &THUMB_NONCE, &key));
let encrypted_file = secretbox::seal(file_data, &FILE_NONCE, &key);
let encrypted_thumb = thumb_data.map(|t| secretbox::seal(t, &THUMB_NONCE, &key));

(encrypted_file, encrypted_thumb, key)
}
Expand Down Expand Up @@ -288,19 +289,19 @@ mod test {
#[test]
fn test_recipient_key_from_str() {
let encoded = "5cf143cd8f3652f31d9b44786c323fbc222ecfcbb8dac5caf5caa257ac272df0";
let recipient = RecipientKey::from_str(&encoded);
let recipient = RecipientKey::from_str(encoded);
assert!(recipient.is_ok());

let encoded = "5CF143CD8F3652F31D9B44786C323FBC222ECFCBB8DAC5CAF5CAA257AC272DF0";
let recipient = RecipientKey::from_str(&encoded);
let recipient = RecipientKey::from_str(encoded);
assert!(recipient.is_ok());

let too_short = "5cf143cd8f3652f31d9b44786c323fbc222ecfcbb8dac5ca";
let recipient = RecipientKey::from_str(&too_short);
let recipient = RecipientKey::from_str(too_short);
assert!(recipient.is_err());

let invalid = "qyz143cd8f3652f31d9b44786c323fbc222ecfcbb8dac5caf5caa257ac272df0";
let recipient = RecipientKey::from_str(&invalid);
let recipient = RecipientKey::from_str(invalid);
assert!(recipient.is_err());
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
//! For more examples, see the
//! [`examples/`](https://github.com/dbrgn/threema-gateway-rs/tree/master/examples) directory.
#![allow(clippy::collapsible_else_if)]
#![allow(clippy::too_many_arguments)]

#[macro_use]
extern crate log;

Expand Down
2 changes: 1 addition & 1 deletion src/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl IncomingMessage {
let nonce: Nonce = Nonce::from_slice(&self.nonce).ok_or(CryptoError::BadNonce)?;

// Decrypt bytes
let mut decrypted = box_::open(&self.box_data, &nonce, &public_key, &private_key)
let mut decrypted = box_::open(&self.box_data, &nonce, public_key, private_key)
.map_err(|_| CryptoError::DecryptionFailed)?;

// Remove PKCS#7 style padding
Expand Down
31 changes: 15 additions & 16 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::default::Default;
use std::fmt;
use std::str::FromStr;
use std::string::ToString;
use std::{default::Default, fmt, str::FromStr, string::ToString};

use data_encoding::{HEXLOWER, HEXLOWER_PERMISSIVE};
use serde::{Serialize, Serializer};

use crate::errors::{ApiError, FileMessageBuilderError};
use crate::{Key, Mime};
use crate::{
errors::{ApiError, FileMessageBuilderError},
Key, Mime,
};

/// A message type.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand All @@ -19,9 +18,9 @@ pub enum MessageType {
DeliveryReceipt,
}

impl Into<u8> for MessageType {
fn into(self) -> u8 {
match self {
impl From<MessageType> for u8 {
fn from(val: MessageType) -> Self {
match val {
MessageType::Text => 0x01,
MessageType::Image => 0x02,
MessageType::Video => 0x13,
Expand All @@ -43,19 +42,19 @@ pub enum RenderingType {
Sticker,
}

impl Into<u8> for RenderingType {
fn into(self) -> u8 {
match self {
Self::File => 0,
Self::Media => 1,
Self::Sticker => 2,
impl From<RenderingType> for u8 {
fn from(val: RenderingType) -> Self {
match val {
RenderingType::File => 0,
RenderingType::Media => 1,
RenderingType::Sticker => 2,
}
}
}

impl Serialize for RenderingType {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u8(self.clone().into())
serializer.serialize_u8((*self).into())
}
}

Expand Down

0 comments on commit 705c9de

Please sign in to comment.