]>
code.octet-stream.net Git - hashgood/blob - src/main.rs
2 use std
::path
::{Path
, PathBuf
};
4 use structopt
::StructOpt
;
6 /// Calculate digests for given input data
9 /// Display output nicely in the terminal
12 /// Collect candidate hashes based on options and match them against a calculated hash
15 /// Problem running the program
16 const EXIT_ERR
: i32 = 1;
17 /// Verification was performed and was not a match
18 const EXIT_MISMATCH
: i32 = 2;
21 #[structopt(name = "hashgood")]
23 /// Disable ANSI colours in output
24 #[structopt(short = "C", long = "no-colour")]
27 /// A file containing the hash to verify. It can either be a raw hash or a SHASUMS-style listing. Use `-` for standard input.
28 #[structopt(short = "c", long = "check", parse(from_os_str))]
29 hash_file
: Option
<PathBuf
>,
31 /// The file to be verified or `-` for standard input
32 #[structopt(name = "input", parse(from_os_str))]
35 /// A hash to verify, supplied directly on the command line
36 #[structopt(name = "hash")]
40 /// Types of supported digest algorithm
41 #[derive(Debug, PartialEq, Copy, Clone)]
50 /// Assume a hash type from the binary length. Fortunately the typical 3 algorithms we care about are different lengths.
51 pub fn from_len(len
: usize) -> Result
<Algorithm
, String
> {
53 16 => Ok(Algorithm
::Md5
),
54 20 => Ok(Algorithm
::Sha1
),
55 32 => Ok(Algorithm
::Sha256
),
56 64 => Ok(Algorithm
::Sha512
),
57 _
=> Err(format
!("Unrecognised hash length: {} bytes", len
)),
62 /// The method by which one or more hashes were supplied to verify the calculated digest
63 #[derive(Debug, PartialEq)]
64 pub enum VerificationSource
{
70 /// A complete standalone hash result
78 pub fn new(alg
: Algorithm
, bytes
: Vec
<u8>, path
: &Path
) -> Self {
79 // Taking the filename component should always work?
80 // If not, just fall back to the full path
81 let filename
= match path
.file
_name
() {
82 Some(filename
) => filename
.to_string_lossy(),
83 None
=> path
.to_string_lossy(),
88 filename
: filename
.to_string(),
93 /// A possible hash to match against. The algorithm is assumed.
94 #[derive(Debug, PartialEq)]
95 pub struct CandidateHash
{
97 filename
: Option
<String
>,
100 /// A list of candidate hashes that our input could potentially match. At this point it is
101 /// assumed that we will be verifying a digest of a particular, single algorithm.
102 #[derive(Debug, PartialEq)]
103 pub struct CandidateHashes
{
105 hashes
: Vec
<CandidateHash
>,
106 source
: VerificationSource
,
109 /// Summary of an atetmpt to match the calculated digest against candidates
111 pub enum MatchLevel
{
117 /// The severity of any informational messages to be printed before the final result
118 pub enum MessageLevel
{
124 /// Overall details of an attempt to match the calculated digest against candidates
125 pub struct Verification
<'a
> {
126 match_level
: MatchLevel
,
127 comparison_hash
: Option
<&'a CandidateHash
>,
128 messages
: Vec
<(MessageLevel
, String
)>,
131 /// Entry point - run the program and handle errors ourselves cleanly.
133 /// At the moment there aren't really any errors that can be handled by the application. Therefore
134 /// stringly-typed errors are used and they are all captured here, where the problem is printed
135 /// and the application terminates with a non-zero return code.
137 hashgood().unwrap
_or
_else
(|e
| {
138 eprintln
!("Error: {}", e
);
139 process
::exit(EXIT_ERR
);
143 /// Main application logic
144 fn hashgood() -> Result
<(), Box
<dyn Error
>> {
145 let opt
= get_verified_options()?
;
146 let candidates
= verify
::get_candidate_hashes(&opt
)?
;
147 let input
= calculate
::get_input_reader(opt
.inp
ut
.as_path())?
;
148 if let Some(c
) = candidates
{
149 // If we have a candidate hash of a particular type, use that specific algorithm
150 let hashes
= calculate
::create_digests(&[c
.alg
], input
)?
;
151 for (alg
, bytes
) in hashes
{
152 // Should always be true
154 let hash
= Hash
::new(alg
, bytes
, &opt
.inp
ut
);
155 let verification
= verify
::verify_hash(&hash
, &c
);
156 let successful_match
= verification
.match_level
== MatchLevel
::Ok
;
159 verification
.comparison_hash
,
163 display
::print_messages(verification
.messages
, opt
.no_colour
)?
;
164 display
::print_match_level(verification
.match_level
, opt
.no_colour
)?
;
165 if !successful_match
{
166 process
::exit(EXIT_MISMATCH
);
171 // If no candidate, calculate all three common digest types for output
172 let hashes
= calculate
::create_digests(
181 for (alg
, bytes
) in hashes
{
185 filename
: opt
.inp
ut
.file
_name
().unwrap
().to_string_lossy().to_string(),
187 display
::print_hash(&hash
, None
, None
, opt
.no_colour
)?
;
193 /// Parse the command line options and check for ambiguous or inconsistent settings
194 fn get_verified_options() -> Result
<Opt
, String
> {
195 let opt
= Opt
::from_args();
196 let hash_methods
= opt
.hash
.is
_some
() as i32 + opt
.hash_file
.is
_some
() as i32;
197 if hash_methods
> 1 {
198 if opt
.hash
.is
_some
() {
199 eprintln
!("* specified as command line argument");
201 if opt
.hash_file
.is
_some
() {
202 eprintln
!("* check hash from file (-c)")
204 return Err("Error: Hashes were provided by multiple methods. Use only one.".to_owned());
206 if opt
.inp
ut
.to_str() == Some("-")
207 && opt
.hash_file
.as_ref().and_then(|h
| h
.to_str()) == Some("-")
209 return Err("Error: Cannot use use stdin for both hash file and input data".to_owned());