Skip to main content

HashAlgorithm

Enum HashAlgorithm 

Source
#[non_exhaustive]
pub enum HashAlgorithm { Sha1, Sha256, Sha384, Sha512, }
Expand description

The hash a file names.

This is the same enum the decrypt path runs on (hash.rs adds the digest, HMAC and IV-derivation operations to it under the crypto-ops feature), so there is one spelling table rather than a reporting copy that can drift from an operational one.

Reported as Option<HashAlgorithm>, with None meaning absent or not one of the four this crate implements.

§Examples

use msoffice_crypto::{classify, HashAlgorithm};

let class = classify(include_bytes!("../tests/fixtures/agile_encrypted.docx"));
assert_eq!(class.key_data.unwrap().hash, Some(HashAlgorithm::Sha512));

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Sha1

SHA-1, 20-byte digest.

§

Sha256

SHA-256, 32-byte digest.

§

Sha384

SHA-384, 48-byte digest.

§

Sha512

SHA-512, 64-byte digest. What Office 16 writes for agile.

Implementations§

Source§

impl HashAlgorithm

Source

pub fn name(self) -> &'static str

Available on crate feature crypto-ops only.

The unhyphenated ECMA-376 spelling — what Office writes, and what this crate prints in errors. HashAlgorithm::parse also accepts the hyphenated form (SHA-1), so a file that used it is reported back in the canonical spelling rather than in its own.

pub rather than pub(crate) for the same reason as Self::digest_len: the spelling is the one a caller has to put back into hashAlgorithm when it describes its own parameters, and it is not derivable from Debug, which prints the Rust variant name (Sha512) and not the attribute value (SHA512).

Source

pub fn digest_len(self) -> usize

Available on crate feature crypto-ops only.

Digest length in bytes. A hashSize attribute is checked against this before it is trusted as a truncation length, and keyBits / 8 before it is trusted as one.

Why this is pub when everything else on this impl is internal. It is a fact about SHA, not about this crate — FIPS 180-4 fixes all four numbers, and no decision of ours can move them. What is ours is the coupling it feeds: a caller choosing encryption parameters meets a refusal when keyBits / 8 exceeds the digest of the hash it named (can_carry_key_bits, internal), and without this number it cannot reason about that rule before it trips over it — it would have to hardcode the same four constants to predict our own error. hashSize is not a companion to this: [MS-OFFCRYPTO] §2.3.4.10 requires it to equal the named hash’s digest length, so it is derived from this value rather than chosen beside it, which is why no setter for it exists.

Trait Implementations§

Source§

impl Clone for HashAlgorithm

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for HashAlgorithm

Source§

impl Debug for HashAlgorithm

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for HashAlgorithm

Source§

impl PartialEq for HashAlgorithm

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for HashAlgorithm

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.