base32 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

base32 命令安装:


-bash/zsh: base32 command not found

#Debian
apt-get install coreutils

#Ubuntu
apt-get install coreutils

#Alpine
apk add coreutils

#Arch Linux
pacman -S coreutils

#Kali Linux
apt-get install coreutils

#CentOS
yum install coreutils

#Fedora
dnf install coreutils

#OS X
brew install coreutils

#Raspbian
apt-get install coreutils

#Docker
docker run cmd.cat/base32 base32

base32 命令补充说明:


base32 编码旨在以需要不区分大小写但不需要人类可读的形式,表示任意八位字节序列。

base32 编码的数据比原始数据大 60% 左右,Base32 另外一个缺点,是生成的字符串比 Base64 长约 20%,因为 Base32 使用 8 个 ASCII 字符去编码原数据中的 5 个字节数据,而 Base64 是使用 4 个 ASCII 字符去编码原数据中的 3 个字节数据。

base32 数据按照 RFC 4648 中 base64 字母表的描述进行编码。这使用字母表 AZ,后跟 2-7。由于与字母 O、I、B 和 G 的相似性,跳过了 0、1、8 和 9 ..当输入组中可用的输入位少于 40 个时,值为零的位将被添加(在右侧)以形成整数个 5 位组。数据末尾的填充使用 = 字符执行。

base32 解码时,除了正式 base64 字母表的字节外,输入还可能包含换行符。可以使用 --ignore-garbage 尝试从编码流中的任何其他非字母字节中恢复。

base32 的优点:


base32 是 coreutils(基本文件、Shell 和文本操作实用程序)项目的一部分,其优点如下:

  • base32 适合不区分大小写的文件系统,更利于人类口语交流或记忆。
  • base32 的结果,可以用作文件名,因为它不包含路径分隔符 / 等符号。
  • base32(较小的)输出字符集不区分大小写,排除了填充符号 = 的结果可以包含在URL中,而不编码任何字符。
  • 由于输出字符集避免了外观相似的不同字符对,例如 1l,因此可以以低错误率手动转录输出。
  • base32 输出适合用作 URL 字符串。

RFC 4648 Base32 字母表:


Base32 将任意字符串按照字节进行切分,并将每个字节对应的二进制值(不足8比特高位补0)串联起来,按照5比特一组进行切分,并将每组二进制值转换成十进制来对应32个可打印字符中的一个。
符号 符号 符号 符号
0 A 8 I 16 Q 24 Y
1 B 9 J 17 R 25 Z
2 C 10 K 18 S 26 2
3 D 11 L 19 T 27 3
4 E 12 M 20 U 28 4
5 F 13 N 21 V 29 5
6 G 14 O 22 W 30 6
7 H 15 P 23 X 31 7
填充 =

由于数据的二进制传输是按照8比特一组进行(即一个字节),因此 Base32 按 5 比特切分的二进制数据必须是 40 比特的倍数(5 和 8 的最小公倍数)。例如输入单字节字符 %,它对应的二进制值是 100101,前面补两个 0 变成 00100101(二进制值不足8比特的都要在高位加 0 直到 8 比特),从左侧开始按照5比特切分成两组:00100101,后一组不足 5 比特,则在末尾填充 0 直到 5 比特,变成 0010010100,这两组二进制数分别转换成十进制数,通过上述表格即可找到其对应的可打印字符 EU,但是这里只用到两组共 10 比特,还差 30 比特达到 40 比特,按照 5 比特一组还需 6 组,则在末尾填充 6 个 =。填充 = 符号的作用是方便一些程序的标准化运行,大多数情况下不添加也无关紧要,而且,在URL中使用时必须去掉 = 符号。

base32 命令语法:


 base32 [OPTION]... [FILE]

base32 命令选项:


Base32 encode or decode FILE, or standard input, to standard
output.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short
options too.

-d, --decode
      decode data

-i, --ignore-garbage
      when decoding, ignore non-alphabet characters

-w, --wrap=COLS
      wrap encoded lines after COLS character (default 76).  Use
      0 to disable line wrapping

--help display this help and exit

--version
      output version information and exit

The data are encoded as described for the base32 alphabet in RFC
4648.  When decoding, the input may contain newlines in addition
to the bytes of the formal base32 alphabet.  Use --ignore-garbage
to attempt to recover from any other non-alphabet bytes in the
encoded stream.

base32 命令参数:


目标文件

base32 命令实例:


base32 基本的 echo 和 piping 管道示例(base32 编码 / 解码):

$ echo 'this is some text I want to encode' | base32

ORUGS4ZANFZSA43PNVSSA5DFPB2CASJAO5QW45BAORXSAZLOMNXWIZIK

----------------

$ echo 'ORUGS4ZANFZSA43PNVSSA5DFPB2CASJAO5QW45BAORXSAZLOMNXWIZIK' | base32 -d
this is some text I want to encode

base32 编码文件:

base32 filename

base32 解码文件:

base32 -d filename

base32 从标准输入编码:

somecommand | base32

base32 从标准输入解码:

somecommand | base32 -d

base32 命令扩展阅读:




base32 命令评论