Переглянути джерело

renamed `EncryptKind` to `EncryptFlag`

master
Isabelle L. 5 роки тому
джерело
коміт
4a35869292
4 змінених файлів з 25 додано та 25 видалено
  1. +2
    -2
      src/agreement.rs
  2. +11
    -11
      src/encrypt.rs
  3. +10
    -10
      src/lib.rs
  4. +2
    -2
      src/message.rs

+ 2
- 2
src/agreement.rs Переглянути файл

@@ -27,10 +27,10 @@ impl Agreement {
} }


impl crate::Sendable for Agreement { impl crate::Sendable for Agreement {
fn to_packet(&self, encrypt_kind: crate::EncryptKind) -> Result<Packet> {
fn to_packet(&self, encrypt_flag: crate::EncryptFlag) -> Result<Packet> {
let contents: Vec<u8> = serde_json::to_string(&self)?.into_bytes(); let contents: Vec<u8> = serde_json::to_string(&self)?.into_bytes();
let kind = 0xff; let kind = 0xff;
Ok(Packet::new(kind, contents, encrypt_kind))
Ok(Packet::new(kind, contents, encrypt_flag))
} }


fn from_packet(packet: Packet) -> Result<Self> { fn from_packet(packet: Packet) -> Result<Self> {


+ 11
- 11
src/encrypt.rs Переглянути файл

@@ -7,7 +7,7 @@ use ring::digest;
/// trait that allows for me to be lazy /// trait that allows for me to be lazy
pub trait Encryption { pub trait Encryption {
/// return the encryption kind /// return the encryption kind
fn kind(&self) -> EncryptKind;
fn kind(&self) -> EncryptFlag;
/// returns Option<SecretKey> /// returns Option<SecretKey>
fn key(&self) -> Option<&SecretKey>; fn key(&self) -> Option<&SecretKey>;
/// encrypts the packet contents and updates the integrity hash /// encrypts the packet contents and updates the integrity hash
@@ -21,8 +21,8 @@ pub trait Encryption {
pub struct SymmetricEncrypt(SecretKey); pub struct SymmetricEncrypt(SecretKey);


impl Encryption for SymmetricEncrypt { impl Encryption for SymmetricEncrypt {
fn kind(&self) -> EncryptKind {
EncryptKind::Symmetric
fn kind(&self) -> EncryptFlag {
EncryptFlag::Symmetric
} }


fn key(&self) -> Option<&SecretKey> { fn key(&self) -> Option<&SecretKey> {
@@ -69,8 +69,8 @@ impl NoEncrypt {
} }


impl Encryption for NoEncrypt { impl Encryption for NoEncrypt {
fn kind(&self) -> EncryptKind {
EncryptKind::None
fn kind(&self) -> EncryptFlag {
EncryptFlag::None
} }


// lol // lol
@@ -92,17 +92,17 @@ impl Encryption for NoEncrypt {
/// encryption kind /// encryption kind
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)] #[repr(u8)]
pub enum EncryptKind {
pub enum EncryptFlag {
None = 0x00, None = 0x00,
Symmetric = 0xff, Symmetric = 0xff,
} }


impl EncryptKind {
/// returns `EncryptKind` from u8 if returned value is valid
pub fn from_u8(kind: u8) -> Option<EncryptKind> {
impl EncryptFlag {
/// returns `EncryptFlag` from u8 if returned value is valid
pub fn from_u8(kind: u8) -> Option<EncryptFlag> {
match kind { match kind {
0x00 => Some(EncryptKind::None),
0xff => Some(EncryptKind::Symmetric),
0x00 => Some(EncryptFlag::None),
0xff => Some(EncryptFlag::Symmetric),
_ => None, _ => None,
} }
} }


+ 10
- 10
src/lib.rs Переглянути файл

@@ -24,7 +24,7 @@ pub use agreement::Agreement;
pub mod encrypt; pub mod encrypt;


// namespacing // namespacing
use encrypt::{EncryptKind, Encryption};
use encrypt::{EncryptFlag, Encryption};
use futures_util::io::{AsyncReadExt, AsyncWriteExt}; use futures_util::io::{AsyncReadExt, AsyncWriteExt};
use orion::aead; use orion::aead;
use ring::{agreement as agree, digest, rand}; use ring::{agreement as agree, digest, rand};
@@ -41,7 +41,7 @@ struct NetworkPacket(Vec<u8>);
/// a type of data that can be sent /// a type of data that can be sent
pub trait Sendable: Sized { pub trait Sendable: Sized {
/// create a packet from the struct /// create a packet from the struct
fn to_packet(&self, encrypt_flag: EncryptKind) -> Result<Packet>;
fn to_packet(&self, encrypt_flag: EncryptFlag) -> Result<Packet>;
/// create the struct from a packet /// create the struct from a packet
fn from_packet(packet: Packet) -> Result<Self>; fn from_packet(packet: Packet) -> Result<Self>;
/// returns the sendable's packet kind /// returns the sendable's packet kind
@@ -52,14 +52,14 @@ pub trait Sendable: Sized {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Packet { pub struct Packet {
pub kind: u8, pub kind: u8,
pub encrypt_flag: EncryptKind,
pub encrypt_flag: EncryptFlag,
pub integrity_hash: Vec<u8>, pub integrity_hash: Vec<u8>,
pub contents: Vec<u8>, pub contents: Vec<u8>,
} }


impl Packet { impl Packet {
/// create a new `Packet` /// create a new `Packet`
pub fn new(kind: u8, contents: Vec<u8>, encrypt_flag: EncryptKind) -> Packet {
pub fn new(kind: u8, contents: Vec<u8>, encrypt_flag: EncryptFlag) -> Packet {
let integrity_hash = digest::digest(&digest::SHA256, &contents).as_ref().to_vec(); let integrity_hash = digest::digest(&digest::SHA256, &contents).as_ref().to_vec();
Packet { Packet {
kind, kind,
@@ -189,7 +189,7 @@ where
} }


let kind = info_buf[0]; let kind = info_buf[0];
let encrypt_flag = EncryptKind::from_u8(info_buf[1]).unwrap();
let encrypt_flag = EncryptFlag::from_u8(info_buf[1]).unwrap();
let length = u64::from_le_bytes(info_buf[2..10].try_into().unwrap()) as usize; let length = u64::from_le_bytes(info_buf[2..10].try_into().unwrap()) as usize;
let checksum = u32::from_le_bytes(info_buf[10..14].try_into().unwrap()); let checksum = u32::from_le_bytes(info_buf[10..14].try_into().unwrap());


@@ -209,7 +209,7 @@ where
packet.verify_checksum(checksum)?; packet.verify_checksum(checksum)?;
packet.verify_integrity()?; packet.verify_integrity()?;


if packet.encrypt_flag == EncryptKind::Symmetric {
if packet.encrypt_flag == EncryptFlag::Symmetric {
encryption.decrypt(&mut packet)?; encryption.decrypt(&mut packet)?;
} }
Ok(Some(packet)) Ok(Some(packet))
@@ -223,12 +223,12 @@ where
E: Encryption, E: Encryption,
{ {
match encryption.kind() { match encryption.kind() {
EncryptKind::None => {
EncryptFlag::None => {
let network_packet = packet.to_packet(encryption.kind())?.to_network_packet(); let network_packet = packet.to_packet(encryption.kind())?.to_network_packet();
stream.write(&network_packet.0).await?; stream.write(&network_packet.0).await?;
Ok(()) Ok(())
} }
EncryptKind::Symmetric => {
EncryptFlag::Symmetric => {
let mut packet = packet.to_packet(encryption.kind())?; let mut packet = packet.to_packet(encryption.kind())?;
encryption.encrypt(&mut packet)?; encryption.encrypt(&mut packet)?;
let network_packet = packet.to_network_packet(); let network_packet = packet.to_network_packet();
@@ -245,12 +245,12 @@ where
E: Encryption, E: Encryption,
{ {
match encryption.kind() { match encryption.kind() {
EncryptKind::None => {
EncryptFlag::None => {
let network_packet = packet.to_network_packet(); let network_packet = packet.to_network_packet();
stream.write(&network_packet.0).await?; stream.write(&network_packet.0).await?;
Ok(()) Ok(())
} }
EncryptKind::Symmetric => {
EncryptFlag::Symmetric => {
let mut packet = packet; let mut packet = packet;
encryption.encrypt(&mut packet)?; encryption.encrypt(&mut packet)?;
let network_packet = packet.to_network_packet(); let network_packet = packet.to_network_packet();


+ 2
- 2
src/message.rs Переглянути файл

@@ -28,10 +28,10 @@ impl Message {
} }


impl crate::Sendable for Message { impl crate::Sendable for Message {
fn to_packet(&self, encrypt_kind: crate::EncryptKind) -> Result<Packet> {
fn to_packet(&self, encrypt_flag: crate::EncryptFlag) -> Result<Packet> {
let contents: Vec<u8> = serde_json::to_string(&self)?.into_bytes(); let contents: Vec<u8> = serde_json::to_string(&self)?.into_bytes();
let kind = 0x00; let kind = 0x00;
Ok(Packet::new(kind, contents, encrypt_kind))
Ok(Packet::new(kind, contents, encrypt_flag))
} }


fn from_packet(packet: Packet) -> Result<Self> { fn from_packet(packet: Packet) -> Result<Self> {


Завантаження…
Відмінити
Зберегти