md5-0.8.0/.cargo_vcs_info.json0000644000000001360000000000100115260ustar { "git": { "sha1": "f36c8ac842d4d828298d28330e507477e0836131" }, "path_in_vcs": "" }md5-0.8.0/.github/workflows/build.yml000064400000000000000000000015601046102023000155370ustar 00000000000000name: build on: push: branches: - main pull_request: branches: - main workflow_dispatch: jobs: check: runs-on: macos-latest steps: - uses: actions/checkout@v4 - run: rustup toolchain install stable --profile=minimal --component clippy --component rustfmt - run: cargo clippy -- -D warnings - run: cargo fmt --all -- --check bench: strategy: matrix: os: [macos-latest, ubuntu-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - run: rustup toolchain install nightly --profile=minimal - run: cargo +nightly bench test: strategy: matrix: os: [macos-latest, ubuntu-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - run: rustup toolchain install stable --profile=minimal - run: cargo test md5-0.8.0/.gitignore000064400000000000000000000000241046102023000123020ustar 00000000000000/Cargo.lock /target md5-0.8.0/Cargo.lock0000644000000002230000000000100074760ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "md5" version = "0.8.0" md5-0.8.0/Cargo.toml0000644000000030020000000000100075170ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "md5" version = "0.8.0" authors = [ "Ivan Ukhov ", "Kamal Ahmad ", "Konstantin Stepanov ", "Lukas Kalbertodt ", "Nathan Musoke ", "Scott Mabin ", "Tony Arcieri ", "Wim de With ", "Yosef Dinerstein ", ] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "The package provides the MD5 hash function." homepage = "https://github.com/stainless-steel/md5" documentation = "https://docs.rs/md5" readme = "README.md" keywords = [ "checksum", "digest", "hash", "md5", ] categories = [ "algorithms", "cryptography", ] license = "Apache-2.0/MIT" repository = "https://github.com/stainless-steel/md5" [features] default = ["std"] std = [] [lib] name = "md5" path = "src/lib.rs" [[bench]] name = "lib" path = "benches/lib.rs" md5-0.8.0/Cargo.toml.orig000064400000000000000000000015171046102023000132110ustar 00000000000000[package] name = "md5" version = "0.8.0" edition = "2021" license = "Apache-2.0/MIT" authors = [ "Ivan Ukhov ", "Kamal Ahmad ", "Konstantin Stepanov ", "Lukas Kalbertodt ", "Nathan Musoke ", "Scott Mabin ", "Tony Arcieri ", "Wim de With ", "Yosef Dinerstein ", ] description = "The package provides the MD5 hash function." documentation = "https://docs.rs/md5" homepage = "https://github.com/stainless-steel/md5" repository = "https://github.com/stainless-steel/md5" readme = "README.md" categories = ["algorithms", "cryptography"] keywords = ["checksum", "digest", "hash", "md5"] [features] default = ["std"] std = [] md5-0.8.0/LICENSE.md000064400000000000000000000037611046102023000117310ustar 00000000000000# License The project is dual licensed under the terms of the Apache License, Version 2.0, and the MIT License. You may obtain copies of the two licenses at * https://www.apache.org/licenses/LICENSE-2.0 and * https://opensource.org/licenses/MIT, respectively. The following two notices apply to every file of the project. ## The Apache License ``` Copyright 2015–2025 The md5 Developers Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ``` ## The MIT License ``` Copyright 2015–2025 The md5 Developers 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. ``` md5-0.8.0/README.md000064400000000000000000000031151046102023000115750ustar 00000000000000# MD5 [![Package][package-img]][package-url] [![Documentation][documentation-img]][documentation-url] [![Build][build-img]][build-url] The package provides the [MD5] hash function. ## Example ```rust let digest = md5::compute(b"abcdefghijklmnopqrstuvwxyz"); assert_eq!(format!("{:x}", digest), "c3fcd3d76192e4007dfb496cca67e13b"); ``` ## Security Warning The package is provided for the purposes of interoperability with protocols and systems that mandate the use of MD5. However, MD5 should be considered [cryptographically broken and unsuitable for further use][VU836068]. Collision attacks against MD5 are both practical and trivial, and [theoretical attacks against MD5 have been found][ACM1724151]. [RFC6151] advises no new protocols to be designed with any MD5-based constructions, including HMAC-MD5. ## Contribution Your contribution is highly appreciated. Do not hesitate to open an issue or a pull request. Note that any contribution submitted for inclusion in the project will be licensed according to the terms given in [LICENSE.md](LICENSE.md). [ACM1724151]: https://dl.acm.org/citation.cfm?id=1724151 [MD5]: https://en.wikipedia.org/wiki/MD5 [RFC6151]: https://tools.ietf.org/html/rfc6151 [VU836068]: https://www.kb.cert.org/vuls/id/836068 [build-img]: https://github.com/stainless-steel/md5/workflows/build/badge.svg [build-url]: https://github.com/stainless-steel/md5/actions/workflows/build.yml [documentation-img]: https://docs.rs/md5/badge.svg [documentation-url]: https://docs.rs/md5 [package-img]: https://img.shields.io/crates/v/md5.svg [package-url]: https://crates.io/crates/md5 md5-0.8.0/benches/lib.rs000064400000000000000000000020001046102023000130310ustar 00000000000000#![feature(test)] extern crate test; use md5::Context; macro_rules! implement( ($($size:literal => ($compute:ident, $context:ident),)*) => ($( #[bench] fn $compute(bencher: &mut test::Bencher) { compute($size, bencher); } #[bench] fn $context(bencher: &mut test::Bencher) { context($size, bencher); } )*); ); implement! { 1000 => (compute_0001000, context_0001000), 10000 => (compute_0010000, context_0010000), 100000 => (compute_0100000, context_0100000), 1000000 => (compute_1000000, context_1000000), } fn compute(size: usize, bencher: &mut test::Bencher) { let data = vec![0xffu8; size]; bencher.iter(|| { test::black_box(md5::compute(&data)); }); } fn context(size: usize, bencher: &mut test::Bencher) { let data = vec![0xffu8; size]; bencher.iter(|| { let mut context = Context::new(); context.consume(&data); test::black_box(context.finalize()); }); } md5-0.8.0/src/lib.rs000064400000000000000000000332531046102023000122270ustar 00000000000000//! The [MD5] hash function. //! //! ## Example //! //! ``` //! let digest = md5::compute(b"abcdefghijklmnopqrstuvwxyz"); //! assert_eq!(format!("{:x}", digest), "c3fcd3d76192e4007dfb496cca67e13b"); //! ``` //! //! ## Security Warning //! //! The package is provided for the purposes of interoperability with protocols //! and systems that mandate the use of MD5. However, MD5 should be considered //! [cryptographically broken and unsuitable for further use][VU836068]. //! Collision attacks against MD5 are both practical and trivial, and //! [theoretical attacks against MD5 have been found][ACM1724151]. //! //! [RFC6151] advises no new protocols to be designed with any MD5-based //! constructions, including HMAC-MD5. //! //! [MD5]: https://en.wikipedia.org/wiki/MD5 //! //! [ACM1724151]: https://dl.acm.org/citation.cfm?id=1724151 //! [RFC6151]: https://tools.ietf.org/html/rfc6151 //! [VU836068]: https://www.kb.cert.org/vuls/id/836068 // The implementation is based on: // https://www.ietf.org/rfc/rfc1321.txt #![cfg_attr(not(feature = "std"), no_std)] #[cfg(feature = "std")] use std as core; /// A digest. #[derive(Clone, Copy, Eq, Hash, PartialEq)] pub struct Digest(pub [u8; 16]); impl core::convert::From for [u8; 16] { #[inline] fn from(digest: Digest) -> Self { digest.0 } } impl core::fmt::Debug for Digest { #[inline] fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { core::fmt::LowerHex::fmt(self, formatter) } } impl core::ops::Deref for Digest { type Target = [u8; 16]; #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl core::ops::DerefMut for Digest { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } macro_rules! implement { ($kind:ident, $format:expr) => { impl core::fmt::$kind for Digest { fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { for value in &self.0 { write!(formatter, $format, value)?; } Ok(()) } } }; } implement!(LowerHex, "{:02x}"); implement!(UpperHex, "{:02X}"); /// A context. #[derive(Clone)] pub struct Context { buffer: [u8; 64], count: u64, state: [u32; 4], } const PADDING: [u8; 64] = [ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]; impl Context { /// Create a context for computing a digest. #[inline] pub fn new() -> Context { Context { buffer: [0; 64], count: 0, state: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476], } } /// Consume data. #[inline] pub fn consume>(&mut self, data: T) { consume(self, data.as_ref()); } /// Finalize and return the digest. #[rustfmt::skip] #[allow(clippy::double_parens, clippy::needless_range_loop)] pub fn finalize(mut self) -> Digest { let mut input = [0u32; 16]; let k = ((self.count >> 3) & 0x3f) as usize; input[14] = self.count as u32; input[15] = (self.count >> 32) as u32; consume( &mut self, &PADDING[..(if k < 56 { 56 - k } else { 120 - k })], ); let mut j = 0; for i in 0..14 { input[i] = ((self.buffer[j + 3] as u32) << 24) | ((self.buffer[j + 2] as u32) << 16) | ((self.buffer[j + 1] as u32) << 8) | ((self.buffer[j ] as u32) ); j += 4; } transform(&mut self.state, &input); let mut digest = [0u8; 16]; let mut j = 0; for i in 0..4 { digest[j ] = ((self.state[i] ) & 0xff) as u8; digest[j + 1] = ((self.state[i] >> 8) & 0xff) as u8; digest[j + 2] = ((self.state[i] >> 16) & 0xff) as u8; digest[j + 3] = ((self.state[i] >> 24) & 0xff) as u8; j += 4; } Digest(digest) } /// Finalize and return the digest. #[deprecated(since = "0.8.0", note = "Use `finalize`.")] #[inline] pub fn compute(self) -> Digest { self.finalize() } } impl Default for Context { #[inline] fn default() -> Self { Self::new() } } impl core::convert::From for Digest { #[inline] fn from(context: Context) -> Digest { context.finalize() } } #[cfg(feature = "std")] impl core::io::Write for Context { #[inline] fn write(&mut self, data: &[u8]) -> core::io::Result { self.consume(data); Ok(data.len()) } #[inline] fn flush(&mut self) -> core::io::Result<()> { Ok(()) } } /// Compute the digest of data. #[inline] pub fn compute>(data: T) -> Digest { let mut context = Context::new(); context.consume(data); context.finalize() } #[rustfmt::skip] #[allow(clippy::double_parens, clippy::needless_range_loop)] fn consume( Context { buffer, count, state, }: &mut Context, data: &[u8], ) { let mut input = [0u32; 16]; let mut k = ((*count >> 3) & 0x3f) as usize; *count = count.wrapping_add((data.len() as u64) << 3); for &value in data { buffer[k] = value; k += 1; if k == 0x40 { let mut j = 0; for i in 0..16 { input[i] = ((buffer[j + 3] as u32) << 24) | ((buffer[j + 2] as u32) << 16) | ((buffer[j + 1] as u32) << 8) | ((buffer[j ] as u32) ); j += 4; } transform(state, &input); k = 0; } } } #[rustfmt::skip] fn transform(state: &mut [u32; 4], input: &[u32; 16]) { let (mut a, mut b, mut c, mut d) = (state[0], state[1], state[2], state[3]); macro_rules! add( ($a:expr, $b:expr) => ($a.wrapping_add($b)); ); macro_rules! rotate( ($x:expr, $n:expr) => ($x.rotate_left($n)); ); { macro_rules! F( ($x:expr, $y:expr, $z:expr) => (($x & $y) | (!$x & $z)); ); macro_rules! T( ($a:expr, $b:expr, $c:expr, $d:expr, $x:expr, $s:expr, $ac:expr) => ({ $a = add!(add!(add!($a, F!($b, $c, $d)), $x), $ac); $a = rotate!($a, $s); $a = add!($a, $b); }); ); const S1: u32 = 7; const S2: u32 = 12; const S3: u32 = 17; const S4: u32 = 22; T!(a, b, c, d, input[ 0], S1, 3614090360); T!(d, a, b, c, input[ 1], S2, 3905402710); T!(c, d, a, b, input[ 2], S3, 606105819); T!(b, c, d, a, input[ 3], S4, 3250441966); T!(a, b, c, d, input[ 4], S1, 4118548399); T!(d, a, b, c, input[ 5], S2, 1200080426); T!(c, d, a, b, input[ 6], S3, 2821735955); T!(b, c, d, a, input[ 7], S4, 4249261313); T!(a, b, c, d, input[ 8], S1, 1770035416); T!(d, a, b, c, input[ 9], S2, 2336552879); T!(c, d, a, b, input[10], S3, 4294925233); T!(b, c, d, a, input[11], S4, 2304563134); T!(a, b, c, d, input[12], S1, 1804603682); T!(d, a, b, c, input[13], S2, 4254626195); T!(c, d, a, b, input[14], S3, 2792965006); T!(b, c, d, a, input[15], S4, 1236535329); } { macro_rules! F( ($x:expr, $y:expr, $z:expr) => (($x & $z) | ($y & !$z)); ); macro_rules! T( ($a:expr, $b:expr, $c:expr, $d:expr, $x:expr, $s:expr, $ac:expr) => ({ $a = add!(add!(add!($a, F!($b, $c, $d)), $x), $ac); $a = rotate!($a, $s); $a = add!($a, $b); }); ); const S1: u32 = 5; const S2: u32 = 9; const S3: u32 = 14; const S4: u32 = 20; T!(a, b, c, d, input[ 1], S1, 4129170786); T!(d, a, b, c, input[ 6], S2, 3225465664); T!(c, d, a, b, input[11], S3, 643717713); T!(b, c, d, a, input[ 0], S4, 3921069994); T!(a, b, c, d, input[ 5], S1, 3593408605); T!(d, a, b, c, input[10], S2, 38016083); T!(c, d, a, b, input[15], S3, 3634488961); T!(b, c, d, a, input[ 4], S4, 3889429448); T!(a, b, c, d, input[ 9], S1, 568446438); T!(d, a, b, c, input[14], S2, 3275163606); T!(c, d, a, b, input[ 3], S3, 4107603335); T!(b, c, d, a, input[ 8], S4, 1163531501); T!(a, b, c, d, input[13], S1, 2850285829); T!(d, a, b, c, input[ 2], S2, 4243563512); T!(c, d, a, b, input[ 7], S3, 1735328473); T!(b, c, d, a, input[12], S4, 2368359562); } { macro_rules! F( ($x:expr, $y:expr, $z:expr) => ($x ^ $y ^ $z); ); macro_rules! T( ($a:expr, $b:expr, $c:expr, $d:expr, $x:expr, $s:expr, $ac:expr) => ({ $a = add!(add!(add!($a, F!($b, $c, $d)), $x), $ac); $a = rotate!($a, $s); $a = add!($a, $b); }); ); const S1: u32 = 4; const S2: u32 = 11; const S3: u32 = 16; const S4: u32 = 23; T!(a, b, c, d, input[ 5], S1, 4294588738); T!(d, a, b, c, input[ 8], S2, 2272392833); T!(c, d, a, b, input[11], S3, 1839030562); T!(b, c, d, a, input[14], S4, 4259657740); T!(a, b, c, d, input[ 1], S1, 2763975236); T!(d, a, b, c, input[ 4], S2, 1272893353); T!(c, d, a, b, input[ 7], S3, 4139469664); T!(b, c, d, a, input[10], S4, 3200236656); T!(a, b, c, d, input[13], S1, 681279174); T!(d, a, b, c, input[ 0], S2, 3936430074); T!(c, d, a, b, input[ 3], S3, 3572445317); T!(b, c, d, a, input[ 6], S4, 76029189); T!(a, b, c, d, input[ 9], S1, 3654602809); T!(d, a, b, c, input[12], S2, 3873151461); T!(c, d, a, b, input[15], S3, 530742520); T!(b, c, d, a, input[ 2], S4, 3299628645); } { macro_rules! F( ($x:expr, $y:expr, $z:expr) => ($y ^ ($x | !$z)); ); macro_rules! T( ($a:expr, $b:expr, $c:expr, $d:expr, $x:expr, $s:expr, $ac:expr) => ({ $a = add!(add!(add!($a, F!($b, $c, $d)), $x), $ac); $a = rotate!($a, $s); $a = add!($a, $b); }); ); const S1: u32 = 6; const S2: u32 = 10; const S3: u32 = 15; const S4: u32 = 21; T!(a, b, c, d, input[ 0], S1, 4096336452); T!(d, a, b, c, input[ 7], S2, 1126891415); T!(c, d, a, b, input[14], S3, 2878612391); T!(b, c, d, a, input[ 5], S4, 4237533241); T!(a, b, c, d, input[12], S1, 1700485571); T!(d, a, b, c, input[ 3], S2, 2399980690); T!(c, d, a, b, input[10], S3, 4293915773); T!(b, c, d, a, input[ 1], S4, 2240044497); T!(a, b, c, d, input[ 8], S1, 1873313359); T!(d, a, b, c, input[15], S2, 4264355552); T!(c, d, a, b, input[ 6], S3, 2734768916); T!(b, c, d, a, input[13], S4, 1309151649); T!(a, b, c, d, input[ 4], S1, 4149444226); T!(d, a, b, c, input[11], S2, 3174756917); T!(c, d, a, b, input[ 2], S3, 718787259); T!(b, c, d, a, input[ 9], S4, 3951481745); } state[0] = add!(state[0], a); state[1] = add!(state[1], b); state[2] = add!(state[2], c); state[3] = add!(state[3], d); } #[cfg(test)] mod tests { use std::io::prelude::Write; use super::Context; #[test] fn compute() { let inputs = [ "", "a", "abc", "message digest", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "0123456789012345678901234567890123456789012345678901234567890123", "12345678901234567890123456789012345678901234567890123456789012345678901234567890", ]; let outputs = [ "d41d8cd98f00b204e9800998ecf8427e", "0cc175b9c0f1b6a831c399e269772661", "900150983cd24fb0d6963f7d28e17f72", "f96b697d7cb7938d525a2f31aaf161d0", "c3fcd3d76192e4007dfb496cca67e13b", "d174ab98d277d9f5a5611c2c9f419d9f", "7f7bfd348709deeaace19e3f535f8c54", "57edf4a22be3c955ac49da2e2107b67a", ]; for (input, &output) in inputs.iter().zip(outputs.iter()) { let digest = super::compute(input); assert_eq!(format!("{digest:x}"), output); let mut context = Context::new(); context.consume(input); let digest = context.finalize(); assert_eq!(format!("{digest:x}"), output); } } #[test] fn index() { let mut digest = super::compute(b"abc"); assert_eq!(digest[0], 0x90); assert_eq!(&digest[0], &0x90); assert_eq!(&mut digest[0], &mut 0x90); } #[test] fn write_29() { let data = vec![0; 8 * 1024 * 1024]; let mut context = Context::new(); for _ in 0..64 { context.write(&data).unwrap(); } assert_eq!( format!("{:x}", context.finalize()), "aa559b4e3523a6c931f08f4df52d58f2", ); } #[test] fn write_32() { let data = vec![0; std::u32::MAX as usize + 1]; let mut context = Context::new(); context.write(&data).unwrap(); assert_eq!( format!("{:x}", context.finalize()), "c9a5a6878d97b48cc965c1e41859f034", ); } }