Skip to content

Commit

Permalink
feat(hsh): decoupling models
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Sep 10, 2023
1 parent 0cfe40e commit 0d037bf
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 35 deletions.
4 changes: 2 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 @@ -50,7 +50,7 @@ path = "examples/hsh.rs"

[dependencies]
argon2rs = "0.2.5"
base64 = "0.21.3"
base64 = "0.21.4"
bcrypt = "0.15.0"
scrypt = "0.11.0"
serde = { version = "1.0.188", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use criterion::{
};

extern crate hsh;
use self::hsh::Hash;
use hsh::models::data::Hash;

fn generate_hash_benchmark(c: &mut Criterion) {
c.bench_function("generate_hash", |b| {
Expand Down
3 changes: 2 additions & 1 deletion examples/hsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Using the Hash (HSH) library
use hsh::{new_hash, Hash, HashAlgorithm};
use hsh::{new_hash, HashAlgorithm};
use hsh::models::data::*;
use std::str::FromStr;

// Creating and verifying hashes
Expand Down
Empty file added src/constants.rs
Empty file.
55 changes: 29 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
//! ```rust
//! // Import the Hash struct
//! extern crate hsh;
//! use hsh::Hash;
//! use hsh::models::data::Hash;
//!
//! // Main function
//! fn main() {
Expand Down Expand Up @@ -169,38 +169,41 @@ extern crate base64;
extern crate bcrypt;
extern crate scrypt;
extern crate vrd;
use crate::models::data::*;
use argon2rs::argon2i_simple;
use base64::{engine::general_purpose, Engine as _};

use scrypt::scrypt;
use serde::{Deserialize, Serialize};
use std::{fmt, str::FromStr};
use vrd::Random;

/// A type alias for a salt.
pub type Salt = Vec<u8>;

/// A struct for storing and verifying hashed passwords based on the argon2rs crate
#[non_exhaustive]
#[derive(
Clone,
Debug,
Eq,
Hash,
Ord,
PartialEq,
PartialOrd,
Serialize,
Deserialize,
)]
pub struct Hash {
/// The password hash.
pub hash: Vec<u8>,
/// The salt used for hashing
pub salt: Salt,
/// The hash algorithm used
pub algorithm: HashAlgorithm,
}
/// The `models` module contains the data models for the library.
pub mod models;

// /// A type alias for a salt.
// pub type Salt = Vec<u8>;

// /// A struct for storing and verifying hashed passwords based on the argon2rs crate
// #[non_exhaustive]
// #[derive(
// Clone,
// Debug,
// Eq,
// Hash,
// Ord,
// PartialEq,
// PartialOrd,
// Serialize,
// Deserialize,
// )]
// pub struct Hash {
// /// The password hash.
// pub hash: Vec<u8>,
// /// The salt used for hashing
// pub salt: Salt,
// /// The hash algorithm used
// pub algorithm: HashAlgorithm,
// }

/// The supported hash algorithms
#[non_exhaustive]
Expand Down
228 changes: 228 additions & 0 deletions src/loggers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// Copyright © 2023 Hash (HSH) library. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

// Standard library imports for formatting and I/O operations.
use std::{
fmt,
io::{self, Write as IoWrite},
fs::OpenOptions,
};


/// Enum representing the different log formats that can be used.
///
/// This enum allows the developer to specify the format in which log messages should be displayed.
///
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum LogFormat {
/// The log format is set to the Common Log Format (CLF)
CLF,
/// The log format is set to the JSON format
JSON,
/// The log format is set to the Common Event Format (CEF)
CEF,
/// The log format is set to the Extended Log Format (ELF)
ELF,
/// The log format is set to the W3C Extended Log File Format
W3C,
/// The log format is set to the Graylog Extended Log Format (GELF)
GELF,
}

/// Implements Display trait for LogFormat enum.
///
/// This allows easy conversion of the log format enums to strings.
impl fmt::Display for LogFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "{:?}", self)
}
}

/// An enumeration of the different levels that a log message can have.
/// Each variant of the enumeration represents a different level of
/// importance.
///
/// # Arguments
///
/// * `ALL` - The log level is set to all.
/// * `DEBUG` - The log level is set to debug.
/// * `DISABLED` - The log level is set to disabled.
/// * `ERROR` - The log level is set to error.
/// * `FATAL` - The log level is set to fatal.
/// * `INFO` - The log level is set to info.
/// * `NONE` - The log level is set to none.
/// * `TRACE` - The log level is set to trace.
/// * `VERBOSE` - The log level is set to verbose.
/// * `WARNING` - The log level is set to warning.
///
#[derive(Debug, PartialEq, Eq, Hash, Clone,PartialOrd)]
pub enum LogLevel {
/// The log level is set to all.
ALL,
/// The log level is set to debug.
DEBUG,
/// The log level is set to disabled.
DISABLED,
/// The log level is set to error.
ERROR,
/// The log level is set to fatal.
FATAL,
/// The log level is set to info.
INFO,
/// The log level is set to none.
NONE,
/// The log level is set to trace.
TRACE,
/// The log level is set to verbose.
VERBOSE,
/// The log level is set to warning.
WARNING,
}
/// Display trait implementation for `LogLevel`.
///
/// This converts the enum to a string representation.
impl fmt::Display for LogLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}

/// Struct representing a log message.
///
/// Contains all the elements that make up a complete log message.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Log {
/// A string that holds a session ID. The session ID is a unique
/// identifier for the current session. A random GUID (Globally
/// Unique Identifier) is generated by default.
pub session_id: String,
/// A string that holds the timestamp in ISO 8601 format.
pub time: String,
/// A string that holds the level (INFO, WARN, ERROR, etc.).
pub level: LogLevel,
/// A string that holds the component name.
pub component: String,
/// A string that holds the description of the log message.
pub description: String,
/// A string that holds the log format.
pub format: LogFormat,
}

impl Log {
/// Logs a message to the console using a pre-allocated buffer to
/// reduce memory allocation and flush the output buffer to ensure
/// that the message is written immediately.
///
/// # Errors
///
/// This function will panic if an error occurs when writing to the
/// pre-allocated buffer or flushing the output buffer.
pub fn log(&self) -> io::Result<()> {
// Open the file in append mode. If the file does not exist, create it.
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.open("shokunin.log")?;
match self.format {
LogFormat::CLF => {
writeln!(
file,
"SessionID={}\tTimestamp={}\tDescription={}\tLevel={}\tComponent={}\tFormat={}",
self.session_id, self.time, self.description, self.level, self.component, self.format
)
},
LogFormat::JSON => {
writeln!(
file,
r#"{{"session_id": "{}", "timestamp": "{}", "description": "{}", "level": "{}", "component": "{}", "format": "{}"}}"#,
self.session_id, self.time, self.description, self.level, self.component, self.format
)
},
LogFormat::CEF => {
writeln!(
file,
r#"[CEF]
<Event xmlns="http://www.w3.org/2003/05/events/Log">
<LogID>1</LogID>
<SourceName>shokunin</SourceName>
<SourceType>Application</SourceType>
<EventReceivedTime>{}</EventReceivedTime>
<EventType>Log</EventType>
<Severity>{}</Severity>
<Message>{}</Message>
<SessionID>{}</SessionID>
<HostName>localhost</HostName>
<ComputerName>localhost</ComputerName>
<UserID>-</UserID>
<ThreadID>-</ThreadID>
<FileName>-</FileName>
<LineNumber>-</LineNumber>
<ProcessID>-</ProcessID>
<ModuleID>-</ModuleID>
</Event>
"#,
self.time, self.level, self.description, self.session_id
)
},
_ => Err(io::Error::new(io::ErrorKind::InvalidInput, "Unsupported log format")),
}?;
file.flush()?;
Ok(())
}

/// Creates a new `Log` instance.
///
/// Initializes a new `Log` struct with the provided details.
///
/// # Returns
///
/// Returns a new instance of the `Log` struct.
pub fn new(
session_id: &str,
time: &str,
level: LogLevel,
component: &str,
description: &str,
format: LogFormat,
) -> Self {
Self {
session_id: session_id.to_string(),
time: time.to_string(),
level,
component: component.to_string(),
description: description.to_string(),
format,
}
}
}

/// Provides default values for `Log`.
///
/// This implementation provides a quick way to generate a `Log` instance with default values.
impl Default for Log {
fn default() -> Self {
Self {
session_id: String::default(),
time: String::default(),
level: LogLevel::INFO, // Default log level
component: String::default(),
description: String::default(),
format: LogFormat::CLF, // Default log format
}
}
}
#[cfg(test)]
/// Tests for the `log_info!` macro.
mod tests {
use crate::macro_log_info;

#[test]
fn test_log_info() {
macro_log_info!(
LogLevel::INFO,
"component",
"description",
LogFormat::CLF
);
}
}
6 changes: 3 additions & 3 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ macro_rules! match_algo {
///
/// ```
/// extern crate hsh;
/// use hsh::Hash;
/// use hsh::models::data::Hash;
/// use hsh::{ generate_hash, HashAlgorithm };
///
/// let password = "password";
Expand All @@ -391,7 +391,7 @@ macro_rules! generate_hash {
///
/// ```
/// extern crate hsh;
/// use hsh::Hash;
/// use hsh::models::data::Hash;
/// use hsh::{ new_hash, HashAlgorithm };
///
/// let password = "password";
Expand All @@ -415,7 +415,7 @@ macro_rules! new_hash {
///
/// ```
/// extern crate hsh;
/// use hsh::Hash;
/// use hsh::models::data::Hash;
/// use hsh::{ hash_length };
/// use hsh::{ new_hash, HashAlgorithm };
///
Expand Down
Loading

0 comments on commit 0d037bf

Please sign in to comment.