package util;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class sha1test {
private final int[] abcde = { 0x67452301, 0xefcdab89, 0x98badcfe,
0x10325476, 0xc3d2e1f0 };
// 摘要数据存储数组
private int[] digestInt = new int[5];
// 计算过程中的临时数据存储数组
private int[] tmpData = new int[80];
/**
* 计算sha-1摘要
*
* @param bytedata
* @return
*/
private int process_input_bytes(byte[] bytedata) {
// 初试化常量
System.arraycopy(abcde, 0, digestInt, 0, abcde.length);
// 格式化输入字节数组,补10及长度数据
byte[] newbyte = byteArrayFormatData(bytedata);
// 获取数据摘要计算的数据单元个数
int MCount = newbyte.length / 64;
// 循环对每个数据单元进行摘要计算
for (int pos = 0; pos < MCount; pos++) {
// 将每个单元的数据转换成16个整型数据,并保存到tmpData的前16个数组元素中
for (int j = 0; j < 16; j++) {
tmpData[j] = byteArrayToInt(newbyte, (pos * 64) + (j * 4));
}
// 摘要计算函数
encrypt();
}
return 20;
}
/**
* 格式化输入字节数组格式
*
* @param bytedata
* @return
*/
private byte[] byteArrayFormatData(byte[] bytedata) {
// 补0数量
int zeros = 0;
// 补位后总位数
int size = 0;
// 原始数据长度
int n = bytedata.length;
// 模64后的剩余位数
int m = n % 64;
// 计算添加0的个数以及添加10后的总长度
if (m < 56) {
zeros = 55 - m;
size = n - m + 64;
} else if (m == 56) {
zeros = 63;
size = n + 8 + 64;
} else {
zeros = 63 - m + 56;
size = (n + 64) - m + 64;
}
// 补位后生成的新数组内容
byte[] newbyte = new byte[size];
// 复制数组的前面部分
System.arraycopy(bytedata, 0, newbyte, 0, n);
// 获得数组Append数据元素的位置
int l = n;
// 补1操作
newbyte[l++] = (byte) 0x80;
// 补0操作
for (int i = 0; i < zeros; i++) {
newbyte[l++] = (byte) 0x00;
}
// 计算数据长度,补数据长度位共8字节,长整型
long N = (long) n * 8;
byte h8 = (byte) (N & 0xFF);
byte h7 = (byte) ((N >> 8) & 0xFF);
byte h6 = (byte) ((N >> 16) & 0xFF);
byte h5 = (byte) ((N >> 24) & 0xFF);
byte h4 = (byte) ((N >> 32) & 0xFF);
byte h3 = (byte) ((N >> 40) & 0xFF);
byte h2 = (byte) ((N >> 48) & 0xFF);
byte h1 = (byte) (N >> 56);
newbyte[l++] = h1;
newbyte[l++] = h2;
newbyte[l++] = h3;
newbyte[l++] = h4;
newbyte[l++] = h5;
newbyte[l++] = h6;
newbyte[l++] = h7;
newbyte[l++] = h8;
return newbyte;
}
private int f1(int x, int y, int z) {
return (x & y) | (~x & z);
}
private int f2(int x, int y, int z) {
return x ^ y ^ z;
}
private int f3(int x, int y, int z) {
return (x & y) | (x & z) | (y & z);
}
private int f4(int x, int y) {
return (x << y) | x >>> (32 - y);
}
/**
* 单元摘要计算函数
*/
private void encrypt() {
for (int i = 16; i <= 79; i++) {
tmpData[i] = f4(tmpData[i - 3] ^ tmpData[i - 8] ^ tmpData[i - 14]
^ tmpData[i - 16], 1);
}
int[] tmpabcde = new int[5];
for (int i1 = 0; i1 < tmpabcde.length; i1++) {
tmpabcde[i1] = digestInt[i1];
}
for (int j = 0; j <= 19; j++) {
int tmp = f4(tmpabcde[0], 5)
+ f1(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4]
+ tmpData[j] + 0x5a827999;
tmpabcde[4] = tmpabcde[3];
tmpabcde[3] = tmpabcde[2];
tmpabcde[2] = f4(tmpabcde[1], 30);
tmpabcde[1] = tmpabcde[0];
tmpabcde[0] = tmp;
}
for (int k = 20; k <= 39; k++) {
int tmp = f4(tmpabcde[0], 5)
+ f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4]
+ tmpData[k] + 0x6ed9eba1;
tmpabcde[4] = tmpabcde[3];
tmpabcde[3] = tmpabcde[2];
tmpabcde[2] = f4(tmpabcde[1], 30);
tmpabcde[1] = tmpabcde[0];
tmpabcde[0] = tmp;
}
for (int l = 40; l <= 59; l++) {
int tmp = f4(tmpabcde[0], 5)
+ f3(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4]
+ tmpData[l] + 0x8f1bbcdc;
tmpabcde[4] = tmpabcde[3];
tmpabcde[3] = tmpabcde[2];
tmpabcde[2] = f4(tmpabcde[1], 30);
tmpabcde[1] = tmpabcde[0];
tmpabcde[0] = tmp;
}
for (int m = 60; m <= 79; m++) {
int tmp = f4(tmpabcde[0], 5)
+ f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4]
+ tmpData[m] + 0xca62c1d6;
tmpabcde[4] = tmpabcde[3];
tmpabcde[3] = tmpabcde[2];
tmpabcde[2] = f4(tmpabcde[1], 30);
tmpabcde[1] = tmpabcde[0];
tmpabcde[0] = tmp;
}
for (int i2 = 0; i2 < tmpabcde.length; i2++) {
digestInt[i2] = digestInt[i2] + tmpabcde[i2];
}
for (int n = 0; n < tmpData.length; n++) {
tmpData[n] = 0;
}
}
/**
* 4字节数组转换为整数
*
* @param bytedata
* @param i
* @return
*/
private int byteArrayToInt(byte[] bytedata, int i) {
return ((bytedata[i] & 0xff) << 24) | ((bytedata[i + 1] & 0xff) << 16)
| ((bytedata[i + 2] & 0xff) << 8) | (bytedata[i + 3] & 0xff);
}
//
/**
* 整数转换为4字节数组
*
* @param intValue
* @param byteData
* @param i
*/
private void intToByteArray(int intValue, byte[] byteData, int i) {
byteData[i] = (byte) (intValue >>> 24);
byteData[i + 1] = (byte) (intValue >>> 16);
byteData[i + 2] = (byte) (intValue >>> 8);
byteData[i + 3] = (byte) intValue;
}
/**
* 将字节转换为十六进制字符串
*
* @param ib
* @return
*/
private static String byteToHexString(byte ib) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char[] ob = new char[2];
ob[0] = Digit[(ib >>> 4) & 0X0F];
ob[1] = Digit[ib & 0X0F];
String s = new String(ob);
return s;
}
/**
* 将字节数组转换为十六进制字符串
*
* @param bytearray
* @return
*/
private static String byteArrayToHexString(byte[] bytearray) {
String strDigest = "";
for (int i = 0; i < bytearray.length; i++) {
strDigest += byteToHexString(bytearray[i]);
}
return strDigest;
}
/**
* 计算sha-1摘要,返回相应的字节数组
*
* @param byteData
* @return
*/
public byte[] getDigestOfBytes(byte[] byteData) {
process_input_bytes(byteData);
byte[] digest = new byte[20];
for (int i = 0; i < digestInt.length; i++) {
intToByteArray(digestInt[i], digest, i * 4);
}
return digest;
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
wechatpay_JSAPI.rar (79个子文件)
wechatpay_JSAPI
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 414B
org.eclipse.core.resources.prefs 104B
org.eclipse.wst.common.component 480B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 522B
org.eclipse.jdt.core.prefs 364B
readme.docx 132KB
src
wechat
WxConfig.java 569B
util
Sha1Util.java 2KB
SHA1.java 7KB
Dom4JTest2.java 2KB
HttpResponse.java 2KB
sha1test.java 9KB
Xmltest.java 4KB
GetWxOrderno.java 5KB
TenpayUtil.java 4KB
WeixinOauth2Token.java 1KB
MyUtil.java 2KB
RandomString.java 633B
LoadProperties.java 2KB
CommonUtil.java 4KB
MD5Util.java 1KB
DuXMLDoc.java 5KB
HttpRequest.java 3KB
HttpResultType.java 693B
MyX509TrustManager.java 683B
UtilDate.java 2KB
HttpClientConnectionManager.java 2KB
TrustAnyTrustManager.java 582B
RequestHandler.java 6KB
Sign.java 3KB
MySSLSocketFactory.java 1KB
.project 1KB
WebRoot
wxpay
wxpay.jsp 8KB
META-INF
MANIFEST.MF 39B
WEB-INF
lib
ezmorph-1.0.6.jar 84KB
httpcore-4.2.4.jar 222KB
dom4j-1.6.1.jar 307KB
commons-cli-1.0.jar 29KB
commons-collections-3.2.1.jar 562KB
httpcore-4.3.2.jar 276KB
jaxen-1.1-beta-6.jar 239KB
poi-3.10.1-20140818.jar 1.86MB
commons-dbcp-20030825.184428.jar 92KB
commons-beanutils-1.8.3.jar 227KB
slf4j-api-1.7.7.jar 29KB
junit-4.8.2.jar 232KB
slf4j-simple-1.7.7.jar 10KB
commons-httpclient-3.0.1.jar 273KB
commons-httpclient-3.1.jar 298KB
ueditor-1.1.2.jar 30KB
sqljdbc4.jar 455KB
xstream-1.4.7.jar 519KB
rt.jar 31KB
httpclient-cache-4.3.4.jar 146KB
json.jar 45KB
commons-logging-1.1.1.jar 59KB
commons-beanutils-1.8.0.jar 226KB
httpclient-4.2.5.jar 423KB
servlet-api.jar 141KB
commons-lang-2.6.jar 278KB
fluent-hc-4.3.4.jar 29KB
httpclient-4.3.5.jar 577KB
jdom-1.0.jar 150KB
commons-logging-1.1.3.jar 61KB
json-lib-2.3-jdk15.jar 148KB
commons-codec-1.7.jar 254KB
commons-codec-1.6.jar 227KB
commons-codec-1.9.jar 258KB
xmlpull-1.1.3.1.jar 7KB
commons-lang-2.5.jar 273KB
commons-io-2.4.jar 181KB
json-lib-2.4-jdk15.jar 155KB
commons-pool-1.6.jar 109KB
xpp3_min-1.1.4c.jar 24KB
commons-fileupload-1.3.1.jar 67KB
js
jquery.liMarquee.js 35KB
jquery-1.8.3.min.js 91KB
共 79 条
- 1
资源评论
qq_17084985
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功