支付系统中的常用工具

本贴最后更新于 2882 天前,其中的信息可能已经时异事殊
  • StringUtils.java

  处理常用字符串:判断是否为空 isEmpty(String value);

  按字典排序并拼接参数:createLinkString(Map params);

import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; public class StringUtils { /** * 判断字符数组是否为空 */ public static boolean areNotEmpty(String... values) { boolean result = true; if (values == null || values.length == 0) { result = false; } else { for (String value : values) { result &= !isEmpty(value); } } return result; } /** * 把数组所有元素排序,并按照"name1=value1&name2=value2..."字符拼接成字符串 * * @param params * 需要排序并参与字符拼接的参数组 * @return 拼接后字符串 */ public static String createLinkString(Map<String, String> params) { List<String> keys = new ArrayList<String>(params.keySet()); Collections.sort(keys); String prestr = ""; for (int i = 0; i < keys.size(); i++) { String key = keys.get(i); String value = params.get(key); if (i == keys.size() - 1) { prestr = prestr + key + "=" + value; } else { prestr = prestr + key + "=" + value + "&"; } } return prestr; } /** * 判断字符串是否为空 * <ul> * <li>isEmpty(null) = true</li> * <li>isEmpty("") = true</li> * <li>isEmpty(" ") = true</li> * <li>isEmpty("abc") = false</li> * </ul> * * @param value * 目标字符串 * @return true/false */ public static boolean isEmpty(String value) { int strLen; if (value == null || (strLen = value.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if ((Character.isWhitespace(value.charAt(i)) == false)) { return false; } } return true; } }
  • 支付系统中的签名和验签 SignUtils.java
    import java.io.InputStream;
    import java.security.KeyStore;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.cert.Certificate;
    import java.security.cert.CertificateFactory;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import org.apache.commons.codec.binary.Base64;

    /**

    */
    public class SignUtils {

    /** */ // 缓存公钥和私钥 public static Map<String, Object> certMap = new java.util.concurrent.ConcurrentHashMap<String, Object>(); /** * * * @param sArray * @return */ public static Map<String, String> paraFilter(Map<String, String> sArray) { Map<String, String> result = new HashMap<String, String>(); if (sArray == null || sArray.size() <= 0) { return result; } for (String key : sArray.keySet()) { String value = sArray.get(key); if (value == null || StringUtils.isEmpty(value) || key.equalsIgnoreCase("sign")) { continue; } result.put(key, value); } return result; } /** * * * @param sortedParams * @return */ public static String getSignContent(Map<String, String> sortedParams) { StringBuffer content = new StringBuffer(); List<String> keys = new ArrayList<String>(sortedParams.keySet()); Collections.sort(keys); int index = 0; for (int i = 0; i < keys.size(); i++) { String key = keys.get(i); String value = sortedParams.get(key); if (StringUtils.areNotEmpty(key, value)) { content.append((index == 0 ? "" : "&") + key + "=" + value); index++; } } return content.toString(); } /** * * 签名 * @param params 业务参数 * @param charset 编码 * @param pfxCertFileInputStream 证书输入流 * @param rsaPassword 私钥pkcs12证书密码 * @param algorithmName rsa算法名 * @return * @throws Exception */ public static String rsaSign(Map<String, String> params, String charset, InputStream pfxCertFileInputStream,String rsaPassword, String algorithmName) throws Exception { String signContent = getSignContent(params); return rsaSign(signContent, charset, pfxCertFileInputStream,rsaPassword, algorithmName); } /** * * * @param content * @param charset * @param pfxCertFileInputStream * @param rsaPassword * @param algorithmName * @return * @throws Exception */ public static String rsaSign(String content, String charset, InputStream pfxCertFileInputStream,String rsaPassword, String algorithmName) throws Exception { try { PrivateKey priKey = getPrivateKeyFromPKCS12( rsaPassword, pfxCertFileInputStream); java.security.Signature signature = java.security.Signature .getInstance(algorithmName); signature.initSign(priKey); if (StringUtils.isEmpty(charset)) { signature.update(content.getBytes()); } else { signature.update(content.getBytes(charset)); } byte[] signed = signature.sign(); String sign = new String(Base64.encodeBase64(signed), charset); return sign; } catch (Exception e) { throw new Exception("RSAcontent = " + content + "; charset = " + charset, e); } } /** * * * @param publicCertFileInputStream * @param params * @param sign * @param charset * @param algorithmName * @return * @throws Exception */ public static boolean rsaCheckContent( InputStream publicCertFileInputStream, Map<String, String> params, String sign, String charset,String algorithmName) throws Exception { String content = StringUtils.createLinkString(SignUtils .paraFilter(params)); return rsaCheckContent(publicCertFileInputStream, content, sign, charset, algorithmName); } /** * * * @param publicCertFileInputStream * @param content * @param sign * @param charset * @param algorithmName * @return * @throws Exception */ public static boolean rsaCheckContent( InputStream publicCertFileInputStream, String content, String sign, String charset, String algorithmName) throws Exception { boolean bFlag = false; try { java.security.Signature signetcheck = java.security.Signature .getInstance(algorithmName); signetcheck .initVerify(getPublicKeyFromCert(publicCertFileInputStream)); signetcheck.update(content.getBytes(charset)); if (signetcheck.verify(Base64.decodeBase64(sign.getBytes(charset)))) { bFlag = true; System.out.println("签名成功!"); }else{ System.out.println("签名失败!"); } } catch (Exception e) { throw new Exception("验证签名异常"); } return bFlag; } /** * 读取公钥,x509格式. * * @param ins * @return * @throws Exception */ public static PublicKey getPublicKeyFromCert(InputStream ins) throws Exception { PublicKey pubKey = (PublicKey) certMap.get("PublicKey"); if (pubKey != null) { return pubKey; } try { CertificateFactory cf = CertificateFactory.getInstance("X.509"); Certificate cac = cf.generateCertificate(ins); pubKey = cac.getPublicKey(); certMap.put("PublicKey", pubKey); } catch (Exception e) { if (ins != null) ins.close(); throw e; } finally { if (ins != null) { ins.close(); } } return pubKey; } /** * 读取PKCS12格式的key(私钥)pfx格式. * * @param rsaPassword * @param ins * @return * @throws Exception */ public static PrivateKey getPrivateKeyFromPKCS12(String rsaPassword, InputStream ins) throws Exception { PrivateKey priKey = (PrivateKey) certMap.get("PrivateKey"); if (priKey != null) { return priKey; } KeyStore keystoreCA = KeyStore.getInstance("PKCS12"); try { // 读取CA根证书 keystoreCA.load(ins, rsaPassword.toCharArray()); Enumeration<?> aliases = keystoreCA.aliases(); String keyAlias = null; if (aliases != null) { while (aliases.hasMoreElements()) { keyAlias = (String) aliases.nextElement(); // 获取CA私钥 priKey = (PrivateKey) (keystoreCA.getKey(keyAlias, rsaPassword.toCharArray())); if (priKey != null) { certMap.put("PrivateKey", priKey); break; } } } } catch (Exception e) { if (ins != null) ins.close(); throw e; } finally { if (ins != null) { ins.close(); } } return priKey; }

    }

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...

推荐标签 标签

  • 导航

    各种网址链接、内容导航。

    44 引用 • 177 回帖
  • PWA

    PWA(Progressive Web App)是 Google 在 2015 年提出、2016 年 6 月开始推广的项目。它结合了一系列现代 Web 技术,在网页应用中实现和原生应用相近的用户体验。

    14 引用 • 69 回帖 • 178 关注
  • 旅游

    希望你我能在旅途中找到人生的下一站。

    97 引用 • 903 回帖
  • Q&A

    提问之前请先看《提问的智慧》,好的问题比好的答案更有价值。

    9805 引用 • 44577 回帖 • 81 关注
  • C

    C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

    86 引用 • 165 回帖
  • Redis

    Redis 是一个开源的使用 ANSI C 语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value 数据库,并提供多种语言的 API。从 2010 年 3 月 15 日起,Redis 的开发工作由 VMware 主持。从 2013 年 5 月开始,Redis 的开发由 Pivotal 赞助。

    286 引用 • 248 回帖
  • danl
    168 关注
  • Docker

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的操作系统上。容器完全使用沙箱机制,几乎没有性能开销,可以很容易地在机器和数据中心中运行。

    495 引用 • 931 回帖 • 1 关注
  • JSON

    JSON (JavaScript Object Notation)是一种轻量级的数据交换格式。易于人类阅读和编写。同时也易于机器解析和生成。

    52 引用 • 190 回帖 • 1 关注
  • Latke

    Latke 是一款以 JSON 为主的 Java Web 框架。

    71 引用 • 535 回帖 • 831 关注
  • SQLite

    SQLite 是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite 是全世界使用最为广泛的数据库引擎。

    5 引用 • 7 回帖 • 7 关注
  • IPFS

    IPFS(InterPlanetary File System,星际文件系统)是永久的、去中心化保存和共享文件的方法,这是一种内容可寻址、版本化、点对点超媒体的分布式协议。请浏览 IPFS 入门笔记了解更多细节。

    21 引用 • 245 回帖 • 229 关注
  • 叶归
    9 引用 • 41 回帖 • 19 关注
  • Spark

    Spark 是 UC Berkeley AMP lab 所开源的类 Hadoop MapReduce 的通用并行框架。Spark 拥有 Hadoop MapReduce 所具有的优点;但不同于 MapReduce 的是 Job 中间输出结果可以保存在内存中,从而不再需要读写 HDFS,因此 Spark 能更好地适用于数据挖掘与机器学习等需要迭代的 MapReduce 的算法。

    74 引用 • 46 回帖 • 565 关注
  • 程序员

    程序员是从事程序开发、程序维护的专业人员。

    588 引用 • 3528 回帖
  • Unity

    Unity 是由 Unity Technologies 开发的一个让开发者可以轻松创建诸如 2D、3D 多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎。

    25 引用 • 7 回帖 • 121 关注
  • Ubuntu

    Ubuntu(友帮拓、优般图、乌班图)是一个以桌面应用为主的 Linux 操作系统,其名称来自非洲南部祖鲁语或豪萨语的“ubuntu”一词,意思是“人性”、“我的存在是因为大家的存在”,是非洲传统的一种价值观,类似华人社会的“仁爱”思想。Ubuntu 的目标在于为一般用户提供一个最新的、同时又相当稳定的主要由自由软件构建而成的操作系统。

    127 引用 • 169 回帖
  • Solidity

    Solidity 是一种智能合约高级语言,运行在 [以太坊] 虚拟机(EVM)之上。它的语法接近于 JavaScript,是一种面向对象的语言。

    3 引用 • 18 回帖 • 433 关注
  • GitHub

    GitHub 于 2008 年上线,目前,除了 Git 代码仓库托管及基本的 Web 管理界面以外,还提供了订阅、讨论组、文本渲染、在线文件编辑器、协作图谱(报表)、代码片段分享(Gist)等功能。正因为这些功能所提供的便利,又经过长期的积累,GitHub 的用户活跃度很高,在开源世界里享有深远的声望,并形成了社交化编程文化(Social Coding)。

    210 引用 • 2040 回帖
  • 创业

    你比 99% 的人都优秀么?

    82 引用 • 1395 回帖 • 1 关注
  • 设计模式

    设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的。

    200 引用 • 120 回帖
  • Ruby

    Ruby 是一种开源的面向对象程序设计的服务器端脚本语言,在 20 世纪 90 年代中期由日本的松本行弘(まつもとゆきひろ/Yukihiro Matsumoto)设计并开发。在 Ruby 社区,松本也被称为马茨(Matz)。

    7 引用 • 31 回帖 • 254 关注
  • SpaceVim

    SpaceVim 是一个社区驱动的模块化 vim/neovim 配置集合,以模块的方式组织管理插件以
    及相关配置,为不同的语言开发量身定制了相关的开发模块,该模块提供代码自动补全,
    语法检查、格式化、调试、REPL 等特性。用户仅需载入相关语言的模块即可得到一个开箱
    即用的 Vim-IDE。

    3 引用 • 31 回帖 • 110 关注
  • TextBundle

    TextBundle 文件格式旨在应用程序之间交换 Markdown 或 Fountain 之类的纯文本文件时,提供更无缝的用户体验。

    1 引用 • 2 回帖 • 80 关注
  • MongoDB

    MongoDB(来自于英文单词“Humongous”,中文含义为“庞大”)是一个基于分布式文件存储的数据库,由 C++ 语言编写。旨在为应用提供可扩展的高性能数据存储解决方案。MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似 JSON 的 BSON 格式,因此可以存储比较复杂的数据类型。

    91 引用 • 59 回帖 • 1 关注
  • Hadoop

    Hadoop 是由 Apache 基金会所开发的一个分布式系统基础架构。用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进行高速运算和存储。

    90 引用 • 122 回帖 • 619 关注
  • 星云链

    星云链是一个开源公链,业内简单的将其称为区块链上的谷歌。其实它不仅仅是区块链搜索引擎,一个公链的所有功能,它基本都有,比如你可以用它来开发部署你的去中心化的 APP,你可以在上面编写智能合约,发送交易等等。3 分钟快速接入星云链 (NAS) 测试网

    3 引用 • 16 回帖 • 1 关注