Skip to main content

base64_ng/v2/
profiles.rs

1//! Accurately scoped Base64 body and alphabet presets.
2
3use super::{
4    alphabet::{BCRYPT_ALPHABET, CRYPT_ALPHABET, IMAP_MUTF7_ALPHABET, PBKDF2_ALPHABET},
5    specifications::{
6        Base64, DecodePadding, EncodePadding, RuntimeSpec, STRICT_STANDARD_PADDED,
7        StrictStandardPadded, TrailingBits, runtime_codec,
8    },
9    wrapping::LineWrap,
10};
11
12/// One Base64 codec paired with an encoded-body line layout.
13///
14/// This value describes body bytes only. It does not parse MIME headers, PEM
15/// labels or boundaries, or any surrounding protocol container.
16#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
17pub struct BodyCodec<S> {
18    codec: Base64<S>,
19    wrapping: LineWrap,
20}
21
22impl<S> BodyCodec<S> {
23    const fn new(codec: Base64<S>, wrapping: LineWrap) -> Self {
24        Self { codec, wrapping }
25    }
26
27    /// Returns the complete Base64 alphabet and padding policy.
28    #[must_use]
29    pub const fn codec(&self) -> &Base64<S> {
30        &self.codec
31    }
32
33    /// Returns the encoded-body wrapping policy.
34    #[must_use]
35    pub const fn wrapping(&self) -> LineWrap {
36        self.wrapping
37    }
38}
39
40/// Strict Standard Base64 with MIME's 76-column CRLF body layout.
41///
42/// This is not a MIME message, header, or content-transfer parser.
43pub const MIME_BODY_STRICT: BodyCodec<StrictStandardPadded> =
44    BodyCodec::new(STRICT_STANDARD_PADDED, LineWrap::MIME_BODY_WRAP);
45
46/// Strict Standard Base64 with a 64-column LF PEM body layout.
47///
48/// This does not parse PEM labels, boundaries, headers, or surrounding text.
49pub const PEM_BODY_LF: BodyCodec<StrictStandardPadded> =
50    BodyCodec::new(STRICT_STANDARD_PADDED, LineWrap::PEM_BODY_LF_WRAP);
51
52/// Strict Standard Base64 with a 64-column CRLF PEM body layout.
53///
54/// This does not parse PEM labels, boundaries, headers, or surrounding text.
55pub const PEM_BODY_CRLF: BodyCodec<StrictStandardPadded> =
56    BodyCodec::new(STRICT_STANDARD_PADDED, LineWrap::PEM_BODY_CRLF_WRAP);
57
58/// Standard bit grouping with the bcrypt alphabet and no padding.
59///
60/// This does not parse or construct bcrypt password records.
61pub const BCRYPT_ALPHABET_NO_PAD: Base64<RuntimeSpec> = runtime_codec(
62    BCRYPT_ALPHABET,
63    EncodePadding::Unpadded,
64    DecodePadding::Forbid,
65    TrailingBits::RequireCanonical,
66);
67
68/// Standard bit grouping with the `crypt(3)` alphabet and no padding.
69///
70/// This does not implement the little-endian field transforms used by several
71/// password-record formats and does not parse a `crypt(3)` record.
72pub const CRYPT_ALPHABET_NO_PAD: Base64<RuntimeSpec> = runtime_codec(
73    CRYPT_ALPHABET,
74    EncodePadding::Unpadded,
75    DecodePadding::Forbid,
76    TrailingBits::RequireCanonical,
77);
78
79/// Standard bit grouping with the PBKDF2-adapted alphabet and no padding.
80///
81/// The alphabet replaces RFC 4648 Standard `+` with `.`. This value does not
82/// parse Passlib records or enforce salt and checksum lengths.
83pub const PBKDF2_ALPHABET_NO_PAD: Base64<RuntimeSpec> = runtime_codec(
84    PBKDF2_ALPHABET,
85    EncodePadding::Unpadded,
86    DecodePadding::Forbid,
87    TrailingBits::RequireCanonical,
88);
89
90/// Standard bit grouping with the IMAP modified-UTF-7 alphabet and no padding.
91///
92/// This value performs alphabet-level Base64 only. It does not shift UTF-16BE,
93/// delimit modified-UTF-7 runs, or encode mailbox names.
94pub const IMAP_MUTF7_ALPHABET_NO_PAD: Base64<RuntimeSpec> = runtime_codec(
95    IMAP_MUTF7_ALPHABET,
96    EncodePadding::Unpadded,
97    DecodePadding::Forbid,
98    TrailingBits::RequireCanonical,
99);