Allow(unused_imports) on prelude imports
[rust-lightning] / lightning / src / util / base32.rs
index ff30a024f54bcfb0d9b67743aba1f5c8abe5f654..e116dccbf59ad5bee31f1aa50bb7e2323f127774 100644 (file)
@@ -6,18 +6,27 @@
 // Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) or
 // MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.
 
-
+#[allow(unused)]
 use crate::prelude::*;
 
 /// RFC4648 encoding table
 const RFC4648_ALPHABET: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
 
+/// Zbase encoding alphabet
+const ZBASE_ALPHABET: &'static [u8] = b"ybndrfg8ejkmcpqxot1uwisza345h769";
+
 /// RFC4648 decoding table
 const RFC4648_INV_ALPHABET: [i8; 43] = [
        -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,
        9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
 ];
 
+/// Zbase decoding table
+const ZBASE_INV_ALPHABET: [i8; 43] = [
+       -1, 18, -1, 25, 26, 27, 30, 29, 7, 31, -1, -1, -1, -1, -1, -1, -1, 24, 1, 12, 3, 8, 5, 6, 28,
+       21, 9, 10, -1, 11, 2, 16, 13, 14, 4, 22, 17, 19, -1, 20, 15, 0, 23,
+];
+
 /// Alphabet used for encoding and decoding.
 #[derive(Copy, Clone)]
 pub enum Alphabet {
@@ -25,7 +34,9 @@ pub enum Alphabet {
        RFC4648 {
                /// Whether to use padding.
                padding: bool
-       }
+       },
+       /// Zbase32 encoding.
+       ZBase32
 }
 
 impl Alphabet {
@@ -48,7 +59,10 @@ impl Alphabet {
                                        return String::from_utf8(ret).expect("Invalid UTF-8");
                                }
                                ret
-                       }
+                       },
+                       Self::ZBase32 => {
+                               Self::encode_data(data, ZBASE_ALPHABET)
+                       },
                };
                ret.truncate(output_length);
 
@@ -73,6 +87,9 @@ impl Alphabet {
                                        });
                                }
                                (&data[..unpadded_data_length], RFC4648_INV_ALPHABET)
+                       },
+                       Self::ZBase32 => {
+                               (data, ZBASE_INV_ALPHABET)
                        }
                };
                // If the string has more characters than are required to alphabet_encode the number of bytes
@@ -150,6 +167,44 @@ impl Alphabet {
 mod tests {
        use super::*;
 
+       const ZBASE32_TEST_DATA: &[(&str, &[u8])] = &[
+               ("", &[]),
+               ("yy", &[0x00]),
+               ("oy", &[0x80]),
+               ("tqrey", &[0x8b, 0x88, 0x80]),
+               ("6n9hq", &[0xf0, 0xbf, 0xc7]),
+               ("4t7ye", &[0xd4, 0x7a, 0x04]),
+               ("6im5sdy", &[0xf5, 0x57, 0xbb, 0x0c]),
+               ("ybndrfg8ejkmcpqxot1uwisza345h769", &[0x00, 0x44, 0x32, 0x14, 0xc7, 0x42, 0x54, 0xb6,
+               0x35, 0xcf, 0x84, 0x65, 0x3a, 0x56, 0xd7, 0xc6,
+               0x75, 0xbe, 0x77, 0xdf])
+       ];
+
+       #[test]
+       fn test_zbase32_encode() {
+               for &(zbase32, data) in ZBASE32_TEST_DATA {
+                       assert_eq!(Alphabet::ZBase32.encode(data), zbase32);
+               }
+       }
+
+       #[test]
+       fn test_zbase32_decode() {
+               for &(zbase32, data) in ZBASE32_TEST_DATA {
+                       assert_eq!(Alphabet::ZBase32.decode(zbase32).unwrap(), data);
+               }
+       }
+
+       #[test]
+       fn test_decode_wrong() {
+               const WRONG_DATA: &[&str] = &["00", "l1", "?", "="];
+               for &data in WRONG_DATA {
+                       match Alphabet::ZBase32.decode(data) {
+                               Ok(_) => assert!(false, "Data shouldn't be decodable"),
+                               Err(_) => assert!(true),
+                       }
+               }
+       }
+
        const RFC4648_NON_PADDED_TEST_VECTORS: &[(&[u8], &[u8])] = &[
                (&[0xF8, 0x3E, 0x7F, 0x83, 0xE7], b"7A7H7A7H"),
                (&[0x77, 0xC1, 0xF7, 0x7C, 0x1F], b"O7A7O7A7"),