fix Base32

This commit is contained in:
Looly 2022-03-20 14:21:10 +08:00
parent a1df46f4bd
commit ca5732e6df
4 changed files with 27 additions and 8 deletions

View File

@ -2,13 +2,14 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.0 (2022-03-19)
# 5.8.0 (2022-03-20)
### ❌不兼容特性
* 【db 】 【不向下兼容】增加MongoDB4.x支持pr#568@Gitee
* 【json 】 【可能兼容问题】修改JSONObject结构继承自MapWrapper
* 【core 】 【可能兼容问题】BeanCopier重构新建XXXCopier删除XXXValueProvider
* 【core 】 【可能兼容问题】URLEncoder废弃URLEncoderUtil使用RFC3986
* 【core 】 【可能兼容问题】增加Base32.encode不足位数补=
### 🐣新特性
* 【http 】 HttpRequest.form采用TableMap方式issue#I4W427@Gitee

View File

@ -79,7 +79,12 @@ public class Base32 {
base32.append(BASE32_CHARS.charAt(digit));
}
return StrUtil.fillAfter(base32.toString(), '=', encodeLen);
// 末尾补充不足长度的
while(base32.length() < encodeLen){
base32.append('=');
}
return base32.toString();
}
/**

View File

@ -104,7 +104,7 @@ public class Base62Codec implements Encoder<byte[], byte[]>, Decoder<byte[], byt
return convert(prepared, TARGET_BASE, STANDARD_BASE);
}
// --------------------------------------------------------------------------------------------------------------- Private method start
// region Private Methods
/**
* 按照字典转换bytes
*
@ -112,7 +112,7 @@ public class Base62Codec implements Encoder<byte[], byte[]>, Decoder<byte[], byt
* @param dictionary 字典
* @return 转换值
*/
private byte[] translate(byte[] indices, byte[] dictionary) {
private static byte[] translate(byte[] indices, byte[] dictionary) {
final byte[] translation = new byte[indices.length];
for (int i = 0; i < indices.length; i++) {
@ -130,7 +130,7 @@ public class Base62Codec implements Encoder<byte[], byte[]>, Decoder<byte[], byt
* @param targetBase 目标基准长度
* @return 计算结果
*/
private byte[] convert(byte[] message, int sourceBase, int targetBase) {
private static byte[] convert(byte[] message, int sourceBase, int targetBase) {
// 计算结果长度算法来自http://codegolf.stackexchange.com/a/21672
final int estimatedLength = estimateOutputLength(message.length, sourceBase, targetBase);
@ -175,8 +175,8 @@ public class Base62Codec implements Encoder<byte[], byte[]>, Decoder<byte[], byt
* @param targetBase 目标基准长度
* @return 估算长度
*/
private int estimateOutputLength(int inputLength, int sourceBase, int targetBase) {
private static int estimateOutputLength(int inputLength, int sourceBase, int targetBase) {
return (int) Math.ceil((Math.log(sourceBase) / Math.log(targetBase)) * inputLength);
}
// --------------------------------------------------------------------------------------------------------------- Private method end
// endregion
}

View File

@ -1,5 +1,6 @@
package cn.hutool.core.codec;
import cn.hutool.core.util.RandomUtil;
import org.junit.Assert;
import org.junit.Test;
@ -13,8 +14,20 @@ public class Base32Test {
String decodeStr = Base32.decodeStr(encode);
Assert.assertEquals(a, decodeStr);
}
decodeStr = Base32.decodeStr("4S6KNZNOW3TJRL7EXCAOJOFK5GOZ5ZNYXDUZLP7HTKCOLLMX46WKNZFYWI");
@Test
public void encodeAndDecodeRandomTest(){
String a = RandomUtil.randomString(RandomUtil.randomInt(1000));
String encode = Base32.encode(a);
String decodeStr = Base32.decodeStr(encode);
Assert.assertEquals(a, decodeStr);
}
@Test
public void decodeTest(){
String a = "伦家是一个非常长的字符串";
String decodeStr = Base32.decodeStr("4S6KNZNOW3TJRL7EXCAOJOFK5GOZ5ZNYXDUZLP7HTKCOLLMX46WKNZFYWI");
Assert.assertEquals(a, decodeStr);
}
}