Base32 编码与解码工具

解码 Base32

关于 Base32

什么是 Base32?

RFC 4648 Base32 把字节分成五位值,用 A-Z 和 2-7 表示,并以 = 补齐最后一个八字符块。解码器忽略空白和字母大小写,但会校验块长度、填充及未使用的填充位。

为什么使用 Base32?

Base32 适合人工输入的令牌,或偏好不区分大小写字母数字表的系统。它会使数据增大约 60%,不提供保密或检错能力,并且与 Crockford Base32、Base32hex 字母表不兼容。

JavaScript 中的 Base32

// npm install hi-base32
const base32 = require('hi-base32');

const encoded = base32.encode('Hello');
console.log(encoded);
// Output: JBSWY3DP

const decoded = Buffer.from(base32.decode.asBytes('JBSWY3DP')).toString('utf8');
console.log(decoded);
// Output: Hello
				

Go 中的 Base32

package main

import (
	"encoding/base32"
	"fmt"
)

func main() {
	encoded := base32.StdEncoding.EncodeToString([]byte("Hello"))
	fmt.Println(encoded)
	// Output: JBSWY3DP

	decoded, _ := base32.StdEncoding.DecodeString("JBSWY3DP")
	fmt.Println(string(decoded))
	// Output: Hello
}
				

PHP 中的 Base32

<?php
// composer require paragonie/constant_time_encoding
require 'vendor/autoload.php';

use ParagonIE\ConstantTime\Encoding;

$encoded = Encoding::base32EncodeUpper("Hello");
echo $encoded . "\n";
// Output: JBSWY3DP

$decoded = Encoding::base32DecodeUpper("JBSWY3DP");
echo $decoded . "\n";
// Output: Hello