1 // This is a modification of base32 encoding to support the zbase32 alphabet.
2 // The original piece of software can be found at https://crates.io/crates/base32(v0.4.0)
3 // The original portions of this software are Copyright (c) 2015 The base32 Developers
5 // This file is licensed under either of
6 // Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) or
7 // MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.
10 use crate::prelude::*;
12 /// RFC4648 encoding table
13 const RFC4648_ALPHABET: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
15 /// Zbase encoding alphabet
16 const ZBASE_ALPHABET: &'static [u8] = b"ybndrfg8ejkmcpqxot1uwisza345h769";
18 /// RFC4648 decoding table
19 const RFC4648_INV_ALPHABET: [i8; 43] = [
20 -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,
21 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
24 /// Zbase decoding table
25 const ZBASE_INV_ALPHABET: [i8; 43] = [
26 -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,
27 21, 9, 10, -1, 11, 2, 16, 13, 14, 4, 22, 17, 19, -1, 20, 15, 0, 23,
30 /// Alphabet used for encoding and decoding.
31 #[derive(Copy, Clone)]
35 /// Whether to use padding.
43 /// Encode bytes into a base32 string.
44 pub fn encode(&self, data: &[u8]) -> String {
45 // output_length is calculated as follows:
46 // / 5 divides the data length by the number of bits per chunk (5),
47 // * 8 multiplies the result by the number of characters per chunk (8).
48 // + 4 rounds up to the nearest character.
49 let output_length = (data.len() * 8 + 4) / 5;
50 let mut ret = match self {
51 Self::RFC4648 { padding } => {
52 let mut ret = Self::encode_data(data, RFC4648_ALPHABET);
55 for i in output_length..len {
59 return String::from_utf8(ret).expect("Invalid UTF-8");
64 Self::encode_data(data, ZBASE_ALPHABET)
67 ret.truncate(output_length);
70 assert_eq!(ret.capacity(), (data.len() + 4) / 5 * 8);
72 String::from_utf8(ret).expect("Invalid UTF-8")
75 /// Decode a base32 string into a byte vector.
76 pub fn decode(&self, data: &str) -> Result<Vec<u8>, ()> {
77 let data = data.as_bytes();
78 let (data, alphabet) = match self {
79 Self::RFC4648 { padding } => {
80 let mut unpadded_data_length = data.len();
82 if data.len() % 8 != 0 { return Err(()); }
83 data.iter().rev().take(6).for_each(|&c| {
85 unpadded_data_length -= 1;
89 (&data[..unpadded_data_length], RFC4648_INV_ALPHABET)
92 (data, ZBASE_INV_ALPHABET)
95 // If the string has more characters than are required to alphabet_encode the number of bytes
96 // decodable, treat the string as invalid.
97 match data.len() % 8 { 1|3|6 => return Err(()), _ => {} }
98 Ok(Self::decode_data(data, alphabet)?)
101 /// Encode a byte slice into a base32 string.
102 fn encode_data(data: &[u8], alphabet: &'static [u8]) -> Vec<u8> {
103 // cap is calculated as follows:
104 // / 5 divides the data length by the number of bits per chunk (5),
105 // * 8 multiplies the result by the number of characters per chunk (8).
106 // + 4 rounds up to the nearest character.
107 let cap = (data.len() + 4) / 5 * 8;
108 let mut ret = Vec::with_capacity(cap);
109 for chunk in data.chunks(5) {
110 let mut buf = [0u8; 5];
111 for (i, &b) in chunk.iter().enumerate() {
114 ret.push(alphabet[((buf[0] & 0xF8) >> 3) as usize]);
115 ret.push(alphabet[(((buf[0] & 0x07) << 2) | ((buf[1] & 0xC0) >> 6)) as usize]);
116 ret.push(alphabet[((buf[1] & 0x3E) >> 1) as usize]);
117 ret.push(alphabet[(((buf[1] & 0x01) << 4) | ((buf[2] & 0xF0) >> 4)) as usize]);
118 ret.push(alphabet[(((buf[2] & 0x0F) << 1) | (buf[3] >> 7)) as usize]);
119 ret.push(alphabet[((buf[3] & 0x7C) >> 2) as usize]);
120 ret.push(alphabet[(((buf[3] & 0x03) << 3) | ((buf[4] & 0xE0) >> 5)) as usize]);
121 ret.push(alphabet[(buf[4] & 0x1F) as usize]);
124 assert_eq!(ret.capacity(), cap);
129 fn decode_data(data: &[u8], alphabet: [i8; 43]) -> Result<Vec<u8>, ()> {
130 // cap is calculated as follows:
131 // / 8 divides the data length by the number of characters per chunk (8),
132 // * 5 multiplies the result by the number of bits per chunk (5),
133 // + 7 rounds up to the nearest byte.
134 let cap = (data.len() + 7) / 8 * 5;
135 let mut ret = Vec::with_capacity(cap);
136 for chunk in data.chunks(8) {
137 let mut buf = [0u8; 8];
138 for (i, &c) in chunk.iter().enumerate() {
139 match alphabet.get(c.to_ascii_uppercase().wrapping_sub(b'0') as usize) {
140 Some(&-1) | None => return Err(()),
141 Some(&value) => buf[i] = value as u8,
144 ret.push((buf[0] << 3) | (buf[1] >> 2));
145 ret.push((buf[1] << 6) | (buf[2] << 1) | (buf[3] >> 4));
146 ret.push((buf[3] << 4) | (buf[4] >> 1));
147 ret.push((buf[4] << 7) | (buf[5] << 2) | (buf[6] >> 3));
148 ret.push((buf[6] << 5) | buf[7]);
150 let output_length = data.len() * 5 / 8;
151 for c in ret.drain(output_length..) {
153 // If the original string had any bits set at positions outside of the encoded data,
154 // treat the string as invalid.
159 // Check that our capacity calculation doesn't under-shoot in fuzzing
161 assert_eq!(ret.capacity(), cap);
170 const ZBASE32_TEST_DATA: &[(&str, &[u8])] = &[
174 ("tqrey", &[0x8b, 0x88, 0x80]),
175 ("6n9hq", &[0xf0, 0xbf, 0xc7]),
176 ("4t7ye", &[0xd4, 0x7a, 0x04]),
177 ("6im5sdy", &[0xf5, 0x57, 0xbb, 0x0c]),
178 ("ybndrfg8ejkmcpqxot1uwisza345h769", &[0x00, 0x44, 0x32, 0x14, 0xc7, 0x42, 0x54, 0xb6,
179 0x35, 0xcf, 0x84, 0x65, 0x3a, 0x56, 0xd7, 0xc6,
180 0x75, 0xbe, 0x77, 0xdf])
184 fn test_zbase32_encode() {
185 for &(zbase32, data) in ZBASE32_TEST_DATA {
186 assert_eq!(Alphabet::ZBase32.encode(data), zbase32);
191 fn test_zbase32_decode() {
192 for &(zbase32, data) in ZBASE32_TEST_DATA {
193 assert_eq!(Alphabet::ZBase32.decode(zbase32).unwrap(), data);
198 fn test_decode_wrong() {
199 const WRONG_DATA: &[&str] = &["00", "l1", "?", "="];
200 for &data in WRONG_DATA {
201 match Alphabet::ZBase32.decode(data) {
202 Ok(_) => assert!(false, "Data shouldn't be decodable"),
203 Err(_) => assert!(true),
208 const RFC4648_NON_PADDED_TEST_VECTORS: &[(&[u8], &[u8])] = &[
209 (&[0xF8, 0x3E, 0x7F, 0x83, 0xE7], b"7A7H7A7H"),
210 (&[0x77, 0xC1, 0xF7, 0x7C, 0x1F], b"O7A7O7A7"),
211 (&[0xF8, 0x3E, 0x7F, 0x83, 0xE7], b"7A7H7A7H"),
212 (&[0x77, 0xC1, 0xF7, 0x7C, 0x1F], b"O7A7O7A7"),
215 const RFC4648_TEST_VECTORS: &[(&[u8], &str)] = &[
219 (b"foo", "MZXW6==="),
220 (b"foob", "MZXW6YQ="),
221 (b"fooba", "MZXW6YTB"),
222 (b"foobar", "MZXW6YTBOI======"),
223 (&[0xF8, 0x3E, 0x7F, 0x83], "7A7H7AY="),
227 fn test_rfc4648_encode() {
228 for (input, encoded) in RFC4648_TEST_VECTORS {
229 assert_eq!(&Alphabet::RFC4648 { padding: true }.encode(input), encoded);
232 for (input, encoded) in RFC4648_NON_PADDED_TEST_VECTORS {
233 assert_eq!(&Alphabet::RFC4648 { padding: false }.encode(input).as_bytes(), encoded);
238 fn test_rfc4648_decode() {
239 for (input, encoded) in RFC4648_TEST_VECTORS {
240 let res = &Alphabet::RFC4648 { padding: true }.decode(encoded).unwrap();
241 assert_eq!(&res[..], &input[..]);
244 for (input, encoded) in RFC4648_NON_PADDED_TEST_VECTORS {
245 let res = &Alphabet::RFC4648 { padding: false }.decode(std::str::from_utf8(encoded).unwrap()).unwrap();
246 assert_eq!(&res[..], &input[..]);
252 let num_padding = [0, 6, 4, 3, 1];
254 let encoded = Alphabet::RFC4648 { padding: true }.encode(
255 (0..(i as u8)).collect::<Vec<u8>>().as_ref()
257 assert_eq!(encoded.len(), 8);
258 for j in 0..(num_padding[i % 5]) {
259 assert_eq!(encoded.as_bytes()[encoded.len() - j - 1], b'=');
261 for j in 0..(8 - num_padding[i % 5]) {
262 assert!(encoded.as_bytes()[j] != b'=');
268 fn test_decode_rfc4648_errors() {
269 assert!(Alphabet::RFC4648 { padding: false }.decode("abc2def===").is_err()); // Invalid char because padding is disabled
270 assert!(Alphabet::RFC4648 { padding: true }.decode("abc2def===").is_err()); // Invalid length
271 assert!(Alphabet::RFC4648 { padding: true }.decode("MZX=6YTB").is_err()); // Invalid char