import java.io.UnsupportedEncodingException;

import java.security.PrivateKey;

import java.security.SignatureException;

import org.apache.commons.codec.digest.DigestUtils;

/**

* MD5签名处理核心文件

* */

public class MD5 {

/**

* 签名字符串

*

* @param text

*            需要签名的字符串

* @param key

*            密钥

* @param input_charset

*            编码格式

* @return 签名结果

*/

public static String sign(String text, String key, String charset) throws Exception {

text = text + key;

return DigestUtils.md5Hex(getContentBytes(text, charset));

}

/**

* 签名字符串

*

* @param text

*            需要签名的字符串

* @param key

*            密钥

* @param input_charset

*            编码格式

* @return 签名结果

* @deprecated 无替代方法

*/

public static String sign(String text, PrivateKey key, String charset) throws Exception {

throw new UnsupportedOperationException();

}

/**

* 签名字符串

*

* @param text

*            需要签名的字符串

* @param sign

*            签名结果

* @param key

*            密钥

* @param input_charset

*            编码格式

* @return 签名结果

*/

public static boolean verify(String text, String sign, String key, String charset)

throws Exception {

text = text + key;

String mysign = DigestUtils.md5Hex(getContentBytes(text, charset));

if (mysign.equals(sign)) {

return true;

} else {

return false;

}

}

/**

* @param content

* @param charset

* @return

* @throws SignatureException

* @throws UnsupportedEncodingException

*/

private static byte[] getContentBytes(String content, String charset) {

if (charset == null || "".equals(charset)) {

return content.getBytes();

}

try {

return content.getBytes(charset);

} catch (UnsupportedEncodingException e) {

throw new RuntimeException("签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);

}

}

}

最新文章

  1. php页面编码与字符操作
  2. 王总QQ聊天对话
  3. 樱花漫地集于我心,蝶舞纷飞祈愿相随---总结 适者:survival of the fittest 适者:survival of the fittest
  4. JQuery的Ajax跨域请求原理概述及实例
  5. RSA算法小记
  6. MD5加密算法
  7. hihoCoder:#1079(线段树+离散化)
  8. JedisPool连接池实现难点
  9. LNK1123: 转换到 COFF 期间失败: 文件无效或损坏[汇总]
  10. 关于tcc、tlink的编译链接机制的研究
  11. C库专题(Day1)
  12. Struts2详细说明
  13. java 打开浏览器 url
  14. JAVA课程设计-学生信息管理系统(个人博客)
  15. [ZJOI2007] 矩阵游戏
  16. python excel写入及追加写入
  17. linux服务器部署tomcat和Nginx
  18. centos6.5上安装redis3.2.1遇见的坑
  19. Delphi与各数据库数据类型比较
  20. noip第8课资料

热门文章

  1. mongodb驱动接口
  2. Rendering in UE4
  3. mysql技巧一则-避免重复插入相同数据
  4. django migrate报错:1005 - Can't create table xxx (errno: 150 "Foreign key constraint is incorrectly formed")
  5. object store in javascript
  6. forword动作
  7. 11-Flutter移动电商实战-首页_屏幕适配方案和制作
  8. .net core 多sdk 多版本 环境切换
  9. WinDbg常用命令系列---单步执行p*
  10. 集成omnibus-ctl+ chef 制作一个可配置的软件包