📄 11.htm
字号:
style="WORD-BREAK: break-all">
<TABLE id=SHA-1摘要算法源码(java版) align=left
border=0><TBODY>
<TR>
<TD>
<DIV class=guanggao id=SHA-1摘要算法源码(java版)><SPAN
id=ad3></SPAN></DIV>
<DIV class=guanggao id=SHA-1摘要算法源码(java版)><SPAN
id=contentAdv></SPAN></DIV></TD></TR></TBODY></TABLE>
<DIV id=VbellShow>public class SHA1
{<BR> private final int[] abcde
=
{<BR>
0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476,
0xc3d2e1f0<BR>
};
<P> //
摘要数据存储数组<BR> private int[]
digestInt = new int[5];</P>
<P> //
计算过程中的临时数据存储数组<BR> private int[]
tmpData = new int[80];</P>
<P> //
计算sha-1摘要<BR> private int
process_input_bytes(byte[] bytedata)
{<BR> //
初试化常量<BR>
System.arraycopy(abcde, 0, digestInt, 0,
abcde.length);</P>
<P> //
格式化输入字节数组,补10及长度数据<BR>
byte[] newbyte =
byteArrayFormatData(bytedata);</P>
<P> //
获取数据摘要计算的数据单元个数<BR>
int MCount = newbyte.length / 64;</P>
<P> //
循环对每个数据单元进行摘要计算<BR>
for (int pos = 0; pos < MCount; pos++)
{<BR>
//
将每个单元的数据转换成16个整型数据,并保存到tmpData的前16个数组元素中<BR>
for (int j = 0; j < 16; j++)
{<BR>
tmpData[j] = byteArrayToInt(newbyte, (pos * 64) +
(j *
4));<BR>
}</P>
<P>
//
摘要计算函数<BR>
encrypt();<BR>
}</P>
<P>
return 20;<BR> }</P>
<P> //
格式化输入字节数组格式<BR> private byte[]
byteArrayFormatData(byte[] bytedata)
{<BR> //
补0数量<BR>
int zeros = 0;</P>
<P> //
补位后总位数<BR>
int size = 0;</P>
<P> //
原始数据长度<BR>
int n = bytedata.length;</P>
<P> //
模64后的剩余位数<BR>
int m = n % 64;</P>
<P> //
计算添加0的个数以及添加10后的总长度<BR>
if (m < 56)
{<BR>
zeros = 55 -
m;<BR>
size = n - m +
64;<BR>
} else if (m == 56)
{<BR>
zeros =
63;<BR>
size = n + 8 +
64;<BR>
} else
{<BR>
zeros = 63 - m +
56;<BR>
size = (n + 64) - m +
64;<BR>
}</P>
<P> //
补位后生成的新数组内容<BR>
byte[] newbyte = new
byte[size];<BR>
//
复制数组的前面部分<BR>
System.arraycopy(bytedata, 0, newbyte, 0, n);</P>
<P> //
获得数组Append数据元素的位置<BR>
int l =
n;<BR>
//
补1操作<BR>
newbyte[l++] = (byte) 0x80;</P>
<P> //
补0操作<BR>
for (int i = 0; i < zeros; i++)
{<BR>
newbyte[l++] = (byte)
0x00;<BR>
}</P>
<P> //
计算数据长度,补数据长度位共8字节,长整型<BR>
long N = (long) n *
8;<BR>
byte h8 = (byte) (N &
0xFF);<BR>
byte h7 = (byte) ((N >> 8) &
0xFF);<BR>
byte h6 = (byte) ((N >> 16) &
0xFF);<BR>
byte h5 = (byte) ((N >> 24) &
0xFF);<BR>
byte h4 = (byte) ((N >> 32) &
0xFF);<BR>
byte h3 = (byte) ((N >> 40) &
0xFF);<BR>
byte h2 = (byte) ((N >> 48) &
0xFF);<BR>
byte h1 = (byte) (N >>
56);<BR>
newbyte[l++] =
h1;<BR>
newbyte[l++] =
h2;<BR>
newbyte[l++] =
h3;<BR>
newbyte[l++] =
h4;<BR>
newbyte[l++] =
h5;<BR>
newbyte[l++] =
h6;<BR>
newbyte[l++] =
h7;<BR>
newbyte[l++] = h8;</P>
<P>
return newbyte;<BR> }</P>
<P> private int f1(int x, int y,
int z)
{<BR>
return (x & y) | (~x &
z);<BR> }</P>
<P> private int f2(int x, int y,
int z)
{<BR>
return x ^ y ^ z;<BR> }</P>
<P> private int f3(int x, int y,
int z)
{<BR>
return (x & y) | (x & z) | (y &
z);<BR> }</P>
<P> private int f4(int x, int y)
{<BR>
return (x << y) | x >>> (32 -
y);<BR> }</P>
<P> //
单元摘要计算函数<BR> private void
encrypt()
{<BR>
for (int i = 16; i <= 79; i++)
{<BR>
tmpData[i] = f4(tmpData[i - 3] ^ tmpData[i - 8] ^
tmpData[i - 14]
^<BR>
tmpData[i - 16],
1);<BR>
}</P>
<P>
int[] tmpabcde = new int[5];</P>
<P> for
(int i1 = 0; i1 < tmpabcde.length; i1++)
{<BR>
tmpabcde[i1] =
digestInt[i1];<BR>
}</P>
<P> for
(int j = 0; j <= 19; j++)
{<BR>
int tmp = f4(tmpabcde[0], 5)
+<BR>
f1(tmpabcde[1], tmpabcde[2], tmpabcde[3]) +
tmpabcde[4]
+<BR>
tmpData[j] +
0x5a827999;<BR>
tmpabcde[4] =
tmpabcde[3];<BR>
tmpabcde[3] =
tmpabcde[2];<BR>
tmpabcde[2] = f4(tmpabcde[1],
30);<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -