mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix Base64.isBase64
This commit is contained in:
parent
6a04f20fcf
commit
8f3011e793
@ -8,6 +8,7 @@
|
||||
### 🐣新特性
|
||||
* 【core 】 增加FieldsComparator(pr#374@Gitee)
|
||||
* 【core 】 FileUtil.del采用Files.delete实现
|
||||
* 【core 】 改进Base64.isBase64方法增加等号判断(issue#1710@Github)
|
||||
|
||||
### 🐞Bug修复
|
||||
|
||||
|
@ -322,7 +322,7 @@ public class Base64 {
|
||||
* @return 是否为Base64
|
||||
* @since 5.7.5
|
||||
*/
|
||||
public static boolean isBase64(String base64){
|
||||
public static boolean isBase64(CharSequence base64){
|
||||
return isBase64(StrUtil.utf8Bytes(base64));
|
||||
}
|
||||
|
||||
@ -334,7 +334,17 @@ public class Base64 {
|
||||
* @since 5.7.5
|
||||
*/
|
||||
public static boolean isBase64(byte[] base64Bytes){
|
||||
boolean hasPadding = false;
|
||||
for (byte base64Byte : base64Bytes) {
|
||||
if(hasPadding){
|
||||
if('=' != base64Byte){
|
||||
// 前一个字符是'=',则后边的字符都必须是'=',即'='只能都位于结尾
|
||||
return false;
|
||||
}
|
||||
} else if('=' == base64Byte){
|
||||
// 发现'=' 标记之
|
||||
hasPadding = true;
|
||||
}
|
||||
if (false == (Base64Decoder.isBase64Code(base64Byte) || isWhiteSpace(base64Byte))) {
|
||||
return false;
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ public class Base64Decoder {
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00-0f
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-1f
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, // 20-2f + - /
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, // 30-3f 0-9
|
||||
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -2, -1, -1, // 30-3f 0-9,-2的位置是'='
|
||||
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 40-4f A-O
|
||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, // 50-5f P-Z _
|
||||
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 60-6f a-o
|
||||
|
@ -16,7 +16,19 @@ public class Base64Test {
|
||||
|
||||
@Test
|
||||
public void isBase64Test(){
|
||||
Assert.assertTrue(Base64.isBase64(Base64.encode(RandomUtil.randomString(100))));
|
||||
Assert.assertTrue(Base64.isBase64(Base64.encode(RandomUtil.randomString(1000))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isBase64Test2(){
|
||||
String base64 = "dW1kb3MzejR3bmljM2J6djAyZzcwbWk5M213Nnk3cWQ3eDJwOHFuNXJsYmMwaXhxbmg0dmxrcmN0anRkbmd3\n" +
|
||||
"ZzcyZWFwanI2NWNneTg2dnp6cmJoMHQ4MHpxY2R6c3pjazZtaQ==";
|
||||
Assert.assertTrue(Base64.isBase64(base64));
|
||||
|
||||
// '=' 不位于末尾
|
||||
base64 = "dW1kb3MzejR3bmljM2J6=djAyZzcwbWk5M213Nnk3cWQ3eDJwOHFuNXJsYmMwaXhxbmg0dmxrcmN0anRkbmd3\n" +
|
||||
"ZzcyZWFwanI2NWNneTg2dnp6cmJoMHQ4MHpxY2R6c3pjazZtaQ=";
|
||||
Assert.assertFalse(Base64.isBase64(base64));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user