要求:
1.对所有传入参数(含系统参数和接口参数)按照字段名的 ASCII 码从小到大排序(字典序)后,使用 URL 键值对的格式。(即 key1=value1&key2=value2…)拼接成字符串string1, 注意:值为空的参数不参与签名。
2.在string1后面拼接上key=privateKey得到stringSign字符串,并对stringSign进行sha1运算,再将得到的字符串转为大写,得到sign值。如对 age=12&name=张三 key=privateKey 进行 sha1 加密后得到 xxxxx。
参考代码:
public class Encrypt { private static Logger logger = LoggerFactory.getLogger(Encrypt.class); public static final String key = "12345abc"; public static void main(String[] args) { //创建实体类 SubmitAssetRequest submitAssetRequest = new SubmitAssetRequest(); submitAssetRequest.setBidType("1"); submitAssetRequest.setBankCardNo("6217002870039791536"); submitAssetRequest.setBankName("中国建设银行"); submitAssetRequest.setPeriodsType(1); submitAssetRequest.setAmount(new BigDecimal(1000)); submitAssetRequest.setIdCardNumber("420117199105248734"); submitAssetRequest.setMobile("18807111634"); submitAssetRequest.setSex(1); submitAssetRequest.setMarriageState(0); submitAssetRequest.setName("张三"); submitAssetRequest.setPeriods(14); submitAssetRequest.setChannelCode("NYD"); submitAssetRequest.setRates(new BigDecimal(23.76)); submitAssetRequest.setOrderId("101537895995716001"); //参数封装到map集合 Map<String, String> map = getMap(submitAssetRequest); //排序 String sign = getSign(map); System.out.println(sign); } /** * 将参数封装到map集合,不为空的参数添加进去 * @param request * @return */ public static Map<String, String> getMap(SubmitAssetRequest request){ Map<String, String> map = new HashMap<>(); map.put("channelCode",request.getChannelCode()); map.put("orderId",request.getOrderId()); map.put("name",request.getName()); if (!StringUtils.isEmpty(request.getInvestorList())){ map.put("investorList",request.getInvestorList()); } map.put("bidType",request.getBidType()); map.put("sex",String.valueOf(request.getSex())); map.put("mobile",request.getMobile()); map.put("idCardNumber",request.getIdCardNumber()); map.put("bankName",request.getBankName()); map.put("bankCardNo",request.getBankCardNo()); if (!StringUtils.isEmpty(request.getLoanUse())){ map.put("loanUse",request.getLoanUse()); } if (request.getIncome() != null){ map.put("income",String.valueOf(request.getIncome())); } if (!StringUtils.isEmpty(request.getAddress())){ map.put("address",request.getAddress()); } if (!StringUtils.isEmpty(request.getBirthPlace())){ map.put("birthPlace",request.getBirthPlace()); } if (String.valueOf(request.getMarriageState()) != null){ map.put("marriageState",String.valueOf(request.getMarriageState())); } map.put("periods",String.valueOf(request.getPeriods())); map.put("periodsType",String.valueOf(request.getPeriodsType())); map.put("amount",String.valueOf(request.getAmount())); map.put("rates",String.valueOf(request.getRates())); return map; } public static String getSign(Map<String, String> map){ if (map == null || map.size() == 0) { return null; } List<String> list = new ArrayList<>(); for (Map.Entry<String,String> entry : map.entrySet()) { if(entry.getValue()==null || entry.getValue().trim().length()==0){ continue; } list.add(entry.getKey()); } Collections.sort(list); logger.info("排序后的list:"+list.toString()); StringBuilder sb = new StringBuilder(); for (String str:list){ try { sb.append(str).append("=").append(URLEncoder.encode(map.get(str).toString(),"UTF-8")).append("&"); } catch (UnsupportedEncodingException e) { logger.error("编码异常",e); } } sb.append("key="); sb.append(key); String ss = sb.toString(); if (ss == null) { return null; } String result = DigestUtils.sha1Hex(ss.getBytes()); logger.info("最终结果:"+result); //转为大写 String upperCase = result.toUpperCase(); return upperCase; } }
//对应实体类
public class SubmitAssetRequest { //渠道号 private String channelCode; //订单号 private String orderId; //姓名 private String name; //投资人列表(非必传) private String investorList; //标的类型 1:定向标 2:非定向标 private String bidType; //性别 1:表示男性 2:表示女性 private int sex; //手机号 private String mobile; //身份证号 private String idCardNumber; //银行名称 private String bankName; //银行卡号 private String bankCardNo; //借款用途(非必传) private String loanUse; //收入(非必传) private String income; //住址(非必传) private String address; //户籍地址(非必传) private String birthPlace; //婚姻状况(非必传) 0 未婚 1 已婚 private int marriageState; //借款期限 =1 时,表示天; =2 时,表示期 private int periods; //期限类型 1 表示单期 ; 2 表示多期 private int periodsType; //借款金额 private BigDecimal amount; //借款利率 private BigDecimal rates; //签名 private String sign; public String getChannelCode() { return channelCode; } public void setChannelCode(String channelCode) { this.channelCode = channelCode; } public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getInvestorList() { return investorList; } public void setInvestorList(String investorList) { this.investorList = investorList; } public String getBidType() { return bidType; } public void setBidType(String bidType) { this.bidType = bidType; } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getIdCardNumber() { return idCardNumber; } public void setIdCardNumber(String idCardNumber) { this.idCardNumber = idCardNumber; } public String getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } public String getBankCardNo() { return bankCardNo; } public void setBankCardNo(String bankCardNo) { this.bankCardNo = bankCardNo; } public String getLoanUse() { return loanUse; } public void setLoanUse(String loanUse) { this.loanUse = loanUse; } public String getIncome() { return income; } public void setIncome(String income) { this.income = income; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getBirthPlace() { return birthPlace; } public void setBirthPlace(String birthPlace) { this.birthPlace = birthPlace; } public int getMarriageState() { return marriageState; } public void setMarriageState(int marriageState) { this.marriageState = marriageState; } public int getPeriods() { return periods; } public void setPeriods(int periods) { this.periods = periods; } public int getPeriodsType() { return periodsType; } public void setPeriodsType(int periodsType) { this.periodsType = periodsType; } public BigDecimal getAmount() { return amount; } public void setAmount(BigDecimal amount) { this.amount = amount; } public BigDecimal getRates() { return rates; } public void setRates(BigDecimal rates) { this.rates = rates; } public String getSign() { return sign; } public void setSign(String sign) { this.sign = sign; } }