mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-03 20:27:58 +08:00
change Base64 impl
This commit is contained in:
parent
7b4fd66d81
commit
f52ec39d44
@ -159,7 +159,7 @@ public class Base64 {
|
||||
* @since 3.0.6
|
||||
*/
|
||||
public static String encodeUrlSafe(byte[] source) {
|
||||
return java.util.Base64.getUrlEncoder().encodeToString(source);
|
||||
return java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(source);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -6,15 +6,18 @@ import cn.hutool.core.util.StrUtil;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Base64编码
|
||||
* Base64编码<br>
|
||||
* TODO 6.x移除此类,使用JDK自身
|
||||
*
|
||||
* @author looly
|
||||
* @since 3.2.0
|
||||
*/
|
||||
public class Base64Encoder{
|
||||
public class Base64Encoder {
|
||||
|
||||
private static final Charset DEFAULT_CHARSET = CharsetUtil.CHARSET_UTF_8;
|
||||
/** 标准编码表 */
|
||||
/**
|
||||
* 标准编码表
|
||||
*/
|
||||
private static final byte[] STANDARD_ENCODE_TABLE = { //
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', //
|
||||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', //
|
||||
@ -25,7 +28,9 @@ public class Base64Encoder{
|
||||
'w', 'x', 'y', 'z', '0', '1', '2', '3', //
|
||||
'4', '5', '6', '7', '8', '9', '+', '/' //
|
||||
};
|
||||
/** URL安全的编码表,将 + 和 / 替换为 - 和 _ */
|
||||
/**
|
||||
* URL安全的编码表,将 + 和 / 替换为 - 和 _
|
||||
*/
|
||||
private static final byte[] URL_SAFE_ENCODE_TABLE = { //
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', //
|
||||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', //
|
||||
@ -38,10 +43,11 @@ public class Base64Encoder{
|
||||
};
|
||||
|
||||
// -------------------------------------------------------------------- encode
|
||||
|
||||
/**
|
||||
* 编码为Base64,非URL安全的
|
||||
*
|
||||
* @param arr 被编码的数组
|
||||
* @param arr 被编码的数组
|
||||
* @param lineSep 在76个char之后是CRLF还是EOF
|
||||
* @return 编码后的bytes
|
||||
*/
|
||||
@ -52,7 +58,7 @@ public class Base64Encoder{
|
||||
/**
|
||||
* 编码为Base64,URL安全的
|
||||
*
|
||||
* @param arr 被编码的数组
|
||||
* @param arr 被编码的数组
|
||||
* @param lineSep 在76个char之后是CRLF还是EOF
|
||||
* @return 编码后的bytes
|
||||
* @since 3.0.6
|
||||
@ -85,7 +91,7 @@ public class Base64Encoder{
|
||||
/**
|
||||
* base64编码
|
||||
*
|
||||
* @param source 被编码的base64字符串
|
||||
* @param source 被编码的base64字符串
|
||||
* @param charset 字符集
|
||||
* @return 被加密后的字符串
|
||||
*/
|
||||
@ -96,7 +102,7 @@ public class Base64Encoder{
|
||||
/**
|
||||
* base64编码,URL安全的
|
||||
*
|
||||
* @param source 被编码的base64字符串
|
||||
* @param source 被编码的base64字符串
|
||||
* @param charset 字符集
|
||||
* @return 被加密后的字符串
|
||||
* @since 3.0.6
|
||||
@ -130,9 +136,9 @@ public class Base64Encoder{
|
||||
* 编码为Base64字符串<br>
|
||||
* 如果isMultiLine为{@code true},则每76个字符一个换行符,否则在一行显示
|
||||
*
|
||||
* @param arr 被编码的数组
|
||||
* @param arr 被编码的数组
|
||||
* @param isMultiLine 在76个char之后是CRLF还是EOF
|
||||
* @param isUrlSafe 是否使用URL安全字符,在URL Safe模式下,=为URL中的关键字符,不需要补充。空余的byte位要去掉,一般为{@code false}
|
||||
* @param isUrlSafe 是否使用URL安全字符,在URL Safe模式下,=为URL中的关键字符,不需要补充。空余的byte位要去掉,一般为{@code false}
|
||||
* @return 编码后的bytes
|
||||
* @since 5.7.2
|
||||
*/
|
||||
@ -144,9 +150,9 @@ public class Base64Encoder{
|
||||
* 编码为Base64<br>
|
||||
* 如果isMultiLine为{@code true},则每76个字符一个换行符,否则在一行显示
|
||||
*
|
||||
* @param arr 被编码的数组
|
||||
* @param arr 被编码的数组
|
||||
* @param isMultiLine 在76个char之后是CRLF还是EOF
|
||||
* @param isUrlSafe 是否使用URL安全字符,在URL Safe模式下,=为URL中的关键字符,不需要补充。空余的byte位要去掉,一般为{@code false}
|
||||
* @param isUrlSafe 是否使用URL安全字符,在URL Safe模式下,=为URL中的关键字符,不需要补充。空余的byte位要去掉,一般为{@code false}
|
||||
* @return 编码后的bytes
|
||||
*/
|
||||
public static byte[] encode(byte[] arr, boolean isMultiLine, boolean isUrlSafe) {
|
||||
@ -166,7 +172,7 @@ public class Base64Encoder{
|
||||
|
||||
byte[] encodeTable = isUrlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE;
|
||||
|
||||
for (int s = 0, d = 0, cc = 0; s < evenlen;) {
|
||||
for (int s = 0, d = 0, cc = 0; s < evenlen; ) {
|
||||
int i = (arr[s++] & 0xff) << 16 | (arr[s++] & 0xff) << 8 | (arr[s++] & 0xff);
|
||||
|
||||
dest[d++] = encodeTable[(i >>> 18) & 0x3f];
|
||||
|
@ -71,16 +71,6 @@ public class Base64Test {
|
||||
Assert.assertEquals(a, decodeStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void urlSafeEncodeAndDecodeTest() {
|
||||
String a = "广州伦家需要安全感55";
|
||||
String encode = StrUtil.utf8Str(Base64.encodeUrlSafe(StrUtil.utf8Bytes(a), false));
|
||||
Assert.assertEquals("5bm_5bee5Lym5a626ZyA6KaB5a6J5YWo5oSfNTU", encode);
|
||||
|
||||
String decodeStr = Base64.decodeStr(encode);
|
||||
Assert.assertEquals(a, decodeStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeAndDecodeGbkTest(){
|
||||
String orderDescription = "订购成功立即生效,30天内可观看专区中除单独计费影片外的所有内容,到期自动取消。";
|
||||
|
Loading…
Reference in New Issue
Block a user