mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix JSONUtil.isJson
This commit is contained in:
parent
f126ded53a
commit
1a7812a281
@ -3,10 +3,13 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.5.9 (2021-01-30)
|
||||
# 5.5.9 (2021-02-03)
|
||||
|
||||
### 新特性
|
||||
* 【crypto 】 PemUtil.readPemKey支持EC(pr#1366@Github)
|
||||
|
||||
### Bug修复
|
||||
* 【json 】 JSONUtil.isJson方法改变trim策略,解决特殊空白符导致判断失败问题
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -63,8 +63,16 @@ public class PemUtil {
|
||||
final PemObject object = readPemObject(keyStream);
|
||||
final String type = object.getType();
|
||||
if (StrUtil.isNotBlank(type)) {
|
||||
if (type.endsWith("PRIVATE KEY")) {
|
||||
//private
|
||||
if (type.endsWith("EC PRIVATE KEY")) {
|
||||
return KeyUtil.generatePrivateKey("EC", object.getContent());
|
||||
}if (type.endsWith("PRIVATE KEY")) {
|
||||
return KeyUtil.generateRSAPrivateKey(object.getContent());
|
||||
}
|
||||
|
||||
// public
|
||||
if (type.endsWith("EC PUBLIC KEY")) {
|
||||
return KeyUtil.generatePublicKey("EC", object.getContent());
|
||||
} else if (type.endsWith("PUBLIC KEY")) {
|
||||
return KeyUtil.generateRSAPublicKey(object.getContent());
|
||||
} else if (type.endsWith("CERTIFICATE")) {
|
||||
|
@ -17,7 +17,7 @@ public class PemUtilTest {
|
||||
PrivateKey privateKey = PemUtil.readPemPrivateKey(ResourceUtil.getStream("test_private_key.pem"));
|
||||
Assert.assertNotNull(privateKey);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void readPublicKeyTest() {
|
||||
PublicKey publicKey = PemUtil.readPemPublicKey(ResourceUtil.getStream("test_public_key.csr"));
|
||||
@ -34,12 +34,18 @@ public class PemUtilTest {
|
||||
public void validateKey() {
|
||||
PrivateKey privateKey = PemUtil.readPemPrivateKey(ResourceUtil.getStream("test_private_key.pem"));
|
||||
PublicKey publicKey = PemUtil.readPemPublicKey(ResourceUtil.getStream("test_public_key.csr"));
|
||||
|
||||
|
||||
RSA rsa = new RSA(privateKey, publicKey);
|
||||
String str = "你好,Hutool";//测试字符串
|
||||
|
||||
|
||||
String encryptStr = rsa.encryptBase64(str, KeyType.PublicKey);
|
||||
String decryptStr = rsa.decryptStr(encryptStr, KeyType.PrivateKey);
|
||||
Assert.assertEquals(str, decryptStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readECPrivateKeyTest() {
|
||||
PrivateKey privateKey = PemUtil.readPemPrivateKey(ResourceUtil.getStream("test_ec_private_key.pem"));
|
||||
Assert.assertNotNull(privateKey);
|
||||
}
|
||||
}
|
||||
|
5
hutool-crypto/src/test/resources/test_ec_private_key.pem
Normal file
5
hutool-crypto/src/test/resources/test_ec_private_key.pem
Normal file
@ -0,0 +1,5 @@
|
||||
-----BEGIN EC PRIVATE KEY-----
|
||||
MHcCAQEEIKB89IhhSy9WrtQS7TWO5Yqyv5a3DnogWYUhb3TbzjnWoAoGCCqBHM9V
|
||||
AYItoUQDQgAE3LRuqCM697gL3jPhw98eGfTDcJsuJr6H1nE4VkgdtBdX3So2lC6m
|
||||
UGEnWeRZuh8HnzCRobcu02Bgv7CVR5Iigg==
|
||||
-----END EC PRIVATE KEY-----
|
@ -801,7 +801,7 @@ public final class JSONUtil {
|
||||
if (StrUtil.isBlank(str)) {
|
||||
return false;
|
||||
}
|
||||
return StrUtil.isWrap(str.trim(), '{', '}');
|
||||
return StrUtil.isWrap(StrUtil.trim(str), '{', '}');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -815,7 +815,7 @@ public final class JSONUtil {
|
||||
if (StrUtil.isBlank(str)) {
|
||||
return false;
|
||||
}
|
||||
return StrUtil.isWrap(str.trim(), '[', ']');
|
||||
return StrUtil.isWrap(StrUtil.trim(str), '[', ']');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -179,4 +179,4 @@ public class JSONUtilTest {
|
||||
" \"test\": \"\\\\地库地库\",\n" +
|
||||
"}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user