Intro To 'base32' Command In Linux
2023-06-17 - By Robert Elder
I use the 'base32' command to encode or decode from base32 encoding:
echo "base32" | base32
MJQXGZJTGIFA====
Base 32 Encoding & Decoding
If I pipe this hello world statement into the 'base32' command, I'll see the following string of base 32 characters:
echo "Hello World!" | base32
JBSWY3DPEBLW64TMMQQQU===
I can also decode this same base 32 encoded string by using the '-d' flag:
echo "JBSWY3DPEBLW64TMMQQQU===" | base32 -d
Hello World!
RFC 4648
The 'base32' character set is defined in request for comment 4648. In this table, you can see that the 'base32' encoding uses all 26 uppercase letters of the alphabet and the six numerical digits from 2 to 7:
RFC 4648 Base-N Encodings October 2006
Each 5-bit group is used as an index into an array of 32 printable
characters. The character referenced by the index is placed in the
output string. These characters, identified in Table 3, below, are
selected from US-ASCII digits and uppercase letters.
Table 3: The Base 32 Alphabet
Value Encoding Value Encoding Value Encoding Value Encoding
0 A 9 J 18 S 27 3
1 B 10 K 19 T 28 4
2 C 11 L 20 U 29 5
3 D 12 M 21 V 30 6
4 E 13 N 22 W 31 7
5 F 14 O 23 X
6 G 15 P 24 Y (pad) =
7 H 16 Q 25 Z
8 I 17 R 26 2
Additionally, an equal sign character is used for padding.
Purpose Of Base 32 Encoding
The 'base32' command is useful for reliably encoding non-printable binary characters into a form that can't be easily corrupted by other programs:
echo -n -e '\x00\x01\x02\x95\x96\x97' | xxd
echo -n -e '\x00\x01\x02\x95\x96\x97' | base32 | base32 -d | xxd
00000000: 0001 0295 9697 ......
00000000: 0001 0295 9697 ......
Base 32 Vs. Base 64
Base 32 is immune to information loss from capitalization changes. Here is a command that will capitalize all of the letters in a base32 encoded string and then try to decode it:
echo 'Hello World!' | base32 | tr 'A-Z' 'a-z' | tr 'a-z' 'A-Z' | base32 -d
Hello World!
As you can see from the output above, the original input string can still be recovered.
However, for base 64 encoding this is not the case:
echo 'Hello World!' | base64 | tr 'A-Z' 'a-z' | tr 'a-z' 'A-Z' | base64 -d
HeRoWoX
As you can see from the output above, the string has become corrupted.
Disadvantage Of Base 32
One disadvantage of base 32 is that it will use more characters than base 64 when representing the same piece of data. Let's use this command to sample 100 bytes of random data into the file 'some-data':
head -c 100 /dev/urandom > some-data
Now, here is that data encoded as a base 32 encoded string:
cat some-data | base32
BLQMD4KSP62FFTZ7VQ5ZUP5QFB6WAG6O2VNNIPMOPLSZVCFF4FXAWPGFIUDTM4D23NP25NEZAVWK
AC2XYIJ3BH2AVS7EEJSTJNVKSKS5QA32W3Q6XUNKITUIWL23WPGYCURKZW4QDXAYC64BJO4E4HGI
EVVXZ4OZ
and, here is that same data encoded as a base 64 encoded string:
cat some-data | base64
CuDB8VJ/tFLPP6w7mj+wKH1gG87VWtQ9jnrlmoil4W4LPMVFBzZwettfrrSZBWygC1fCE7CfQKy+
QiZTS2qpKl2AN6tuHr0apE6IsvW7PNgVIqzbkB3BgXuBS7hOHMgla3zx2Q==
As you can see from the above output, the base 64 encoded string is much shorter than the base 32 encoded string.
And that's why the 'base32' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|