initial commit

This commit is contained in:
MedzikUser 2022-06-11 12:42:32 +02:00
commit cdb7a78f83
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
6 changed files with 385 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

112
Cargo.lock generated Normal file
View File

@ -0,0 +1,112 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "block-buffer"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324"
dependencies = [
"generic-array",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cpufeatures"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59a6001667ab124aebae2a495118e11d30984c3a653e99d86d58971708cf5e4b"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "crypto-utils"
version = "0.1.0"
dependencies = [
"hex",
"sha1",
"sha2",
]
[[package]]
name = "digest"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "generic-array"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "libc"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]]
name = "sha1"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c77f4e7f65455545c2153c1253d25056825e77ee2533f0e41deb65a93a34852f"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "sha2"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55deaec60f81eefe3cce0dc50bda92d6d8e88f2a27df7c5033b42afeb1ed2676"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "typenum"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"

23
Cargo.toml Normal file
View File

@ -0,0 +1,23 @@
[package]
name = "crypto-utils"
description = "Cryptography Utils for Rust"
authors = ["MedzikUser <nivua1fn@duck.com>"]
license = "MIT"
keywords = ["crypto", "sha"]
categories = ["cryptography"]
homepage = "https://github.com/MedzikUser/rust-crypto-utils"
repository = "https://github.com/MedzikUser/rust-crypto-utils.git"
version = "0.1.0"
edition = "2021"
[features]
default = ["full"]
full = ["sha"]
sha = ["sha1", "sha2"]
[dependencies]
sha1 = { version = "0.10.1", optional = true }
sha2 = { version = "0.10.2", optional = true }
[dev-dependencies]
hex = { version = "0.4.3" }

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 MedzikUser
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

41
src/lib.rs Normal file
View File

@ -0,0 +1,41 @@
//! Cryptography Utils for Rust
//!
//! ## Importing
//! The driver is available on [crates.io](https://crates.io/crates/crypto-utils). To use the driver in
//! your application, simply add it to your project's `Cargo.toml`.
//!
//! ```toml
//! [dependencies]
//! crypto-utils = "0.1.0"
//! ```
//!
//! ## How to use?
//!
//! ### Compute a Sha hash
//!
//! Add `sha` features (is enabled by default)
//!
//! ```toml
//! [dependencies]
//! crypto-utils = { version = "...", features = ["sha"] }
//! ```
//!
//! Quick and easy Sha1, Sha256 and Sha512 hash computing.
//!
//! ```
//! use crypto_utils::sha::{Algorithm, CryptographicHash};
//!
//! // input data for a hasher
//! let input = "P@ssw0rd"; // &str
//!
//! // compute hash
//! let hash_bytes = CryptographicHash::hash(Algorithm::SHA1, input.as_bytes()); // Vec<u8>
//!
//! // decode hash to a String
//! let hash = hex::encode(hash_bytes); // String
//!
//! assert_eq!(hash, "21bd12dc183f740ee76f27b78eb39c8ad972a757".to_string())
//! ```
#[cfg(feature = "sha")]
pub mod sha;

187
src/sha.rs Normal file
View File

@ -0,0 +1,187 @@
//! Module for creating sha1, sha256 and sha512 hashes.
//!
//! **Required `sha` feature!**
//!
//! Examples:
//! ```
//! use crypto_utils::sha::{Algorithm, CryptographicHash};
//!
//! // sha1
//! CryptographicHash::hash(Algorithm::SHA1, b"P@ssw0rd");
//!
//! // sha256
//! CryptographicHash::hash(Algorithm::SHA256, b"P@ssw0rd");
//!
//! // sha512
//! CryptographicHash::hash(Algorithm::SHA512, b"P@ssw0rd");
//! ```
use sha1::{Digest, Sha1};
use sha2::{Sha256, Sha512};
/// Hashing algorithms
#[derive(Debug)]
pub enum Algorithm {
SHA1,
SHA256,
SHA512,
}
/// Compute cryptographic hash from bytes (sha1, sha256, sha512).
///
/// Method 1
/// ```
/// use crypto_utils::sha::{Algorithm, CryptographicHash};
///
/// // compute hash
/// let hash_bytes = CryptographicHash::hash(Algorithm::SHA1, b"P@ssw0rd");
///
/// // decode hash to a String
/// let hash = hex::encode(hash_bytes);
///
/// assert_eq!(hash, "21bd12dc183f740ee76f27b78eb39c8ad972a757".to_string())
/// ```
///
/// Method 2
/// ```
/// use crypto_utils::sha::{Algorithm, CryptographicHash};
///
/// // create a new hasher
/// let mut sha1 = CryptographicHash::new(Algorithm::SHA1);
///
/// // set value in hasher
/// sha1.update(b"P@ssw0rd");
///
/// // compute hash
/// let hash_bytes = sha1.finalize();
///
/// // decode hash to a String
/// let hash = hex::encode(hash_bytes);
///
/// assert_eq!(hash, "21bd12dc183f740ee76f27b78eb39c8ad972a757".to_string())
/// ```
#[derive(Debug, Clone)]
pub enum CryptographicHash {
/// Sha1 hasher
Sha1(Sha1),
/// Sha256 hasher
Sha256(Sha256),
/// Sha512 hasher
Sha512(Sha512),
}
impl CryptographicHash {
/// Create a new hasher
pub fn new(algo: Algorithm) -> Self {
match algo {
// new Sha1 hasher
Algorithm::SHA1 => Self::Sha1(Sha1::new()),
// new Sha256 hasher
Algorithm::SHA256 => Self::Sha256(Sha256::new()),
// new Sha512 hasher
Algorithm::SHA512 => Self::Sha512(Sha512::new()),
}
}
/// Set value in hasher
pub fn update(&mut self, input: &[u8]) {
match self {
// Sha1
Self::Sha1(sha1) => sha1.update(input),
// Sha256
Self::Sha256(sha256) => sha256.update(input),
// Sha512
Self::Sha512(sha512) => sha512.update(input),
}
}
/// Compute hash
pub fn finalize(&mut self) -> Vec<u8> {
match self {
// Sha1
Self::Sha1(sha1) => sha1.finalize_reset().to_vec(),
// Sha256
Self::Sha256(sha256) => sha256.finalize_reset().to_vec(),
// Sha512
Self::Sha512(sha512) => sha512.finalize_reset().to_vec(),
}
}
/// Compute hash using a single function
/// ```
/// use crypto_utils::sha::{Algorithm, CryptographicHash};
///
/// // compute hash
/// let mut hash_bytes = CryptographicHash::hash(Algorithm::SHA1, b"P@ssw0rd");
///
/// // decode hash to a String
/// let hash = hex::encode(hash_bytes);
///
/// assert_eq!(hash, "21bd12dc183f740ee76f27b78eb39c8ad972a757".to_string())
/// ```
pub fn hash(algo: Algorithm, input: &[u8]) -> Vec<u8> {
// create hasher
let mut hasher = Self::new(algo);
// set value in hasher
hasher.update(input);
// compute hash
hasher.finalize()
}
}
#[cfg(test)]
mod tests {
use super::{Algorithm, CryptographicHash};
/// Test a Sha1 hasher
#[test]
fn sha1() {
// expected hash
let expected_hash = "7726bd9560e1ad4a1a4f056cae5c0c9ea8bacfc2".to_string();
// compute hash
let hash_bytes = CryptographicHash::hash(Algorithm::SHA1, b"test sha1 hash");
// decode hash to a String
let hash = hex::encode(hash_bytes);
// validate hash
assert_eq!(hash, expected_hash)
}
/// Test a Sha256 hasher
#[test]
fn sha256() {
// expected hash
let expected_hash =
"eaf6e4198f39ccd63bc3e957d43bf4ef67f12c318c8e3cdc2567a37339902dac".to_string();
// compute hash
let hash_bytes = CryptographicHash::hash(Algorithm::SHA256, b"test sha256 hash");
// decode hash to a String
let hash = hex::encode(hash_bytes);
// validate hash
assert_eq!(hash, expected_hash)
}
/// Test a Sha512 hasher
#[test]
fn sha512() {
// expected hash
let expected_hash =
"b43b4d7178014c92f55be828d66c9f98211fc67b385f7790a5b4b2fcb89fe1831645b5a4c17f3f7f11d8f34d2800a77a2b8faa5a0fb9d6b8f7befbc29a9ce795".to_string();
// compute hash
let hash_bytes = CryptographicHash::hash(Algorithm::SHA512, b"test sha512 hash");
// decode hash to a String
let hash = hex::encode(hash_bytes);
// validate hash
assert_eq!(hash, expected_hash)
}
}