Coverage Report

Created: 2026-08-13 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/base64ct-1.8.3/src/alphabet/shacrypt.rs
Line
Count
Source
1
//! `crypt(3)` Base64 encoding for sha* family.
2
3
use super::{Alphabet, DecodeStep, EncodeStep};
4
5
/// `crypt(3)` Base64 encoding.
6
///
7
/// This is the standard Base64 encoding used by password hashes for the following schemes:
8
/// - MD5-Crypt
9
/// - scrypt
10
/// - SHA1-Crypt
11
/// - SHA256-Crypt
12
/// - SHA512-Crypt
13
/// - yescrypt
14
///
15
/// ```text
16
/// [.-9]      [A-Z]      [a-z]
17
/// 0x2e-0x39, 0x41-0x5a, 0x61-0x7a
18
/// ```
19
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
20
pub struct Base64ShaCrypt;
21
22
impl Alphabet for Base64ShaCrypt {
23
    const BASE: u8 = b'.';
24
25
    const DECODER: &'static [DecodeStep] = &[
26
        DecodeStep::Range(b'.'..=b'9', -45),
27
        DecodeStep::Range(b'A'..=b'Z', -52),
28
        DecodeStep::Range(b'a'..=b'z', -58),
29
    ];
30
31
    const ENCODER: &'static [EncodeStep] =
32
        &[EncodeStep::Apply(b'9', 7), EncodeStep::Apply(b'Z', 6)];
33
34
    const PADDED: bool = false;
35
36
    type Unpadded = Self;
37
38
    #[inline(always)]
39
0
    fn decode_3bytes(src: &[u8], dst: &mut [u8]) -> i16 {
40
0
        debug_assert_eq!(src.len(), 4);
41
0
        debug_assert!(dst.len() >= 3, "dst too short: {}", dst.len());
42
43
0
        let c0 = Self::decode_6bits(src[0]);
44
0
        let c1 = Self::decode_6bits(src[1]);
45
0
        let c2 = Self::decode_6bits(src[2]);
46
0
        let c3 = Self::decode_6bits(src[3]);
47
48
0
        dst[0] = (c0 | ((c1 & 0x3) << 6)) as u8;
49
0
        dst[1] = ((c1 >> 2) | ((c2 & 0xF) << 4)) as u8;
50
0
        dst[2] = ((c2 >> 4) | (c3 << 2)) as u8;
51
52
0
        ((c0 | c1 | c2 | c3) >> 8) & 1
53
0
    }
54
55
    #[inline(always)]
56
0
    fn encode_3bytes(src: &[u8], dst: &mut [u8]) {
57
0
        debug_assert_eq!(src.len(), 3);
58
0
        debug_assert!(dst.len() >= 4, "dst too short: {}", dst.len());
59
60
0
        let b0 = src[0] as i16;
61
0
        let b1 = src[1] as i16;
62
0
        let b2 = src[2] as i16;
63
64
0
        dst[0] = Self::encode_6bits(b0 & 63);
65
0
        dst[1] = Self::encode_6bits(((b1 << 2) | (b0 >> 6)) & 63);
66
0
        dst[2] = Self::encode_6bits(((b2 << 4) | (b1 >> 4)) & 63);
67
0
        dst[3] = Self::encode_6bits(b2 >> 2);
68
0
    }
69
}