From 162c080e5abb1556d28a65ad57c959d68af70d6b Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 26 Nov 2020 14:42:13 +0800 Subject: [PATCH] add method --- CHANGELOG.md | 1 + .../java/cn/hutool/core/codec/Base64.java | 77 ++++++------------- .../java/cn/hutool/crypto/digest/BCrypt.java | 2 +- 3 files changed, 25 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f10eb39e9..0c6036887 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * 【core 】 AnnotationUtil增加setValue方法(pr#1250@Github) * 【core 】 ZipUtil增加get方法 * 【cache 】 对CacheObj等变量使用volatile关键字 +* 【core 】 Base64增加encodeWithoutPadding方法(issue#I26J16@Gitee) ### Bug修复 * 【cron 】 修复CronTimer可能死循环的问题(issue#1224@Github) diff --git a/hutool-core/src/main/java/cn/hutool/core/codec/Base64.java b/hutool-core/src/main/java/cn/hutool/core/codec/Base64.java index 979b568c8..5aa6ea320 100644 --- a/hutool-core/src/main/java/cn/hutool/core/codec/Base64.java +++ b/hutool-core/src/main/java/cn/hutool/core/codec/Base64.java @@ -3,6 +3,7 @@ package cn.hutool.core.codec; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.CharsetUtil; +import cn.hutool.core.util.StrUtil; import java.io.File; import java.io.InputStream; @@ -75,6 +76,17 @@ public class Base64 { return encode(source, CharsetUtil.charset(charset)); } + /** + * base64编码,不进行padding(末尾不会填充'=') + * + * @param source 被编码的base64字符串 + * @return 被加密后的字符串 + * @since 5.5.2 + */ + public static String encodeWithoutPadding(CharSequence source, String charset) { + return encodeWithoutPadding(StrUtil.bytes(source, charset)); + } + /** * base64编码,URL安全 * @@ -120,6 +132,17 @@ public class Base64 { return Base64Encoder.encode(source); } + /** + * base64编码,不进行padding(末尾不会填充'=') + * + * @param source 被编码的base64字符串 + * @return 被加密后的字符串 + * @since 5.5.2 + */ + public static String encodeWithoutPadding(byte[] source) { + return java.util.Base64.getEncoder().withoutPadding().encodeToString(source); + } + /** * base64编码,URL安全的 * @@ -175,60 +198,6 @@ public class Base64 { return Base64Encoder.encodeUrlSafe(FileUtil.readBytes(file)); } - /** - * base64编码 - * - * @param source 被编码的base64字符串 - * @param charset 字符集 - * @return 被加密后的字符串 - * @deprecated 编码参数无意义,作废 - */ - @Deprecated - public static String encode(byte[] source, String charset) { - return Base64Encoder.encode(source); - } - - /** - * base64编码,URL安全的 - * - * @param source 被编码的base64字符串 - * @param charset 字符集 - * @return 被加密后的字符串 - * @since 3.0.6 - * @deprecated 编码参数无意义,作废 - */ - @Deprecated - public static String encodeUrlSafe(byte[] source, String charset) { - return Base64Encoder.encodeUrlSafe(source); - } - - /** - * base64编码 - * - * @param source 被编码的base64字符串 - * @param charset 字符集 - * @return 被加密后的字符串 - * @deprecated 编码参数无意义,作废 - */ - @Deprecated - public static String encode(byte[] source, Charset charset) { - return Base64Encoder.encode(source); - } - - /** - * base64编码,URL安全的 - * - * @param source 被编码的base64字符串 - * @param charset 字符集 - * @return 被加密后的字符串 - * @since 3.0.6 - * @deprecated 编码参数无意义,作废 - */ - @Deprecated - public static String encodeUrlSafe(byte[] source, Charset charset) { - return Base64Encoder.encodeUrlSafe(source); - } - /** * 编码为Base64
* 如果isMultiLine为true,则每76个字符一个换行符,否则在一行显示 diff --git a/hutool-crypto/src/main/java/cn/hutool/crypto/digest/BCrypt.java b/hutool-crypto/src/main/java/cn/hutool/crypto/digest/BCrypt.java index 7113bb467..cf96e7997 100644 --- a/hutool-crypto/src/main/java/cn/hutool/crypto/digest/BCrypt.java +++ b/hutool-crypto/src/main/java/cn/hutool/crypto/digest/BCrypt.java @@ -179,7 +179,7 @@ public class BCrypt { private static byte char64(char x) { if ((int) x > index_64.length) return -1; - return index_64[(int) x]; + return index_64[x]; } /**