diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 865a291..e396ca4 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,6 +1,6 @@ { "git": { - "sha1": "4184f8ef58a6f148f1dda5c83dcf2a98962f541e" + "sha1": "52a913197072c480a3d8d244ffa427ea6ca815c0" }, "path_in_vcs": "" } \ No newline at end of file diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 62c0014..e0be95b 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -28,7 +28,7 @@ jobs: override: true components: clippy, rustfmt - name: Check format - run: cargo fmt --all -- --check + run: cargo fmt --all - name: Check fix run: cargo fix && cargo fix - name: Check with clippy @@ -48,6 +48,7 @@ jobs: windows-vcpkg: name: windows-vcpkg runs-on: windows-latest + needs: [ build ] steps: - uses: actions/checkout@v3 - uses: sfackler/actions/rustup@master @@ -61,7 +62,7 @@ jobs: override: true components: clippy, rustfmt - name: Check format - run: cargo fmt --all -- --check + run: cargo fmt --all - name: Check fix run: cargo fix && cargo fix - name: Check with clippy @@ -77,3 +78,15 @@ jobs: - name: Build on nightly run: cargo build --release + publish-crate: + if: startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-latest + needs: [ windows-vcpkg ] + steps: + - name: Set up Rust + uses: hecrj/setup-rust-action@v2 + - uses: actions/checkout@v4 + - name: Publish + shell: bash + run: | + cargo publish --token ${{ secrets.CRATES_GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml deleted file mode 100644 index b9e3741..0000000 --- a/.github/workflows/publish.yml +++ /dev/null @@ -1,24 +0,0 @@ -name: Deploy -on: - push: - tags: - - "*" - -jobs: - # Publish starship to Crates.io - cargo_publish: - name: Publish Cargo Package - runs-on: ubuntu-latest - steps: - - name: Setup | Checkout - uses: actions/checkout@v2 - - - name: Setup | Rust - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - profile: minimal - override: true - - - name: Build | Publish - run: cargo publish --token ${{ secrets.CRATES_GITHUB_TOKEN }} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..a12534b --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,571 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + +[[package]] +name = "async-stream" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b5a71a6f37880a80d1d7f19efd781e4b5de42c88f0722cc13bcb6cc2cfe8476" +dependencies = [ + "async-stream-impl", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-stream-impl" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "async-trait" +version = "0.1.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "644dd749086bf3771a2fbc5f256fdb982d53f011c7d5d560304eafeecebce79d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets", +] + +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bytes" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9" + +[[package]] +name = "cc" +version = "1.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be714c154be609ec7f5dad223a33bf1482fff90472de28f7362806e6d4832b8c" +dependencies = [ + "shlex", +] + +[[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.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + +[[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.170" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "miniz_oxide" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e3e04debbb59698c15bacbb6d93584a8c0ca9cc3213cb423d31f760d8843ce5" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + +[[package]] +name = "object" +version = "0.36.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" + +[[package]] +name = "openssl" +version = "0.10.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e14130c6a98cd258fdcb0fb6d744152343ff729cbfcb28c656a9d12b999fbcd" +dependencies = [ + "bitflags", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "openssl-sys" +version = "0.9.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bb61ea9811cc39e3c2069f40b8b8e2e70d8569b361f879786cc7ed48b777cdd" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "pkg-config" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" + +[[package]] +name = "proc-macro2" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b568323e98e49e2a0899dcee453dd679fae22d69adf9b11dd508d1549b7e2f" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha256" +version = "1.6.0" +dependencies = [ + "async-trait", + "bytes", + "hex", + "openssl", + "sha2", + "tokio", + "tokio-test", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + +[[package]] +name = "smallvec" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" + +[[package]] +name = "socket2" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "syn" +version = "2.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tokio" +version = "1.43.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-stream" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eca58d7bba4a75707817a2c44174253f9236b2d5fbd055602e9d5c07c139a047" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-test" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2468baabc3311435b55dd935f702f42cd1b8abb7e754fb7dfb16bd36aa88f9f7" +dependencies = [ + "async-stream", + "bytes", + "futures-core", + "tokio", + "tokio-stream", +] + +[[package]] +name = "typenum" +version = "1.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f" + +[[package]] +name = "unicode-ident" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index e6f1ea4..78d0c90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,8 +12,14 @@ [package] edition = "2018" name = "sha256" -version = "1.5.0" +version = "1.6.0" authors = ["baoyachi "] +build = false +autolib = false +autobins = false +autoexamples = false +autotests = false +autobenches = false description = "sha256 crypto digest" readme = "README.md" keywords = [ @@ -25,6 +31,15 @@ categories = ["cryptography"] license = "MIT OR Apache-2.0" repository = "https://github.com/baoyachi/sha256-rs" +[features] +async = ["tokio"] +default = ["async"] +native_openssl = ["openssl"] + +[lib] +name = "sha256" +path = "src/lib.rs" + [dependencies.async-trait] version = "0.1.68" @@ -57,8 +72,3 @@ features = ["full"] [dev-dependencies.tokio-test] version = "0.4.2" - -[features] -async = ["tokio"] -default = ["async"] -native_openssl = ["openssl"] diff --git a/Cargo.toml.orig b/Cargo.toml.orig index d8f4b9a..85c9153 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "sha256" -version = "1.5.0" +version = "1.6.0" authors = ["baoyachi "] edition = "2018" description = "sha256 crypto digest" diff --git a/README.md b/README.md index e338789..16a4243 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ fn main() { assert_eq!(val,"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"); //sha256 digest char - let mut input = "π"; + let mut input = 'π'; let val = digest(input); assert_eq!(val,"2617fcb92baa83a96341de050f07a3186657090881eae6b833f66a035600f35a"); diff --git a/src/async_digest.rs b/src/async_digest.rs new file mode 100644 index 0000000..9b2ee13 --- /dev/null +++ b/src/async_digest.rs @@ -0,0 +1,74 @@ +use crate::{CalculatorSelector, TrySha256Digest}; +use bytes::BytesMut; +use std::io; + +/// sha256 digest file +/// +/// # Examples +/// +/// ```rust +/// use sha256::{try_async_digest}; +/// use std::path::Path; +/// let input = Path::new("./foo.file"); +/// tokio_test::block_on(async{ +/// let val = try_async_digest(input).await.unwrap(); +/// assert_eq!(val,"433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1") +/// }); +/// +/// ``` +pub async fn try_async_digest(input: D) -> Result { + input.async_digest().await +} + +/// sha256 digest file +/// +/// # Examples +/// +/// ```rust +/// use sha256::{try_async_openssl_digest}; +/// use std::path::Path; +/// let input = Path::new("./foo.file"); +/// tokio_test::block_on(async{ +/// let val = try_async_openssl_digest(input).await.unwrap(); +/// assert_eq!(val,"433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1") +/// }); +/// ``` +#[cfg(feature = "native_openssl")] +pub async fn try_async_openssl_digest(input: D) -> Result { + input.async_openssl_digest().await +} + +#[async_trait::async_trait] +pub trait AsyncCalculatorInput { + async fn read_inner(&mut self, buf: &mut BytesMut) -> io::Result; +} + +pub async fn async_calc(mut input: I, mut selector: S) -> io::Result +where + I: AsyncCalculatorInput, + S: CalculatorSelector, +{ + let mut buf = BytesMut::with_capacity(1024); + loop { + buf.clear(); + let len = input.read_inner(&mut buf).await?; + if len == 0 { + break; + } + selector.update_inner(&buf[0..len]); + } + let hash = selector.finish_inner(); + Ok(hex::encode(hash)) +} + +#[async_trait::async_trait] +impl AsyncCalculatorInput for tokio::io::BufReader +where + R: tokio::io::AsyncRead + Unpin + Send, +{ + async fn read_inner(&mut self, buf: &mut BytesMut) -> io::Result { + use tokio::io::AsyncReadExt; + + self.read_buf(buf).await + } +} diff --git a/src/lib.rs b/src/lib.rs index 94ec155..32a2e81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,7 @@ //! assert_eq!(val,"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"); //! //! //sha256 digest char -//! let mut input = "π"; +//! let mut input = 'π'; //! let val = digest(input); //! assert_eq!(val,"2617fcb92baa83a96341de050f07a3186657090881eae6b833f66a035600f35a"); //! @@ -37,11 +37,17 @@ //! //! ``` +#[cfg(feature = "async")] +pub mod async_digest; +#[cfg(feature = "native_openssl")] +mod openssl_sha256; + #[cfg(feature = "native_openssl")] use crate::openssl_sha256::OpenSslSha256; #[cfg(feature = "async")] pub use async_digest::*; + use sha2::digest::Output; use sha2::{Digest, Sha256}; use std::fmt::Debug; @@ -50,6 +56,9 @@ use std::io; use std::io::{BufReader, Read}; use std::path::Path; +#[cfg(test)] +mod tests; + /// sha256 digest string /// /// # Examples @@ -266,248 +275,3 @@ where let hash = selector.finish_inner(); Ok(hex::encode(hash)) } - -#[cfg(feature = "async")] -pub mod async_digest { - use crate::{CalculatorSelector, TrySha256Digest}; - use bytes::BytesMut; - use std::io; - - /// sha256 digest file - /// - /// # Examples - /// - /// ```rust - /// use sha256::{try_async_digest}; - /// use std::path::Path; - /// let input = Path::new("./foo.file"); - /// tokio_test::block_on(async{ - /// let val = try_async_digest(input).await.unwrap(); - /// assert_eq!(val,"433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1") - /// }); - /// - /// ``` - pub async fn try_async_digest(input: D) -> Result { - input.async_digest().await - } - - /// sha256 digest file - /// - /// # Examples - /// - /// ```rust - /// use sha256::{try_async_openssl_digest}; - /// use std::path::Path; - /// let input = Path::new("./foo.file"); - /// tokio_test::block_on(async{ - /// let val = try_async_openssl_digest(input).await.unwrap(); - /// assert_eq!(val,"433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1") - /// }); - /// ``` - #[cfg(feature = "native_openssl")] - pub async fn try_async_openssl_digest( - input: D, - ) -> Result { - input.async_openssl_digest().await - } - - #[async_trait::async_trait] - pub trait AsyncCalculatorInput { - async fn read_inner(&mut self, buf: &mut BytesMut) -> io::Result; - } - - pub async fn async_calc(mut input: I, mut selector: S) -> io::Result - where - I: AsyncCalculatorInput, - S: CalculatorSelector, - { - let mut buf = BytesMut::with_capacity(1024); - loop { - buf.clear(); - let len = input.read_inner(&mut buf).await?; - if len == 0 { - break; - } - selector.update_inner(&buf[0..len]); - } - let hash = selector.finish_inner(); - Ok(hex::encode(hash)) - } - - #[async_trait::async_trait] - impl AsyncCalculatorInput for tokio::io::BufReader - where - R: tokio::io::AsyncRead + Unpin + Send, - { - async fn read_inner(&mut self, buf: &mut BytesMut) -> io::Result { - use tokio::io::AsyncReadExt; - - self.read_buf(buf).await - } - } -} - -#[cfg(feature = "native_openssl")] -mod openssl_sha256 { - use crate::CalculatorSelector; - - pub type OpenSslSha256 = openssl::sha::Sha256; - - impl CalculatorSelector for OpenSslSha256 { - type FinishType = [u8; 32]; - - fn update_inner(&mut self, data: &[u8]) { - self.update(data) - } - - fn finish_inner(self) -> Self::FinishType { - self.finish() - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[cfg(feature = "native_openssl")] - #[test] - fn test_openssl() { - let input = Path::new("./foo.file"); - let f = fs::File::open(input).unwrap(); - let reader = BufReader::new(f); - let sha = openssl::sha::Sha256::new(); - assert_eq!( - "433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1", - calc(reader, sha).unwrap() - ); - } - - #[test] - fn test_sha256() { - let input = Path::new("./foo.file"); - let f = fs::File::open(input).unwrap(); - let reader = BufReader::new(f); - let sha = Sha256::new(); - assert_eq!( - "433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1", - calc(reader, sha).unwrap() - ); - } - - #[cfg(all(feature = "async", feature = "native_openssl"))] - #[tokio::test] - async fn test_async_openssl() { - let input = Path::new("./foo.file"); - let f = tokio::fs::File::open(input).await.unwrap(); - let reader = tokio::io::BufReader::new(f); - let sha = openssl::sha::Sha256::new(); - assert_eq!( - "433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1", - async_calc(reader, sha).await.unwrap() - ); - } - - #[cfg(feature = "async")] - #[tokio::test] - async fn test_async() { - let input = Path::new("./foo.file"); - let f = tokio::fs::File::open(input).await.unwrap(); - let reader = tokio::io::BufReader::new(f); - let sha = Sha256::new(); - assert_eq!( - "433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1", - async_calc(reader, sha).await.unwrap() - ); - } - - #[cfg(all(feature = "async", feature = "native_openssl"))] - #[tokio::test] - async fn test_try_async_openssl_digest() { - let hash = try_async_openssl_digest("./foo.file").await.unwrap(); - assert_eq!( - "433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1", - hash - ); - } - - #[cfg(feature = "async")] - #[tokio::test] - async fn test_try_async_digest() { - let hash = try_async_digest("./foo.file").await.unwrap(); - assert_eq!( - "433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1", - hash - ); - } - - #[cfg(feature = "async")] - #[tokio::test] - async fn test_async_parity() { - let bytes = (0..0x1000).map(|v| (v % 256) as u8).collect::>(); - let val = digest(&bytes); - - let async_res = { - let bytes = &bytes; - // We want to force Poll::Pending on reads during async_calc, which may break parity - // between sync and async hashing. - let (client, mut server) = tokio::io::duplex(64); - let reader = tokio::io::BufReader::new(client); - let sha = Sha256::new(); - - use tokio::io::AsyncWriteExt; - tokio::join! { - async_calc(reader, sha), - async move { - server.write_all(&bytes[..]).await.unwrap(); - core::mem::drop(server); - } - } - .0 - .unwrap() - }; - - let sync_res = { - let reader = BufReader::new(&*bytes); - let sha = Sha256::new(); - calc(reader, sha).unwrap() - }; - assert_eq!(val, async_res); - assert_eq!(async_res, sync_res); - } - - #[cfg(all(feature = "async", feature = "native_openssl"))] - #[tokio::test] - async fn test_async_parity_openssl() { - let bytes = (0..0x1000).map(|v| (v % 256) as u8).collect::>(); - let val = digest(&bytes); - - let async_res = { - let bytes = &bytes; - // We want to force Poll::Pending on reads during async_calc, which may break parity - // between sync and async hashing. - let (client, mut server) = tokio::io::duplex(64); - let reader = tokio::io::BufReader::new(client); - let sha = OpenSslSha256::new(); - - use tokio::io::AsyncWriteExt; - tokio::join! { - async_calc(reader, sha), - async move { - server.write_all(&bytes[..]).await.unwrap(); - core::mem::drop(server); - } - } - .0 - .unwrap() - }; - - let sync_res = { - let reader = BufReader::new(&*bytes); - let sha = OpenSslSha256::new(); - calc(reader, sha).unwrap() - }; - assert_eq!(val, async_res); - assert_eq!(async_res, sync_res); - } -} diff --git a/src/openssl_sha256.rs b/src/openssl_sha256.rs new file mode 100644 index 0000000..739d0ed --- /dev/null +++ b/src/openssl_sha256.rs @@ -0,0 +1,15 @@ +use crate::CalculatorSelector; + +pub type OpenSslSha256 = openssl::sha::Sha256; + +impl CalculatorSelector for OpenSslSha256 { + type FinishType = [u8; 32]; + + fn update_inner(&mut self, data: &[u8]) { + self.update(data) + } + + fn finish_inner(self) -> Self::FinishType { + self.finish() + } +} diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..43804a9 --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,142 @@ +use super::*; + +#[cfg(feature = "native_openssl")] +#[test] +fn test_openssl() { + let input = Path::new("./foo.file"); + let f = fs::File::open(input).unwrap(); + let reader = BufReader::new(f); + let sha = openssl::sha::Sha256::new(); + assert_eq!( + "433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1", + calc(reader, sha).unwrap() + ); +} + +#[test] +fn test_sha256() { + let input = Path::new("./foo.file"); + let f = fs::File::open(input).unwrap(); + let reader = BufReader::new(f); + let sha = Sha256::new(); + assert_eq!( + "433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1", + calc(reader, sha).unwrap() + ); +} + +#[cfg(all(feature = "async", feature = "native_openssl"))] +#[tokio::test] +async fn test_async_openssl() { + let input = Path::new("./foo.file"); + let f = tokio::fs::File::open(input).await.unwrap(); + let reader = tokio::io::BufReader::new(f); + let sha = openssl::sha::Sha256::new(); + assert_eq!( + "433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1", + async_calc(reader, sha).await.unwrap() + ); +} + +#[cfg(feature = "async")] +#[tokio::test] +async fn test_async() { + let input = Path::new("./foo.file"); + let f = tokio::fs::File::open(input).await.unwrap(); + let reader = tokio::io::BufReader::new(f); + let sha = Sha256::new(); + assert_eq!( + "433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1", + async_calc(reader, sha).await.unwrap() + ); +} + +#[cfg(all(feature = "async", feature = "native_openssl"))] +#[tokio::test] +async fn test_try_async_openssl_digest() { + let hash = try_async_openssl_digest("./foo.file").await.unwrap(); + assert_eq!( + "433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1", + hash + ); +} + +#[cfg(feature = "async")] +#[tokio::test] +async fn test_try_async_digest() { + let hash = try_async_digest("./foo.file").await.unwrap(); + assert_eq!( + "433855b7d2b96c23a6f60e70c655eb4305e8806b682a9596a200642f947259b1", + hash + ); +} + +#[cfg(feature = "async")] +#[tokio::test] +async fn test_async_parity() { + let bytes = (0..0x1000).map(|v| (v % 256) as u8).collect::>(); + let val = digest(&bytes); + + let async_res = { + let bytes = &bytes; + // We want to force Poll::Pending on reads during async_calc, which may break parity + // between sync and async hashing. + let (client, mut server) = tokio::io::duplex(64); + let reader = tokio::io::BufReader::new(client); + let sha = Sha256::new(); + + use tokio::io::AsyncWriteExt; + tokio::join! { + async_calc(reader, sha), + async move { + server.write_all(&bytes[..]).await.unwrap(); + core::mem::drop(server); + } + } + .0 + .unwrap() + }; + + let sync_res = { + let reader = BufReader::new(&*bytes); + let sha = Sha256::new(); + calc(reader, sha).unwrap() + }; + assert_eq!(val, async_res); + assert_eq!(async_res, sync_res); +} + +#[cfg(all(feature = "async", feature = "native_openssl"))] +#[tokio::test] +async fn test_async_parity_openssl() { + let bytes = (0..0x1000).map(|v| (v % 256) as u8).collect::>(); + let val = digest(&bytes); + + let async_res = { + let bytes = &bytes; + // We want to force Poll::Pending on reads during async_calc, which may break parity + // between sync and async hashing. + let (client, mut server) = tokio::io::duplex(64); + let reader = tokio::io::BufReader::new(client); + let sha = OpenSslSha256::new(); + + use tokio::io::AsyncWriteExt; + tokio::join! { + async_calc(reader, sha), + async move { + server.write_all(&bytes[..]).await.unwrap(); + core::mem::drop(server); + } + } + .0 + .unwrap() + }; + + let sync_res = { + let reader = BufReader::new(&*bytes); + let sha = OpenSslSha256::new(); + calc(reader, sha).unwrap() + }; + assert_eq!(val, async_res); + assert_eq!(async_res, sync_res); +}