Skip to main content

git_sshripped_encryption_models/
lib.rs

1#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]
2#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
3#![allow(clippy::multiple_crate_versions)]
4
5use thiserror::Error;
6
7pub const ENCRYPTED_MAGIC: [u8; 4] = *b"GSC1";
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
10#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
11pub enum EncryptionAlgorithm {
12    AesSivV1,
13}
14
15impl EncryptionAlgorithm {
16    #[must_use]
17    pub const fn id(self) -> u8 {
18        match self {
19            Self::AesSivV1 => 1,
20        }
21    }
22
23    /// Parse an algorithm from its binary id.
24    ///
25    /// # Errors
26    ///
27    /// Returns [`EncryptionModelsError::UnknownAlgorithm`] if `id` does not
28    /// correspond to a known algorithm.
29    pub const fn from_id(id: u8) -> Result<Self, EncryptionModelsError> {
30        match id {
31            1 => Ok(Self::AesSivV1),
32            _ => Err(EncryptionModelsError::UnknownAlgorithm(id)),
33        }
34    }
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
38pub struct EncryptedHeader {
39    pub version: u8,
40    pub algorithm: EncryptionAlgorithm,
41}
42
43impl Default for EncryptedHeader {
44    fn default() -> Self {
45        Self {
46            version: 1,
47            algorithm: EncryptionAlgorithm::AesSivV1,
48        }
49    }
50}
51
52#[derive(Debug, Error)]
53pub enum EncryptionModelsError {
54    #[error("unknown encryption algorithm id: {0}")]
55    UnknownAlgorithm(u8),
56    #[error("invalid encrypted file header")]
57    InvalidHeader,
58}