Pure Programmer
Blue Matrix


Cluster Map

Project: One-Way Hash

A one-way hash is a small value that we compute based on a larger amount of data. It is also known as a digest.

One-way hashes are useful in cryptographic contexts because good ones can't be used to deduce the original data from the hash. We could, for example, store the one-way hash values of passwords in a file and use them to confirm that the password entered by a user hashes to the correct value stored in the file. This way we don't expose the actual passwords in the file. They can also be used for digital signatures and checksums.

They are also useful in creating hash tables used to implement maps or dictionaries. The one-way hash computed from a given key will correspond to the index in the array where the corresponding value will be stored in the hash table.

Write a function that implements the [[https://en.wikipedia.org/wiki/MD5|MD5]] hash algorithm. You main program should accept a filename on the command line, read the file in binary mode, compute the MD5 hash and print the MD5 hash in hexadecimal on the console.

See [[https://en.wikipedia.org/wiki/Cryptographic_hash_function|Cryptographic Hash]]
See other project

Output
$ rustc OneWayHash.rs error: expected one of `.`, `;`, `?`, `else`, or an operator, found `,` --> OneWayHash.rs:68:23 | 68 | let mut F:isize = 0, g:isize = 0; | ^ expected one of `.`, `;`, `?`, `else`, or an operator error[E0412]: cannot find type `usuze` in this scope --> OneWayHash.rs:55:47 | 55 | return ((x << i) as i32) as isize | (((x) as usuze >> (32 - i)) as isize); | ^^^^^ help: a builtin type with a similar name exists: `usize` error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:110:10 | 110 | binmode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:115:18 | 115 | while getbyte(c,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:167:8 | 167 | close(ifh)?; | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> OneWayHash.rs:175:3 | 175 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0425]: cannot find function `suffix` in this scope --> OneWayHash.rs:27:10 | 27 | return (suffix(paddedHex,count)).to_string(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `suffix` in this scope --> OneWayHash.rs:32:15 | 32 | paddedHex = (suffix(paddedHex,count)).to_string(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `substrByLen` in this scope --> OneWayHash.rs:39:15 | 39 | result = &(substrByLen(paddedHex,p,2)); | ^^^^^^^^^^^ not found in this scope error[E0308]: mismatched types --> OneWayHash.rs:43:9 | 30 | fn toHexadecimalLE(i:isize, count:isize) -> String { | ------ expected `String` because of return type ... 43 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0308]: mismatched types --> OneWayHash.rs:51:9 | 46 | fn printMessageBlock(block:Vec<u8>) -> String { | ------ expected `String` because of return type ... 51 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0425]: cannot find function `binmode` in this scope --> OneWayHash.rs:110:2 | 110 | binmode(ifh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `getbyte` in this scope --> OneWayHash.rs:115:8 | 115 | while getbyte(c,ifh) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> OneWayHash.rs:167:2 | 167 | close(ifh)?; | ^^^^^ not found in this scope error[E0308]: mismatched types --> OneWayHash.rs:168:9 | 109 | fn computeMD5(filespec:&str) -> Result<Vec<isize>, Arc<dyn error::Error>> { | ----------------------------------------- expected `Result<Vec<isize>, Arc<(dyn std::error::Error + 'static)>>` because of return type ... 168 | return md5State; | ^^^^^^^^ expected `Result<Vec<isize>, Arc<dyn Error>>`, found `Vec<isize>` | = note: expected enum `Result<Vec<_>, Arc<(dyn std::error::Error + 'static)>>` found struct `Vec<_>` help: try wrapping the expression in `Ok` | 168 | return Ok(md5State); | +++ + error[E0433]: failed to resolve: use of undeclared crate or module `io` --> OneWayHash.rs:189:59 | 189 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 16 previous errors Some errors have detailed explanations: E0308, E0412, E0425, E0433. For more information about an error, try `rustc --explain E0308`. $ rustc OneWayHash.rs error: expected one of `.`, `;`, `?`, `else`, or an operator, found `,` --> OneWayHash.rs:68:23 | 68 | let mut F:isize = 0, g:isize = 0; | ^ expected one of `.`, `;`, `?`, `else`, or an operator error[E0412]: cannot find type `usuze` in this scope --> OneWayHash.rs:55:47 | 55 | return ((x << i) as i32) as isize | (((x) as usuze >> (32 - i)) as isize); | ^^^^^ help: a builtin type with a similar name exists: `usize` error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:110:10 | 110 | binmode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:115:18 | 115 | while getbyte(c,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:167:8 | 167 | close(ifh)?; | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> OneWayHash.rs:175:3 | 175 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0425]: cannot find function `suffix` in this scope --> OneWayHash.rs:27:10 | 27 | return (suffix(paddedHex,count)).to_string(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `suffix` in this scope --> OneWayHash.rs:32:15 | 32 | paddedHex = (suffix(paddedHex,count)).to_string(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `substrByLen` in this scope --> OneWayHash.rs:39:15 | 39 | result = &(substrByLen(paddedHex,p,2)); | ^^^^^^^^^^^ not found in this scope error[E0308]: mismatched types --> OneWayHash.rs:43:9 | 30 | fn toHexadecimalLE(i:isize, count:isize) -> String { | ------ expected `String` because of return type ... 43 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0308]: mismatched types --> OneWayHash.rs:51:9 | 46 | fn printMessageBlock(block:Vec<u8>) -> String { | ------ expected `String` because of return type ... 51 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0425]: cannot find function `binmode` in this scope --> OneWayHash.rs:110:2 | 110 | binmode(ifh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `getbyte` in this scope --> OneWayHash.rs:115:8 | 115 | while getbyte(c,ifh) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> OneWayHash.rs:167:2 | 167 | close(ifh)?; | ^^^^^ not found in this scope error[E0308]: mismatched types --> OneWayHash.rs:168:9 | 109 | fn computeMD5(filespec:&str) -> Result<Vec<isize>, Arc<dyn error::Error>> { | ----------------------------------------- expected `Result<Vec<isize>, Arc<(dyn std::error::Error + 'static)>>` because of return type ... 168 | return md5State; | ^^^^^^^^ expected `Result<Vec<isize>, Arc<dyn Error>>`, found `Vec<isize>` | = note: expected enum `Result<Vec<_>, Arc<(dyn std::error::Error + 'static)>>` found struct `Vec<_>` help: try wrapping the expression in `Ok` | 168 | return Ok(md5State); | +++ + error[E0433]: failed to resolve: use of undeclared crate or module `io` --> OneWayHash.rs:189:59 | 189 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 16 previous errors Some errors have detailed explanations: E0308, E0412, E0425, E0433. For more information about an error, try `rustc --explain E0308`. $ rustc OneWayHash.rs error: expected one of `.`, `;`, `?`, `else`, or an operator, found `,` --> OneWayHash.rs:68:23 | 68 | let mut F:isize = 0, g:isize = 0; | ^ expected one of `.`, `;`, `?`, `else`, or an operator error[E0412]: cannot find type `usuze` in this scope --> OneWayHash.rs:55:47 | 55 | return ((x << i) as i32) as isize | (((x) as usuze >> (32 - i)) as isize); | ^^^^^ help: a builtin type with a similar name exists: `usize` error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:110:10 | 110 | binmode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:115:18 | 115 | while getbyte(c,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:167:8 | 167 | close(ifh)?; | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> OneWayHash.rs:175:3 | 175 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0425]: cannot find function `suffix` in this scope --> OneWayHash.rs:27:10 | 27 | return (suffix(paddedHex,count)).to_string(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `suffix` in this scope --> OneWayHash.rs:32:15 | 32 | paddedHex = (suffix(paddedHex,count)).to_string(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `substrByLen` in this scope --> OneWayHash.rs:39:15 | 39 | result = &(substrByLen(paddedHex,p,2)); | ^^^^^^^^^^^ not found in this scope error[E0308]: mismatched types --> OneWayHash.rs:43:9 | 30 | fn toHexadecimalLE(i:isize, count:isize) -> String { | ------ expected `String` because of return type ... 43 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0308]: mismatched types --> OneWayHash.rs:51:9 | 46 | fn printMessageBlock(block:Vec<u8>) -> String { | ------ expected `String` because of return type ... 51 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0425]: cannot find function `binmode` in this scope --> OneWayHash.rs:110:2 | 110 | binmode(ifh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `getbyte` in this scope --> OneWayHash.rs:115:8 | 115 | while getbyte(c,ifh) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> OneWayHash.rs:167:2 | 167 | close(ifh)?; | ^^^^^ not found in this scope error[E0308]: mismatched types --> OneWayHash.rs:168:9 | 109 | fn computeMD5(filespec:&str) -> Result<Vec<isize>, Arc<dyn error::Error>> { | ----------------------------------------- expected `Result<Vec<isize>, Arc<(dyn std::error::Error + 'static)>>` because of return type ... 168 | return md5State; | ^^^^^^^^ expected `Result<Vec<isize>, Arc<dyn Error>>`, found `Vec<isize>` | = note: expected enum `Result<Vec<_>, Arc<(dyn std::error::Error + 'static)>>` found struct `Vec<_>` help: try wrapping the expression in `Ok` | 168 | return Ok(md5State); | +++ + error[E0433]: failed to resolve: use of undeclared crate or module `io` --> OneWayHash.rs:189:59 | 189 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 16 previous errors Some errors have detailed explanations: E0308, E0412, E0425, E0433. For more information about an error, try `rustc --explain E0308`. $ rustc OneWayHash.rs error: expected one of `.`, `;`, `?`, `else`, or an operator, found `,` --> OneWayHash.rs:68:23 | 68 | let mut F:isize = 0, g:isize = 0; | ^ expected one of `.`, `;`, `?`, `else`, or an operator error[E0412]: cannot find type `usuze` in this scope --> OneWayHash.rs:55:47 | 55 | return ((x << i) as i32) as isize | (((x) as usuze >> (32 - i)) as isize); | ^^^^^ help: a builtin type with a similar name exists: `usize` error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:110:10 | 110 | binmode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:115:18 | 115 | while getbyte(c,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:167:8 | 167 | close(ifh)?; | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> OneWayHash.rs:175:3 | 175 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0425]: cannot find function `suffix` in this scope --> OneWayHash.rs:27:10 | 27 | return (suffix(paddedHex,count)).to_string(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `suffix` in this scope --> OneWayHash.rs:32:15 | 32 | paddedHex = (suffix(paddedHex,count)).to_string(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `substrByLen` in this scope --> OneWayHash.rs:39:15 | 39 | result = &(substrByLen(paddedHex,p,2)); | ^^^^^^^^^^^ not found in this scope error[E0308]: mismatched types --> OneWayHash.rs:43:9 | 30 | fn toHexadecimalLE(i:isize, count:isize) -> String { | ------ expected `String` because of return type ... 43 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0308]: mismatched types --> OneWayHash.rs:51:9 | 46 | fn printMessageBlock(block:Vec<u8>) -> String { | ------ expected `String` because of return type ... 51 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0425]: cannot find function `binmode` in this scope --> OneWayHash.rs:110:2 | 110 | binmode(ifh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `getbyte` in this scope --> OneWayHash.rs:115:8 | 115 | while getbyte(c,ifh) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> OneWayHash.rs:167:2 | 167 | close(ifh)?; | ^^^^^ not found in this scope error[E0308]: mismatched types --> OneWayHash.rs:168:9 | 109 | fn computeMD5(filespec:&str) -> Result<Vec<isize>, Arc<dyn error::Error>> { | ----------------------------------------- expected `Result<Vec<isize>, Arc<(dyn std::error::Error + 'static)>>` because of return type ... 168 | return md5State; | ^^^^^^^^ expected `Result<Vec<isize>, Arc<dyn Error>>`, found `Vec<isize>` | = note: expected enum `Result<Vec<_>, Arc<(dyn std::error::Error + 'static)>>` found struct `Vec<_>` help: try wrapping the expression in `Ok` | 168 | return Ok(md5State); | +++ + error[E0433]: failed to resolve: use of undeclared crate or module `io` --> OneWayHash.rs:189:59 | 189 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 16 previous errors Some errors have detailed explanations: E0308, E0412, E0425, E0433. For more information about an error, try `rustc --explain E0308`. $ rustc OneWayHash.rs error: expected one of `.`, `;`, `?`, `else`, or an operator, found `,` --> OneWayHash.rs:68:23 | 68 | let mut F:isize = 0, g:isize = 0; | ^ expected one of `.`, `;`, `?`, `else`, or an operator error[E0412]: cannot find type `usuze` in this scope --> OneWayHash.rs:55:47 | 55 | return ((x << i) as i32) as isize | (((x) as usuze >> (32 - i)) as isize); | ^^^^^ help: a builtin type with a similar name exists: `usize` error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:110:10 | 110 | binmode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:115:18 | 115 | while getbyte(c,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:167:8 | 167 | close(ifh)?; | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> OneWayHash.rs:175:3 | 175 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0425]: cannot find function `suffix` in this scope --> OneWayHash.rs:27:10 | 27 | return (suffix(paddedHex,count)).to_string(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `suffix` in this scope --> OneWayHash.rs:32:15 | 32 | paddedHex = (suffix(paddedHex,count)).to_string(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `substrByLen` in this scope --> OneWayHash.rs:39:15 | 39 | result = &(substrByLen(paddedHex,p,2)); | ^^^^^^^^^^^ not found in this scope error[E0308]: mismatched types --> OneWayHash.rs:43:9 | 30 | fn toHexadecimalLE(i:isize, count:isize) -> String { | ------ expected `String` because of return type ... 43 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0308]: mismatched types --> OneWayHash.rs:51:9 | 46 | fn printMessageBlock(block:Vec<u8>) -> String { | ------ expected `String` because of return type ... 51 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0425]: cannot find function `binmode` in this scope --> OneWayHash.rs:110:2 | 110 | binmode(ifh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `getbyte` in this scope --> OneWayHash.rs:115:8 | 115 | while getbyte(c,ifh) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> OneWayHash.rs:167:2 | 167 | close(ifh)?; | ^^^^^ not found in this scope error[E0308]: mismatched types --> OneWayHash.rs:168:9 | 109 | fn computeMD5(filespec:&str) -> Result<Vec<isize>, Arc<dyn error::Error>> { | ----------------------------------------- expected `Result<Vec<isize>, Arc<(dyn std::error::Error + 'static)>>` because of return type ... 168 | return md5State; | ^^^^^^^^ expected `Result<Vec<isize>, Arc<dyn Error>>`, found `Vec<isize>` | = note: expected enum `Result<Vec<_>, Arc<(dyn std::error::Error + 'static)>>` found struct `Vec<_>` help: try wrapping the expression in `Ok` | 168 | return Ok(md5State); | +++ + error[E0433]: failed to resolve: use of undeclared crate or module `io` --> OneWayHash.rs:189:59 | 189 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 16 previous errors Some errors have detailed explanations: E0308, E0412, E0425, E0433. For more information about an error, try `rustc --explain E0308`. $ rustc OneWayHash.rs error: expected one of `.`, `;`, `?`, `else`, or an operator, found `,` --> OneWayHash.rs:68:23 | 68 | let mut F:isize = 0, g:isize = 0; | ^ expected one of `.`, `;`, `?`, `else`, or an operator error[E0412]: cannot find type `usuze` in this scope --> OneWayHash.rs:55:47 | 55 | return ((x << i) as i32) as isize | (((x) as usuze >> (32 - i)) as isize); | ^^^^^ help: a builtin type with a similar name exists: `usize` error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:110:10 | 110 | binmode(ifh); | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:115:18 | 115 | while getbyte(c,ifh) { | ^^^ not found in this scope error[E0425]: cannot find value `ifh` in this scope --> OneWayHash.rs:167:8 | 167 | close(ifh)?; | ^^^ not found in this scope error[E0425]: cannot find function `exit` in this scope --> OneWayHash.rs:175:3 | 175 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 8 + use std::process::exit; | error[E0425]: cannot find function `suffix` in this scope --> OneWayHash.rs:27:10 | 27 | return (suffix(paddedHex,count)).to_string(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `suffix` in this scope --> OneWayHash.rs:32:15 | 32 | paddedHex = (suffix(paddedHex,count)).to_string(); | ^^^^^^ not found in this scope error[E0425]: cannot find function `substrByLen` in this scope --> OneWayHash.rs:39:15 | 39 | result = &(substrByLen(paddedHex,p,2)); | ^^^^^^^^^^^ not found in this scope error[E0308]: mismatched types --> OneWayHash.rs:43:9 | 30 | fn toHexadecimalLE(i:isize, count:isize) -> String { | ------ expected `String` because of return type ... 43 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0308]: mismatched types --> OneWayHash.rs:51:9 | 46 | fn printMessageBlock(block:Vec<u8>) -> String { | ------ expected `String` because of return type ... 51 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0425]: cannot find function `binmode` in this scope --> OneWayHash.rs:110:2 | 110 | binmode(ifh); | ^^^^^^^ not found in this scope error[E0425]: cannot find function `getbyte` in this scope --> OneWayHash.rs:115:8 | 115 | while getbyte(c,ifh) { | ^^^^^^^ not found in this scope error[E0425]: cannot find function `close` in this scope --> OneWayHash.rs:167:2 | 167 | close(ifh)?; | ^^^^^ not found in this scope error[E0308]: mismatched types --> OneWayHash.rs:168:9 | 109 | fn computeMD5(filespec:&str) -> Result<Vec<isize>, Arc<dyn error::Error>> { | ----------------------------------------- expected `Result<Vec<isize>, Arc<(dyn std::error::Error + 'static)>>` because of return type ... 168 | return md5State; | ^^^^^^^^ expected `Result<Vec<isize>, Arc<dyn Error>>`, found `Vec<isize>` | = note: expected enum `Result<Vec<_>, Arc<(dyn std::error::Error + 'static)>>` found struct `Vec<_>` help: try wrapping the expression in `Ok` | 168 | return Ok(md5State); | +++ + error[E0433]: failed to resolve: use of undeclared crate or module `io` --> OneWayHash.rs:189:59 | 189 | if let Some(ex) = utils::isCustomErrorType(ex.clone(), io::Error("".to_string())){ | ^^ | | | use of undeclared crate or module `io` | help: a builtin type with a similar name exists: `i8` error: aborting due to 16 previous errors Some errors have detailed explanations: E0308, E0412, E0425, E0433. For more information about an error, try `rustc --explain E0308`.

Solution