From 360b5df1c6b7b42293301841c2a4f703971aa75d Mon Sep 17 00:00:00 2001 From: youlinlong Date: Thu, 18 Aug 2022 15:02:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E7=AE=97=E6=B3=95=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB=EF=BC=8CJWT=E7=AD=BE=E5=90=8D=E5=99=A8=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=E7=9A=84=E4=B8=80=E4=BA=9B=E8=A1=A5=E5=85=85?= =?UTF-8?q?=EF=BC=8C=E8=A1=A5=E5=85=85=E9=83=A8=E5=88=86=E5=B7=B2=E5=AE=8C?= =?UTF-8?q?=E6=88=90ut=E8=87=AA=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/jwt/signers/AlgorithmUtil.java | 13 +++ .../cn/hutool/jwt/signers/JWTSignerUtil.java | 103 ++++++++++++++++++ .../java/cn/hutool/jwt/JWTSignerTest.java | 92 ++++++++++++++++ 3 files changed, 208 insertions(+) diff --git a/hutool-jwt/src/main/java/cn/hutool/jwt/signers/AlgorithmUtil.java b/hutool-jwt/src/main/java/cn/hutool/jwt/signers/AlgorithmUtil.java index 1aa8e2b1e..82a5ec2de 100755 --- a/hutool-jwt/src/main/java/cn/hutool/jwt/signers/AlgorithmUtil.java +++ b/hutool-jwt/src/main/java/cn/hutool/jwt/signers/AlgorithmUtil.java @@ -23,6 +23,11 @@ public class AlgorithmUtil { map.put("HS384", HmacAlgorithm.HmacSHA384.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("RS384", SignAlgorithm.SHA384withRSA.getValue()); map.put("RS512", SignAlgorithm.SHA512withRSA.getValue()); @@ -34,6 +39,14 @@ public class AlgorithmUtil { map.put("PS256", SignAlgorithm.SHA256withRSA_PSS.getValue()); map.put("PS384", SignAlgorithm.SHA384withRSA_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()); } /** diff --git a/hutool-jwt/src/main/java/cn/hutool/jwt/signers/JWTSignerUtil.java b/hutool-jwt/src/main/java/cn/hutool/jwt/signers/JWTSignerUtil.java index 82076ad23..0d631a08d 100755 --- a/hutool-jwt/src/main/java/cn/hutool/jwt/signers/JWTSignerUtil.java +++ b/hutool-jwt/src/main/java/cn/hutool/jwt/signers/JWTSignerUtil.java @@ -120,6 +120,109 @@ public class JWTSignerUtil { 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); + } + + + + /** * 创建签名器 * diff --git a/hutool-jwt/src/test/java/cn/hutool/jwt/JWTSignerTest.java b/hutool-jwt/src/test/java/cn/hutool/jwt/JWTSignerTest.java index c0e0d622b..6bd335644 100755 --- a/hutool-jwt/src/test/java/cn/hutool/jwt/JWTSignerTest.java +++ b/hutool-jwt/src/test/java/cn/hutool/jwt/JWTSignerTest.java @@ -116,6 +116,98 @@ public class JWTSignerTest { 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){ JWT jwt = JWT.create() .setPayload("sub", "1234567890")