/rust/registry/src/index.crates.io-1949cf8c6b5b557f/argon2-0.5.3/src/version.rs
Line | Count | Source |
1 | | //! Version of the algorithm. |
2 | | |
3 | | use crate::{Error, Result}; |
4 | | |
5 | | /// Version of the algorithm. |
6 | | #[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)] |
7 | | #[repr(u32)] |
8 | | pub enum Version { |
9 | | /// Version 16 (0x10 in hex) |
10 | | /// |
11 | | /// Performs overwrite internally |
12 | | V0x10 = 0x10, |
13 | | |
14 | | /// Version 19 (0x13 in hex, default) |
15 | | /// |
16 | | /// Performs XOR internally |
17 | | #[default] |
18 | | V0x13 = 0x13, |
19 | | } |
20 | | |
21 | | impl Version { |
22 | | /// Serialize version as little endian bytes |
23 | 973 | pub(crate) const fn to_le_bytes(self) -> [u8; 4] { |
24 | 973 | (self as u32).to_le_bytes() |
25 | 973 | } |
26 | | } |
27 | | |
28 | | impl From<Version> for u32 { |
29 | 973 | fn from(version: Version) -> u32 { |
30 | 973 | version as u32 |
31 | 973 | } |
32 | | } |
33 | | |
34 | | impl TryFrom<u32> for Version { |
35 | | type Error = Error; |
36 | | |
37 | 0 | fn try_from(version_id: u32) -> Result<Version> { |
38 | 0 | match version_id { |
39 | 0 | 0x10 => Ok(Version::V0x10), |
40 | 0 | 0x13 => Ok(Version::V0x13), |
41 | 0 | _ => Err(Error::VersionInvalid), |
42 | | } |
43 | 0 | } |
44 | | } |