Forráskód Böngészése

海慈签名算法开发

hurugang 5 éve
szülő
commit
202d3c2782

+ 39 - 0
src/main/java/cn/hnthyy/thmz/Utils/HcPassWordUtil.java

@@ -0,0 +1,39 @@
+package cn.hnthyy.thmz.Utils;
+
+import org.apache.commons.compress.utils.Lists;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.UnsupportedEncodingException;
+import java.security.NoSuchAlgorithmException;
+import java.util.*;
+
+/**
+ * 海慈签名算法
+ */
+public class HcPassWordUtil {
+    /**
+     * sign 签名 (参数名按ASCII码从小到大排序(字典序)+key+MD5+转大写签名)
+     * @param map
+     * @return
+     */
+    public static String encodeSign(SortedMap<String,String> map, String key) throws UnsupportedEncodingException, NoSuchAlgorithmException {
+        if(StringUtils.isEmpty(key)){
+            throw new RuntimeException("签名key不能为空");
+        }
+        Set<Map.Entry<String, String>> entries = map.entrySet();
+        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
+        List<String> values = Lists.newArrayList();
+
+        while(iterator.hasNext()){
+            Map.Entry entry = iterator.next();
+            String k = String.valueOf(entry.getKey());
+            String v = String.valueOf(entry.getValue());
+            if (StringUtils.isNotEmpty(v) && entry.getValue() !=null && !"sign".equals(k) && !"key".equals(k)) {
+                values.add(k + "=" + v);
+            }
+        }
+        values.add("key="+ key);
+        String sign = StringUtils.join(values, "&");
+        return Md5Util.encoderByMd5(sign).toUpperCase();
+    }
+}

+ 2 - 2
src/main/java/cn/hnthyy/thmz/Utils/Md5Util.java

@@ -13,7 +13,7 @@ public class Md5Util {
     /**
      * 利用MD5进行加密
      */
-    public static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
+    public static String encoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
         //确定计算方法
         MessageDigest md5 = MessageDigest.getInstance("MD5");
         BASE64Encoder base64en = new BASE64Encoder();
@@ -28,7 +28,7 @@ public class Md5Util {
      * oldPassword 数据库存储的加密密码
      */
     public static boolean checkPassword(String newPassword, String oldPassword) throws NoSuchAlgorithmException, UnsupportedEncodingException {
-        if (EncoderByMd5(newPassword).equals(oldPassword))
+        if (encoderByMd5(newPassword).equals(oldPassword))
             return true;
         else
             return false;

+ 1 - 1
src/main/java/cn/hnthyy/thmz/controller/UserController.java

@@ -55,7 +55,7 @@ public class UserController {
                     resultMap.put("message", "密码重置失败,原密码密码错误,请重新输入");
                     return resultMap;
                 }
-                userParam.setPassword(Md5Util.EncoderByMd5(userParam.getPassword()));
+                userParam.setPassword(Md5Util.encoderByMd5(userParam.getPassword()));
                 message = "用户密码重置成功,即将注销登录,请重新登录!";
             } catch (Exception e) {
                 log.error("密码重置错误,错误信息:{}", e.getMessage());

+ 1 - 1
src/test/java/cn/hnthyy/thmz/ThmzApplicationTests.java

@@ -17,7 +17,7 @@ public class ThmzApplicationTests {
     public void contextLoads() throws UnsupportedEncodingException, NoSuchAlgorithmException {
         String str = "1";
         try {
-            String newString = Md5Util.EncoderByMd5(str);
+            String newString = Md5Util.encoderByMd5(str);
             System.out.println(newString);
         } catch (NoSuchAlgorithmException e) {
             // TODO Auto-generated catch block