From 97758682207518c375a67803001d332d6533f270 Mon Sep 17 00:00:00 2001 From: micuncang Date: Wed, 16 Mar 2022 00:25:16 +0800 Subject: [PATCH] =?UTF-8?q?Base32=E6=B7=BB=E5=8A=A0"=3D"=E8=A1=A5=E7=A0=81?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/codec/Base32.java | 21 +++++++++++++------ .../java/cn/hutool/core/codec/Base32Test.java | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/codec/Base32.java b/hutool-core/src/main/java/cn/hutool/core/codec/Base32.java index 030bf6e66..f98619138 100644 --- a/hutool-core/src/main/java/cn/hutool/core/codec/Base32.java +++ b/hutool-core/src/main/java/cn/hutool/core/codec/Base32.java @@ -1,10 +1,10 @@ package cn.hutool.core.codec; -import java.nio.charset.Charset; - import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.StrUtil; +import java.nio.charset.Charset; + /** * Base32 - encodes and decodes RFC3548 Base32 (see http://www.faqs.org/rfcs/rfc3548.html )
* base32就是用32(2的5次方)个特定ASCII码来表示256个ASCII码。
@@ -30,6 +30,7 @@ public class Base32 { 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 'p', 'q', 'r', 's', 't', 'u', 'v', 'w' 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // 'x', 'y', 'z', '{', '|', '}', '~', 'DEL' }; + private static final int[] BASE32_FILL = {-1, 4, 1, 6, 3}; //----------------------------------------------------------------------------------------- encode /** @@ -43,10 +44,17 @@ public class Base32 { int digit; int currByte; int nextByte; - StringBuilder base32 = new StringBuilder((bytes.length + 7) * 8 / 5); + + int encodeLen = bytes.length * 8 / 5; + if (encodeLen != 0) { + encodeLen = encodeLen + 1 + BASE32_FILL[(bytes.length * 8) % 5]; + } + + StringBuilder base32 = new StringBuilder(encodeLen); while (i < bytes.length) { - currByte = (bytes[i] >= 0) ? bytes[i] : (bytes[i] + 256); // unsign + // unsign + currByte = (bytes[i] >= 0) ? bytes[i] : (bytes[i] + 256); /* Is the current digit going to span a byte boundary? */ if (index > 3) { @@ -71,7 +79,7 @@ public class Base32 { base32.append(BASE32_CHARS.charAt(digit)); } - return base32.toString(); + return StrUtil.fillAfter(base32.toString(), '=', encodeLen); } /** @@ -114,7 +122,8 @@ public class Base32 { */ public static byte[] decode(final String base32) { int i, index, lookup, offset, digit; - byte[] bytes = new byte[base32.length() * 5 / 8]; + int len = base32.endsWith("=") ? base32.indexOf("=") * 5 / 8 : base32.length() * 5 / 8; + byte[] bytes = new byte[len]; for (i = 0, index = 0, offset = 0; i < base32.length(); i++) { lookup = base32.charAt(i) - '0'; diff --git a/hutool-core/src/test/java/cn/hutool/core/codec/Base32Test.java b/hutool-core/src/test/java/cn/hutool/core/codec/Base32Test.java index 76313f085..b19d98762 100644 --- a/hutool-core/src/test/java/cn/hutool/core/codec/Base32Test.java +++ b/hutool-core/src/test/java/cn/hutool/core/codec/Base32Test.java @@ -9,7 +9,7 @@ public class Base32Test { public void encodeAndDecodeTest(){ String a = "伦家是一个非常长的字符串"; String encode = Base32.encode(a); - Assert.assertEquals("4S6KNZNOW3TJRL7EXCAOJOFK5GOZ5ZNYXDUZLP7HTKCOLLMX46WKNZFYWI", encode); + Assert.assertEquals("4S6KNZNOW3TJRL7EXCAOJOFK5GOZ5ZNYXDUZLP7HTKCOLLMX46WKNZFYWI======", encode); String decodeStr = Base32.decodeStr(encode); Assert.assertEquals(a, decodeStr);