对算法工具类,JWT签名器工具类的一些补充,补充部分已完成ut自测

This commit is contained in:
youlinlong 2022-08-18 15:02:42 +08:00
parent a60aa0ec47
commit 360b5df1c6
3 changed files with 208 additions and 0 deletions

View File

@ -23,6 +23,11 @@ public class AlgorithmUtil {
map.put("HS384", HmacAlgorithm.HmacSHA384.getValue()); map.put("HS384", HmacAlgorithm.HmacSHA384.getValue());
map.put("HS512", HmacAlgorithm.HmacSHA512.getValue()); map.put("HS512", HmacAlgorithm.HmacSHA512.getValue());
map.put("HMD5", HmacAlgorithm.HmacMD5.getValue());
map.put("HSHA1", HmacAlgorithm.HmacSHA1.getValue());
map.put("SM4CMAC", HmacAlgorithm.SM4CMAC.getValue());
map.put("RS256", SignAlgorithm.SHA256withRSA.getValue()); map.put("RS256", SignAlgorithm.SHA256withRSA.getValue());
map.put("RS384", SignAlgorithm.SHA384withRSA.getValue()); map.put("RS384", SignAlgorithm.SHA384withRSA.getValue());
map.put("RS512", SignAlgorithm.SHA512withRSA.getValue()); map.put("RS512", SignAlgorithm.SHA512withRSA.getValue());
@ -34,6 +39,14 @@ public class AlgorithmUtil {
map.put("PS256", SignAlgorithm.SHA256withRSA_PSS.getValue()); map.put("PS256", SignAlgorithm.SHA256withRSA_PSS.getValue());
map.put("PS384", SignAlgorithm.SHA384withRSA_PSS.getValue()); map.put("PS384", SignAlgorithm.SHA384withRSA_PSS.getValue());
map.put("PS512", SignAlgorithm.SHA512withRSA_PSS.getValue()); map.put("PS512", SignAlgorithm.SHA512withRSA_PSS.getValue());
map.put("RMD2", SignAlgorithm.MD2withRSA.getValue());
map.put("RMD5", SignAlgorithm.MD5withRSA.getValue());
map.put("RSHA1", SignAlgorithm.SHA1withRSA.getValue());
map.put("DNONE", SignAlgorithm.NONEwithDSA.getValue());
map.put("DSHA1", SignAlgorithm.SHA1withDSA.getValue());
map.put("ENONE", SignAlgorithm.NONEwithECDSA.getValue());
map.put("ESHA1", SignAlgorithm.SHA1withECDSA.getValue());
} }
/** /**

View File

@ -120,6 +120,109 @@ public class JWTSignerUtil {
return createSigner("ES512", key); return createSigner("ES512", key);
} }
/**
* HMD5(HmacMD5)签名器
*
* @param key 密钥
* @return 签名器
*/
public static JWTSigner hmd5(Key key) {
return createSigner("HMD5",key);
}
/**
* HSHA1(HmacSHA1)签名器
*
* @param key 密钥
* @return 签名器
*/
public static JWTSigner hsha1(Key key) {
return createSigner("HSHA1",key);
}
/**
* SM4CMAC(SM4CMAC)签名器
*
* @param key 密钥
* @return 签名器
*/
public static JWTSigner sm4cmac(Key key) {
return createSigner("SM4CMAC",key);
}
/**
* RMD2(MD2withRSA)签名器
*
* @param key 密钥
* @return 签名器
*/
public static JWTSigner rmd2(Key key) {
return createSigner("RMD2",key);
}
/**
* RMD5(MD5withRSA)签名器
*
* @param key 密钥
* @return 签名器
*/
public static JWTSigner rmd5(Key key) {
return createSigner("RMD5",key);
}
/**
* RSHA1(SHA1withRSA)签名器
*
* @param key 密钥
* @return 签名器
*/
public static JWTSigner rsha1(Key key) {
return createSigner("RSHA1",key);
}
/**
* DNONE(NONEwithDSA)签名器
*
* @param key 密钥
* @return 签名器
*/
public static JWTSigner dnone(Key key) {
return createSigner("DNONE",key);
}
/**
* DSHA1(SHA1withDSA)签名器
*
* @param key 密钥
* @return 签名器
*/
public static JWTSigner dsha1(Key key) {
return createSigner("DSHA1",key);
}
/**
* ENONE(NONEwithECDSA)签名器
*
* @param key 密钥
* @return 签名器
*/
public static JWTSigner enone(Key key) {
return createSigner("ENONE",key);
}
/**
* ESHA1(SHA1withECDSA)签名器
*
* @param key 密钥
* @return 签名器
*/
public static JWTSigner esha1(Key key) {
return createSigner("ESHA1",key);
}
/** /**
* 创建签名器 * 创建签名器
* *

View File

@ -116,6 +116,98 @@ public class JWTSignerTest {
signAndVerify(signer); signAndVerify(signer);
} }
@Test
public void hmd5Test(){
String id = "hmd5";
final JWTSigner signer = JWTSignerUtil.createSigner(id, KeyUtil.generateKey(AlgorithmUtil.getAlgorithm(id)));
Assert.assertEquals(AlgorithmUtil.getAlgorithm(id), signer.getAlgorithm());
signAndVerify(signer);
}
@Test
public void hsha1Test(){
String id = "hsha1";
final JWTSigner signer = JWTSignerUtil.createSigner(id, KeyUtil.generateKey(AlgorithmUtil.getAlgorithm(id)));
Assert.assertEquals(AlgorithmUtil.getAlgorithm(id), signer.getAlgorithm());
signAndVerify(signer);
}
@Test
public void sm4cmacTest(){
String id = "sm4cmac";
final JWTSigner signer = JWTSignerUtil.createSigner(id, KeyUtil.generateKey(AlgorithmUtil.getAlgorithm(id)));
Assert.assertEquals(AlgorithmUtil.getAlgorithm(id), signer.getAlgorithm());
signAndVerify(signer);
}
@Test
public void rmd2Test(){
String id = "rmd2";
final JWTSigner signer = JWTSignerUtil.createSigner(id, KeyUtil.generateKeyPair(AlgorithmUtil.getAlgorithm(id)));
Assert.assertEquals(AlgorithmUtil.getAlgorithm(id), signer.getAlgorithm());
signAndVerify(signer);
}
@Test
public void rmd5Test(){
String id = "rmd5";
final JWTSigner signer = JWTSignerUtil.createSigner(id, KeyUtil.generateKeyPair(AlgorithmUtil.getAlgorithm(id)));
Assert.assertEquals(AlgorithmUtil.getAlgorithm(id), signer.getAlgorithm());
signAndVerify(signer);
}
@Test
public void rsha1Test(){
String id = "rsha1";
final JWTSigner signer = JWTSignerUtil.createSigner(id, KeyUtil.generateKeyPair(AlgorithmUtil.getAlgorithm(id)));
Assert.assertEquals(AlgorithmUtil.getAlgorithm(id), signer.getAlgorithm());
signAndVerify(signer);
}
@Test
public void dnoneTest(){
String id = "dnone";
final JWTSigner signer = JWTSignerUtil.createSigner(id, KeyUtil.generateKeyPair(AlgorithmUtil.getAlgorithm(id)));
Assert.assertEquals(AlgorithmUtil.getAlgorithm(id), signer.getAlgorithm());
signAndVerify(signer);
}
@Test
public void dsha1Test(){
String id = "dsha1";
final JWTSigner signer = JWTSignerUtil.createSigner(id, KeyUtil.generateKeyPair(AlgorithmUtil.getAlgorithm(id)));
Assert.assertEquals(AlgorithmUtil.getAlgorithm(id), signer.getAlgorithm());
signAndVerify(signer);
}
@Test
public void enoneTest(){
String id = "enone";
final JWTSigner signer = JWTSignerUtil.createSigner(id, KeyUtil.generateKeyPair(AlgorithmUtil.getAlgorithm(id)));
Assert.assertEquals(AlgorithmUtil.getAlgorithm(id), signer.getAlgorithm());
signAndVerify(signer);
}
@Test
public void esha1Test(){
String id = "esha1";
final JWTSigner signer = JWTSignerUtil.createSigner(id, KeyUtil.generateKeyPair(AlgorithmUtil.getAlgorithm(id)));
Assert.assertEquals(AlgorithmUtil.getAlgorithm(id), signer.getAlgorithm());
signAndVerify(signer);
}
private static void signAndVerify(JWTSigner signer){ private static void signAndVerify(JWTSigner signer){
JWT jwt = JWT.create() JWT jwt = JWT.create()
.setPayload("sub", "1234567890") .setPayload("sub", "1234567890")