diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml deleted file mode 100644 index 516d3f2..0000000 --- a/.github/workflows/rust.yml +++ /dev/null @@ -1,151 +0,0 @@ -name: Rust - -on: - push: - branches: [ "master" ] - pull_request: - branches: [ "master" ] - -env: - CARGO_TERM_COLOR: always - -jobs: - build: - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest, windows-latest] - rust: [nightly, stable, '1.60'] - runs-on: ${{ matrix.os }} - continue-on-error: ${{ matrix.rust == 'nightly'}} - - steps: - - uses: actions/checkout@v3 - - - name: Restore Cargo cache - uses: actions/cache@v2 - env: - cache-name: rust - with: - path: | - ~/.cargo/registry - ~/.cargo/git - ~/.cargo/bin - target - key: ${{ matrix.os }}-${{ env.cache-name }}-${{ matrix.rust }}-${{ hashFiles('Cargo.lock')}} - - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: ${{ matrix.rust }} - default: true - profile: minimal - components: clippy - - - name: Build Debug - run: | - cargo build - - - name: Run Tests - run: | - cargo test - - - name: Run Clippy - run: | - cargo clippy - - - name: Build the Release - run: | - cargo build --release - - - name: Binary Size (Unix) - if: matrix.os != 'windows-latest' - run: | - ls -l ./target/release/rustgenhash - - - name: Binary Size (Windows) - if: matrix.os == 'windows-latest' - run: | - ls -l ./target/release/rustgenhash.exe - - - name: Binary dependencies (MacOS) - if: matrix.os == 'macos-latest' - run: | - otool -L ./target/release/rustgenhash - - - name: Build the MSI install package (Windows) - if: matrix.os == 'windows-latest' - run: | - cargo install cargo-wix --version 0.3.3 - cargo wix --version - cargo wix -p rustgenhash --no-build --nocapture --output ./target/wix/rustgenhash.msi - ls -l ./target/wix/rustgenhash.msi - - build-linux-musl: - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - rust: [nightly, stable, '1.60'] - continue-on-error: ${{ matrix.rust == 'nightly' }} - steps: - - uses: actions/checkout@master - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: ${{ matrix.rust }} - profile: minimal - default: true - target: x86_64-unknown-linux-musl - - - name: Setup MUSL - run: | - sudo apt-get -qq install musl-tools - - name: Build Debug - run: | - cargo build - ./target/x86_64-unknown-linux-musl/debug/rustgenhash --version - - name: Build Release - run: | - cargo build --release - ./target/x86_64-unknown-linux-musl/release/rustgenhash --version - ls -l ./target/x86_64-unknown-linux-musl/release/gitui - - name: Test - run: | - cargo test - - linting: - name: Lints - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@master - - name: Install Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - components: rustfmt - - - run: cargo fmt -- --check - - - name: cargo-sort - run: | - cargo install cargo-sort --force - cargo sort -c -w - - name: cargo-deny install - run: | - cargo install --locked cargo-deny - - name: cargo-deny licenses - run: | - cargo deny check licenses - - name: cargo-deny bans - run: | - cargo deny check bans - - sec: - name: Security audit - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions-rs/audit-check@v1 - with: - token: ${{ secrets.GITHUB_TOKEN }} diff --git a/Cargo.lock b/Cargo.lock index 8a2f9c6..ade198a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -473,7 +473,7 @@ dependencies = [ [[package]] name = "rustgenhash" -version = "0.5.13" +version = "0.5.14" dependencies = [ "argon2", "balloon-hash", diff --git a/Cargo.toml b/Cargo.toml index 5b25ddf..aa1348c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustgenhash" -version = "0.5.13" +version = "0.5.14" license = "MIT" authors = ["Volker Schwaberow "] description = "A tool to generate hashes from the command line." diff --git a/src/app.rs b/src/app.rs index b7b912e..ac56817 100644 --- a/src/app.rs +++ b/src/app.rs @@ -82,6 +82,26 @@ pub enum Algorithm { Whirlpool, } +struct AlgorithmProperties { + file_support: bool, +} + +impl Algorithm { + fn properties(&self) -> AlgorithmProperties { + match *self { + Algorithm::Argon2 => AlgorithmProperties { file_support: false }, + Algorithm::Pbkdf2Sha256 | Algorithm::Pbkdf2Sha512 => { + AlgorithmProperties { file_support: false } + }, + Algorithm::Scrypt => AlgorithmProperties { file_support: false }, + Algorithm::Shacrypt => AlgorithmProperties { file_support: false }, + Algorithm::Bcrypt => AlgorithmProperties { file_support: false }, + Algorithm::Balloon => AlgorithmProperties { file_support: false }, + _ => AlgorithmProperties { file_support: true } + } + } +} + fn hash_string( algor: Algorithm, password: &str, @@ -135,26 +155,12 @@ fn hash_string( } fn hash_file(alg: Algorithm, input: &str, option: OutputOptions) { - use Algorithm as algo; - match alg { - algo::Argon2 - | algo::Balloon - | algo::Bcrypt - | algo::Pbkdf2Sha256 - | algo::Pbkdf2Sha512 - | algo::Scrypt - | algo::Shacrypt => { - println!( - "{:?} is not supported for files", - format!("{:?}", alg).to_lowercase().as_str() - ); - std::process::exit(1); - } - _ => { - let alg_s = format!("{:?}", alg).to_uppercase(); - RHash::new(&alg_s).process_file(input, option); - } + if !alg.properties().file_support { + println!("Algorithm {:?} does not support file hashing", alg); + std::process::exit(1); } + let alg_s = format!("{:?}", alg).to_uppercase(); + RHash::new(&alg_s).process_file(input, option); } fn build_cli() -> clap::Command { diff --git a/src/hash.rs b/src/hash.rs index b679f60..5d79f97 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -34,7 +34,7 @@ use pbkdf2::{ password_hash::{Ident as PbIdent, SaltString as PbSaltString}, Pbkdf2, }; -use std::io::Read; +use std::{io::Read, collections::HashMap}; use scrypt::{password_hash::SaltString as ScSaltString, Scrypt}; @@ -95,12 +95,12 @@ impl PHash { } pub fn hash_pbkdf2(password: &str, pb_scheme: &str) { - let pb_s = match pb_scheme { - "pbkdf2sha256" => "pbkdf2-sha256", - "pbkdf2sha512" => "pbkdf2-sha512", - _ => "NONE", - }; + let pb_scheme_hmap: HashMap<&str, &str> = [ + ("pbkdf2sha256", "pbkdf2-sha256"), + ("pbkdf2sha512", "pbkdf2-sha512") + ].iter().cloned().collect(); + let pb_s = pb_scheme_hmap.get(pb_scheme).unwrap_or(&"NONE"); let algorithm = PbIdent::new(pb_s).unwrap(); let salt = PbSaltString::generate(&mut OsRng); let params = pbkdf2::Params { @@ -115,11 +115,15 @@ impl PHash { params, salt.as_salt(), ) - .unwrap() - .to_string(); + .unwrap_or_else(|_| { + eprintln!("Error: {}", "Could not hash PBKDF2 password"); + std::process::exit(1); + }); println!("{} {}", password_hash, password); + } + pub fn hash_scrypt(password: &str) { let salt = ScSaltString::generate(&mut OsRng); let password_hash = Scrypt