|
@@ -0,0 +1,36 @@
|
|
|
+package org.springblade.common.utils;
|
|
|
+
|
|
|
+import java.security.KeyFactory;
|
|
|
+import java.security.PrivateKey;
|
|
|
+import java.security.Signature;
|
|
|
+import java.security.spec.KeySpec;
|
|
|
+import java.security.spec.PKCS8EncodedKeySpec;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 通用工具类
|
|
|
+ *
|
|
|
+ * @author Chill
|
|
|
+ */
|
|
|
+public class SignUtil {
|
|
|
+
|
|
|
+ //签名
|
|
|
+ public static String sign(String content, String privateKey) throws Exception {
|
|
|
+ return sign(content.getBytes("utf-8"), getPrivateKey(privateKey), "SHA256withRSA");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String sign(byte[] content, PrivateKey privateKey, String signInstance) throws Exception {
|
|
|
+ Signature signature = Signature.getInstance(signInstance);
|
|
|
+ signature.initSign(privateKey);
|
|
|
+ signature.update(content);
|
|
|
+ return Base64.getEncoder().encodeToString(signature.sign());
|
|
|
+ }
|
|
|
+
|
|
|
+ //生成私钥
|
|
|
+ public static PrivateKey getPrivateKey(String privateKey) throws Exception {
|
|
|
+ byte[] bytes = Base64.getDecoder().decode(privateKey);
|
|
|
+ KeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
|
|
|
+ KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
|
|
+ return keyFactory.generatePrivate(keySpec);
|
|
|
+ }
|
|
|
+}
|