pattern/
lib.rs

1const UPPER_CASE: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2const LOWER_CASE: &str = "abcdefghijklmnopqrstuvwxyz";
3const NUMBERS: &str = "0123456789";
4
5pub fn create(length: usize) -> String {
6    let mut payload: Vec<String> = Vec::new();
7    for c in UPPER_CASE.chars() {
8        for s in LOWER_CASE.chars() {
9            for n in NUMBERS.chars() {
10                if payload.len() < length {
11                    payload.push(c.to_string());
12                }
13                if payload.len() < length {
14                    payload.push(s.to_string());
15                }
16                if payload.len() < length {
17                    payload.push(n.to_string());    
18                }
19            }
20        }
21    }
22    payload.join("")
23}
24
25#[test]
26fn test_create() {
27    let expected = "Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab";
28    let actual = create(50);
29    assert_eq!(expected, actual);
30}
31
32
33pub fn offset(value: &str, flag: bool) -> Option<usize> {
34    let payload = create(20280);
35    if let Some(i) = payload.find(value) {
36        return Some(i);
37    } else {
38        if let Ok(h) = u64::from_str_radix(value, 16) {
39            if let Some(i) = payload.find(&hex2ascii(h, flag)) {
40                return Some(i);
41            }
42        }
43        return None;
44    }
45}
46
47#[test]
48fn test_offset() {
49    let expected = 26;
50    let actual = offset("8Aa9", false).unwrap();
51    assert_eq!(expected, actual);
52    let expected = 0;
53    let actual = offset("Aa0", false).unwrap();
54    assert_eq!(expected, actual);
55    let expected = 20277;
56    let actual = offset("Zz9", false).unwrap();
57    assert_eq!(expected, actual);
58}
59
60fn hex2ascii(hex: u64, bigendian_flag: bool) -> String {
61    let mut s: Vec<String> = Vec::new();
62    let mut h = hex;
63    while h != 0 {
64        s.push(
65            format!("{}", (h & 0xff) as u8 as char)
66        );
67        h >>= 8;
68    }
69    if bigendian_flag {
70        s.reverse();
71    }
72    s.join("")
73}
74
75#[test]
76fn test_hex2ascii() {
77    let expected = String::from("AAAA");
78    let actual = hex2ascii(0x41414141, false);
79    assert_eq!(expected, actual);
80
81    let expected = String::from("ABCD");
82    let actual = hex2ascii(0x44434241, false);
83    assert_eq!(expected, actual);
84
85    // Testing with big endian
86    let expected = String::from("ABCD");
87    let actual = hex2ascii(0x41424344, true);
88    assert_eq!(expected, actual);
89}