progress_encode/
lib.rs

1#![forbid(unsafe_code)]
2#![deny(missing_debug_implementations, missing_copy_implementations)]
3#![warn(missing_docs, clippy::todo, clippy::unwrap_used)]
4#![cfg_attr(docsrs, feature(doc_cfg))]
5
6//! A Rust implementation of the Progress `ENCODE` function
7//!
8//! Progress' documentation on `ENCODE` can be found [here][psc]. This particular
9//! implementation is based on the [C# implementation by pvginkel][csharp]
10//!
11//! [psc]: https://docs.progress.com/bundle/openedge-abl-reference-117/page/ENCODE-function.html
12//! [csharp]: https://github.com/pvginkel/ProgressEncode
13
14static LOOKUP: &[u16] = &[
15    0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920, 51009, 1280, 50625, 50305,
16    1088, 52225, 3264, 3456, 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 51457,
17    2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217,
18    8000, 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 54849, 53761,
19    4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480, 12672, 61761, 13056, 62401, 62081,
20    12864, 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 15360, 64705, 64897, 15680,
21    65281, 16320, 16000, 65089, 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240,
22    59585, 59777, 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865,
23    60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 58049, 58241, 9024,
24    57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689,
25    42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288,
26    44609, 43521, 27328, 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040,
27    47873, 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808, 46081,
28    29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953, 29504, 45313, 29120,
29    28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 22400,
30    38721, 21760, 38337, 38017, 21568, 39937, 23744, 23936, 40257, 24320, 40897, 40577, 24128,
31    23040, 39617, 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200,
32    35777, 35457, 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985,
33    34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640, 33217, 32897,
34    16448,
35];
36
37/// Return a 16-character encoding of the `input` bytes.
38///
39/// ### Example usage
40///
41/// ```rust
42/// use progress_encode::encode;
43///
44/// let password = String::from("my-passw0rd");
45/// let encoded_password = encode(password.as_bytes());
46/// assert_eq!(encoded_password, "lEsdklcFaOOjlbma");
47/// ```
48pub fn encode(input: &[u8]) -> String {
49    let mut scratch: [u8; 16] = [0; 16];
50
51    let mut hash: u16 = 17;
52
53    for _ in 0..5 {
54        input.iter().enumerate().for_each(|(j, val)| {
55            scratch[15 - (j % 16)] ^= val;
56        });
57
58        (0..16).step_by(2).for_each(|j| {
59            hash = pr_hash(&scratch, hash);
60
61            scratch[j] = (hash & 0xff) as u8;
62            scratch[j + 1] = ((hash >> 8) & 0xff) as u8;
63        });
64    }
65
66    let mut target = String::new();
67
68    scratch.iter().for_each(|byte| {
69        let lower = (byte & 0x7f) as char;
70
71        if lower.is_ascii_uppercase() || lower.is_ascii_lowercase() {
72            target.push(lower);
73        } else {
74            target.push(((byte >> 4) + 0x61) as char);
75        }
76    });
77
78    target
79}
80
81#[inline]
82fn pr_hash(scratch: &[u8; 16], mut hash: u16) -> u16 {
83    scratch.iter().rev().for_each(|byte| {
84        hash = hash >> 8 ^ LOOKUP[(hash & 0xff) as usize] ^ LOOKUP[*byte as usize];
85    });
86    hash
87}
88
89#[cfg(test)]
90mod tests {
91    use serde::{Deserialize, Serialize};
92
93    use crate::encode;
94
95    #[derive(Debug, Clone, Deserialize, Serialize)]
96    struct TestCase {
97        encoded: String,
98        input: Vec<u8>,
99    }
100
101    #[test]
102    fn test_encode() {
103        let json = include_str!("tests.json");
104
105        let test_cases: Vec<TestCase> = serde_json::from_str(json).expect("Deserialize tests");
106
107        for (idx, test) in test_cases.iter().enumerate() {
108            println!("Test {idx}, expecting encoded value {}", test.encoded);
109            if let Ok(input_str) = String::from_utf8(test.input.clone()) {
110                println!("Input string: \"{input_str}\"");
111            }
112            assert_eq!(test.encoded, encode(&test.input));
113        }
114    }
115
116    #[test]
117    fn test_example_password() {
118        let password = String::from("my-passw0rd");
119        let encoded_password = encode(password.as_bytes());
120        assert_eq!(encoded_password, "lEsdklcFaOOjlbma");
121    }
122}