package com.gzylxx.base.util;
/**
* @Class Name : SHA1;
* @Description : 鍔犲瘑绠楁硶锛岀綉缁滆浆杞?闈炲師鍒?
* @Author : 鍚磋穬骞?
* @Creation Date : 2012-6-15 涓嬪崍05:13:28 ;
* @ModificationHistory 1;
*/
public class SHA1 {
private final int[] abcde = { 0x67452301, 0xefcdab89, 0x98badcfe,
0x10325476, 0xc3d2e1f0 };
// 鎽樿鏁版嵁瀛樺偍鏁扮粍
private int[] digestInt = new int[5];
// 璁$畻杩囩▼涓殑涓存椂鏁版嵁瀛樺偍鏁扮粍
private int[] tmpData = new int[80];
// 璁$畻sha-1鎽樿
private int process_input_bytes(byte[] bytedata) {
// 鍒濊瘯鍖栧父閲?
System.arraycopy(abcde, 0, digestInt, 0, abcde.length);
// 鏍煎紡鍖栬緭鍏ュ瓧鑺傛暟缁勶紝琛?0鍙婇暱搴︽暟鎹?
byte[] newbyte = byteArrayFormatData(bytedata);
// 鑾峰彇鏁版嵁鎽樿璁$畻鐨勬暟鎹崟鍏冧釜鏁?
int MCount = newbyte.length / 64;
// 寰幆瀵规瘡涓暟鎹崟鍏冭繘琛屾憳瑕佽绠?
for (int pos = 0; pos < MCount; pos++) {
// 灏嗘瘡涓崟鍏冪殑鏁版嵁杞崲鎴?6涓暣鍨嬫暟鎹紝骞朵繚瀛樺埌tmpData鐨勫墠16涓暟缁勫厓绱犱腑
for (int j = 0; j < 16; j++) {
tmpData[j] = byteArrayToInt(newbyte, (pos * 64) + (j * 4));
}
// 鎽樿璁$畻鍑芥暟
encrypt();
}
return 20;
}
// 鏍煎紡鍖栬緭鍏ュ瓧鑺傛暟缁勬牸寮?
private byte[] byteArrayFormatData(byte[] bytedata) {
// 琛?鏁伴噺
int zeros = 0;
// 琛ヤ綅鍚庢?浣嶆暟
int size = 0;
// 鍘熷鏁版嵁闀垮害
int n = bytedata.length;
// 妯?4鍚庣殑鍓╀綑浣嶆暟
int m = n % 64;
// 璁$畻娣诲姞0鐨勪釜鏁颁互鍙婃坊鍔?0鍚庣殑鎬婚暱搴?
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;
// 琛?鎿嶄綔
newbyte[l++] = (byte) 0x80;
// 琛?鎿嶄綔
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瀛楄妭鏁扮粍杞崲涓烘暣鏁?
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);
}
// 鏁存暟杞崲涓?瀛楄妭鏁扮粍
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;
}
// 灏嗗瓧鑺傝浆鎹负鍗佸叚杩涘埗瀛楃涓?
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;
}
// 灏嗗瓧鑺傛暟缁勮浆鎹负鍗佸叚杩涘埗瀛楃涓?
private static String byteArrayToHexString(byte[] bytearray) {
String strDigest = "";
for (int i = 0; i < bytearray.length; i++) {
strDigest += byteToHexString(bytearray[i]);
}
return strDigest;
}
// 璁$畻sha-1鎽樿锛岃繑鍥炵浉搴旂殑瀛楄妭鏁扮粍
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;
}
// 璁$畻sha-1鎽樿锛岃繑鍥炵浉搴旂殑鍗佸叚杩涘埗瀛楃涓?
public String getDigestOfString(byte[] byteData) {
return byteArrayToHexString(getDigestOfBytes(byteData));
}
public static void main(String[] args) {
String data = "wy932u";
System.out.println(data);
String digest = new SHA1().getDigestOfString(data.getBytes());
System.out.println(digest);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ss3.zip (71个子文件)
ss3
conf
applicationContext-security.xml 6KB
applicationContext-biz.xml 167B
.project 2KB
.mymetadata 282B
src
com
gzylxx
base
util
SHA1.java 6KB
dao
SSUserDao.java 415B
entity
SSResource.java 4KB
SSUser.java 1KB
SSGroup.java 784B
security
MyUsernamePasswordAuthenticationFilter.java 5KB
robin
erp
fwk
security
MyAccessDecisionManager.java 2KB
MyUserDetailService.java 1KB
MyFilterSecurityInterceptor.java 2KB
MyInvocationSecurityMetadataSource.java 2KB
WebRoot
WEB-INF
classes
applicationContext-security.xml 6KB
applicationContext-biz.xml 167B
com
gzylxx
base
util
SHA1.class 5KB
dao
SSUserDao.class 497B
entity
SSGroup.class 2KB
SSUser.class 2KB
SSResource.class 5KB
security
MyUsernamePasswordAuthenticationFilter.class 5KB
robin
erp
fwk
security
MyFilterSecurityInterceptor.class 3KB
MyAccessDecisionManager.class 3KB
MyUserDetailService.class 2KB
MyInvocationSecurityMetadataSource.class 3KB
lib
commons-lang3-3.1.jar 308KB
spring-security-taglibs-3.0.2.RELEASE.jar 18KB
kaptcha-2.3.2.jar 421KB
org.springframework.expression-3.0.1.RELEASE-A.jar 152KB
org.springframework.instrument.tomcat-3.0.1.RELEASE-A.jar 5KB
org.springframework.core-3.0.1.RELEASE-A.jar 347KB
org.springframework.jms-3.0.1.RELEASE-A.jar 179KB
org.springframework.aop-3.0.1.RELEASE-A.jar 313KB
org.springframework.web-3.0.1.RELEASE-A.jar 352KB
spring-security-core-3.0.2.RELEASE.jar 298KB
jstl.jar 17KB
org.springframework.beans-3.0.1.RELEASE-A.jar 535KB
org.springframework.context-3.0.1.RELEASE-A.jar 642KB
org.springframework.test-3.0.1.RELEASE-A.jar 200KB
org.springframework.aspects-3.0.1.RELEASE-A.jar 29KB
org.springframework.web.struts-3.0.1.RELEASE-A.jar 524KB
org.springframework.transaction-3.0.1.RELEASE-A.jar 223KB
spring-security-config-3.0.2.RELEASE.jar 157KB
org.springframework.asm-3.0.1.RELEASE-A.jar 52KB
org.springframework.web.servlet-3.0.1.RELEASE-A.jar 389KB
org.springframework.jdbc-3.0.1.RELEASE-A.jar 369KB
org.springframework.context.support-3.0.1.RELEASE-A.jar 98KB
org.springframework.orm-3.0.1.RELEASE-A.jar 324KB
org.springframework.instrument-3.0.1.RELEASE-A.jar 2KB
commons-logging.jar 54KB
org.springframework.web.portlet-3.0.1.RELEASE-A.jar 169KB
standard.jar 287KB
spring-security-web-3.0.2.RELEASE.jar 230KB
org.springframework.oxm-3.0.1.RELEASE-A.jar 56KB
commons-lang-2.3.jar 240KB
web.xml 952B
i.jsp 836B
index.jsp 888B
META-INF
MANIFEST.MF 39B
login.jsp 2KB
403.jsp 811B
.myeclipse
builder
spring
config
message_zh_CN.properties 89B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.jdt.core.prefs 629B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.core.resources.prefs 311B
org.eclipse.wst.common.component 522B
.jsdtscope 500B
.classpath 3KB
共 71 条
- 1
资源评论
磨灭慈生002
- 粉丝: 3
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- pta题库答案c语言之排序4统计工龄.zip
- pta题库答案c语言之树结构7堆中的路径.zip
- pta题库答案c语言之树结构3TreeTraversalsAgain.zip
- pta题库答案c语言之树结构2ListLeaves.zip
- pta题库答案c语言之树结构1树的同构.zip
- 基于C++实现民航飞行与地图简易管理系统可执行程序+说明+详细注释.zip
- pta题库答案c语言之复杂度1最大子列和问题.zip
- 三维装箱问题(Three-Dimensional Bin Packing Problem,3D-BPP)是一个经典的组合优化问题
- 以下是一些关于Linux线程同步的基本概念和方法.txt
- 以下是一个简化的示例,它使用pygame库来模拟烟花动画的框架.txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功