forked from morethink/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSM3Digest.java
More file actions
147 lines (125 loc) · 3.34 KB
/
SM3Digest.java
File metadata and controls
147 lines (125 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package algorithm.security;
/**
* @author 李文浩
* @version 2017/6/27.
*/
public class SM3Digest {
/**
* SM3值的长度
*/
private static final int BYTE_LENGTH = 32;
/**
* SM3分组长度
*/
private static final int BLOCK_LENGTH = 64;
/**
* 缓冲区长度
*/
private static final int BUFFER_LENGTH = BLOCK_LENGTH * 1;
/**
* 缓冲区
*/
private byte[] xBuf = new byte[BUFFER_LENGTH];
/**
* 缓冲区偏移量
*/
private int xBufOff;
/**
* 初始向量
*/
private byte[] V = SM3.iv.clone();
private int cntBlock = 0;
public SM3Digest() {
}
public SM3Digest(SM3Digest t) {
System.arraycopy(t.xBuf, 0, this.xBuf, 0, t.xBuf.length);
this.xBufOff = t.xBufOff;
System.arraycopy(t.V, 0, this.V, 0, t.V.length);
}
/**
* SM3结果输出
*
* @return
* @param out 保存SM3结构的缓冲区
* @param outOff 缓冲区偏移量
*/
public int doFinal(byte[] out, int outOff) {
byte[] tmp = doFinal();
System.arraycopy(tmp, 0, out, 0, tmp.length);
return BYTE_LENGTH;
}
public void reset() {
xBufOff = 0;
cntBlock = 0;
V = SM3.iv.clone();
}
/**
* 明文输入
*
* @param in 明文输入缓冲区
* @param inOff 缓冲区偏移量
* @param len 明文长度
*/
public void update(byte[] in, int inOff, int len) {
int partLen = BUFFER_LENGTH - xBufOff;
int inputLen = len;
int dPos = inOff;
if (partLen < inputLen) {
System.arraycopy(in, dPos, xBuf, xBufOff, partLen);
inputLen -= partLen;
dPos += partLen;
doUpdate();
while (inputLen > BUFFER_LENGTH) {
System.arraycopy(in, dPos, xBuf, 0, BUFFER_LENGTH);
inputLen -= BUFFER_LENGTH;
dPos += BUFFER_LENGTH;
doUpdate();
}
}
System.arraycopy(in, dPos, xBuf, xBufOff, inputLen);
xBufOff += inputLen;
}
private void doUpdate() {
byte[] B = new byte[BLOCK_LENGTH];
for (int i = 0; i < BUFFER_LENGTH; i += BLOCK_LENGTH) {
System.arraycopy(xBuf, i, B, 0, B.length);
doHash(B);
}
xBufOff = 0;
}
private void doHash(byte[] B) {
byte[] tmp = SM3.CF(V, B);
System.arraycopy(tmp, 0, V, 0, V.length);
cntBlock++;
}
private byte[] doFinal() {
byte[] B = new byte[BLOCK_LENGTH];
byte[] buffer = new byte[xBufOff];
System.arraycopy(xBuf, 0, buffer, 0, buffer.length);
byte[] tmp = SM3.padding(buffer, cntBlock);
for (int i = 0; i < tmp.length; i += BLOCK_LENGTH) {
System.arraycopy(tmp, i, B, 0, B.length);
doHash(B);
}
return V;
}
public void update(byte in) {
byte[] buffer = new byte[]{in};
update(buffer, 0, 1);
}
public int getDigestSize() {
return BYTE_LENGTH;
}
public static boolean f(int i){
if (i==1){
System.out.println("true");
return false;
}
System.out.println("false");
return false;
}
public static void main(String[] args) {
if (f(1)&&f(0)){
}
}
}