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
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use crate::encoding;
use crate::error::Result;

pub(crate) trait BaseCodec {
    /// Encode with the given byte slice.
    fn encode<I: AsRef<[u8]>>(input: I) -> String;

    /// Decode with the given string.
    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>>;
}

/// Identity, 8-bit binary (encoder and decoder keeps data unmodified).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Identity;

impl BaseCodec for Identity {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        String::from_utf8(input.as_ref().to_vec()).expect("input must be valid UTF-8 bytes")
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(input.as_ref().as_bytes().to_vec())
    }
}

/// Base2 (alphabet: 01).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base2;

impl BaseCodec for Base2 {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE2.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE2.decode(input.as_ref().as_bytes())?)
    }
}

/// Base8 (alphabet: 01234567).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base8;

impl BaseCodec for Base8 {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE8.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE8.decode(input.as_ref().as_bytes())?)
    }
}

/// Base10 (alphabet: 0123456789).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base10;

impl BaseCodec for Base10 {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        base_x::encode(encoding::BASE10, input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(base_x::decode(encoding::BASE10, input.as_ref())?)
    }
}

/// Base16 lower hexadecimal (alphabet: 0123456789abcdef).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base16Lower;

impl BaseCodec for Base16Lower {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE16_LOWER.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE16_LOWER.decode(input.as_ref().as_bytes())?)
    }
}

/// Base16 upper hexadecimal (alphabet: 0123456789ABCDEF).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base16Upper;

impl BaseCodec for Base16Upper {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE16_UPPER.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE16_UPPER.decode(input.as_ref().as_bytes())?)
    }
}

/// Base32, rfc4648 no padding (alphabet: abcdefghijklmnopqrstuvwxyz234567).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base32Lower;

impl BaseCodec for Base32Lower {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE32_NOPAD_LOWER.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE32_NOPAD_LOWER.decode(input.as_ref().as_bytes())?)
    }
}

/// Base32, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base32Upper;

impl BaseCodec for Base32Upper {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE32_NOPAD_UPPER.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE32_NOPAD_UPPER.decode(input.as_ref().as_bytes())?)
    }
}

/// Base32, rfc4648 with padding (alphabet: abcdefghijklmnopqrstuvwxyz234567).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base32PadLower;

impl BaseCodec for Base32PadLower {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE32_PAD_LOWER.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE32_PAD_LOWER.decode(input.as_ref().as_bytes())?)
    }
}

/// Base32, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base32PadUpper;

impl BaseCodec for Base32PadUpper {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE32_PAD_UPPER.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE32_PAD_UPPER.decode(input.as_ref().as_bytes())?)
    }
}

/// Base32hex, rfc4648 no padding (alphabet: 0123456789abcdefghijklmnopqrstuv).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base32HexLower;

impl BaseCodec for Base32HexLower {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE32HEX_NOPAD_LOWER.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE32HEX_NOPAD_LOWER.decode(input.as_ref().as_bytes())?)
    }
}

/// Base32hex, rfc4648 no padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base32HexUpper;

impl BaseCodec for Base32HexUpper {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE32HEX_NOPAD_UPPER.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE32HEX_NOPAD_UPPER.decode(input.as_ref().as_bytes())?)
    }
}

/// Base32hex, rfc4648 with padding (alphabet: 0123456789abcdefghijklmnopqrstuv).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base32HexPadLower;

impl BaseCodec for Base32HexPadLower {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE32HEX_PAD_LOWER.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE32HEX_PAD_LOWER.decode(input.as_ref().as_bytes())?)
    }
}

/// Base32hex, rfc4648 with padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base32HexPadUpper;

impl BaseCodec for Base32HexPadUpper {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE32HEX_PAD_UPPER.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE32HEX_PAD_UPPER.decode(input.as_ref().as_bytes())?)
    }
}

/// z-base-32 (used by Tahoe-LAFS) (alphabet: ybndrfg8ejkmcpqxot1uwisza345h769).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base32Z;

impl BaseCodec for Base32Z {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE32Z.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE32Z.decode(input.as_ref().as_bytes())?)
    }
}

/// Base58 flicker (alphabet: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base58Flickr;

impl BaseCodec for Base58Flickr {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        base_x::encode(encoding::BASE58_FLICKR, input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(base_x::decode(encoding::BASE58_FLICKR, input.as_ref())?)
    }
}

/// Base58 bitcoin (alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base58Btc;

impl BaseCodec for Base58Btc {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        base_x::encode(encoding::BASE58_BITCOIN, input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(base_x::decode(encoding::BASE58_BITCOIN, input.as_ref())?)
    }
}

/// Base64, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base64;

impl BaseCodec for Base64 {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE64_NOPAD.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE64_NOPAD.decode(input.as_ref().as_bytes())?)
    }
}

/// Base64, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base64Pad;

impl BaseCodec for Base64Pad {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE64_PAD.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE64_PAD.decode(input.as_ref().as_bytes())?)
    }
}

/// Base64 url, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base64Url;

impl BaseCodec for Base64Url {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE64URL_NOPAD.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE64URL_NOPAD.decode(input.as_ref().as_bytes())?)
    }
}

/// Base64 url, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_).
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base64UrlPad;

impl BaseCodec for Base64UrlPad {
    fn encode<I: AsRef<[u8]>>(input: I) -> String {
        encoding::BASE64URL_PAD.encode(input.as_ref())
    }

    fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
        Ok(encoding::BASE64URL_PAD.decode(input.as_ref().as_bytes())?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_identity() {
        assert_eq!(Identity::encode(b"foo"), "foo");
        assert_eq!(Identity::decode("foo").unwrap(), b"foo".to_vec());
    }

    #[test]
    fn test_base2() {
        assert_eq!(Base2::encode(b"foo"), "011001100110111101101111");
        assert_eq!(
            Base2::decode("011001100110111101101111").unwrap(),
            b"foo".to_vec()
        );
    }

    #[test]
    fn test_base8() {
        assert_eq!(Base8::encode(b"foo"), "31467557");
        assert_eq!(Base8::decode("31467557").unwrap(), b"foo".to_vec());
    }

    #[test]
    fn test_base10() {
        assert_eq!(Base10::encode(b"foo"), "6713199");
        assert_eq!(Base10::decode("6713199").unwrap(), b"foo".to_vec());
    }

    #[test]
    fn test_base16() {
        assert_eq!(Base16Lower::encode(b"foo"), "666f6f");
        assert_eq!(Base16Lower::decode("666f6f").unwrap(), b"foo".to_vec());

        assert_eq!(Base16Upper::encode(b"foo"), "666F6F");
        assert_eq!(Base16Upper::decode("666F6F").unwrap(), b"foo".to_vec());
    }

    #[test]
    fn test_base32() {
        assert_eq!(Base32Lower::encode(b"foo"), "mzxw6");
        assert_eq!(Base32Lower::decode("mzxw6").unwrap(), b"foo".to_vec());

        assert_eq!(Base32Upper::encode(b"foo"), "MZXW6");
        assert_eq!(Base32Upper::decode("MZXW6").unwrap(), b"foo".to_vec());

        assert_eq!(Base32HexLower::encode(b"foo"), "cpnmu");
        assert_eq!(Base32HexLower::decode("cpnmu").unwrap(), b"foo".to_vec());

        assert_eq!(Base32HexUpper::encode(b"foo"), "CPNMU");
        assert_eq!(Base32HexUpper::decode("CPNMU").unwrap(), b"foo".to_vec());
    }

    #[test]
    fn test_base32_padding() {
        assert_eq!(Base32PadLower::encode(b"foo"), "mzxw6===");
        assert_eq!(Base32PadLower::decode("mzxw6===").unwrap(), b"foo".to_vec());

        assert_eq!(Base32PadUpper::encode(b"foo"), "MZXW6===");
        assert_eq!(Base32PadUpper::decode("MZXW6===").unwrap(), b"foo".to_vec());

        assert_eq!(Base32HexPadLower::encode(b"foo"), "cpnmu===");
        assert_eq!(
            Base32HexPadLower::decode("cpnmu===").unwrap(),
            b"foo".to_vec()
        );

        assert_eq!(Base32HexPadUpper::encode(b"foo"), "CPNMU===");
        assert_eq!(
            Base32HexPadUpper::decode("CPNMU===").unwrap(),
            b"foo".to_vec()
        );
    }

    #[test]
    fn test_base32z() {
        assert_eq!(Base32Z::encode(b"foo"), "c3zs6");
        assert_eq!(Base32Z::decode("c3zs6").unwrap(), b"foo".to_vec());
    }

    #[test]
    fn test_base58() {
        assert_eq!(Base58Flickr::encode(b"foo"), "ApAP");
        assert_eq!(Base58Flickr::decode("ApAP").unwrap(), b"foo".to_vec());

        assert_eq!(Base58Btc::encode(b"foo"), "bQbp");
        assert_eq!(Base58Btc::decode("bQbp").unwrap(), b"foo".to_vec());
    }

    #[test]
    fn test_base64() {
        assert_eq!(Base64::encode(b"foo"), "Zm9v");
        assert_eq!(Base64::decode("Zm9v").unwrap(), b"foo".to_vec());

        assert_eq!(Base64Url::encode(b"foo"), "Zm9v");
        assert_eq!(Base64Url::decode("Zm9v").unwrap(), b"foo".to_vec());
    }

    #[test]
    fn test_base64_padding() {
        assert_eq!(Base64Pad::encode(b"foopadding"), "Zm9vcGFkZGluZw==");
        assert_eq!(
            Base64Pad::decode("Zm9vcGFkZGluZw==").unwrap(),
            b"foopadding".to_vec()
        );

        assert_eq!(Base64UrlPad::encode(b"foopadding"), "Zm9vcGFkZGluZw==");
        assert_eq!(
            Base64UrlPad::decode("Zm9vcGFkZGluZw==").unwrap(),
            b"foopadding".to_vec()
        );
    }
}