-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSM3Digest.java
More file actions
101 lines (86 loc) · 2.76 KB
/
SM3Digest.java
File metadata and controls
101 lines (86 loc) · 2.76 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
/*
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package org.gmssl.crypto.digest;
import org.gmssl.GmSSLException;
import org.gmssl.GmSSLJNI;
import java.security.MessageDigestSpi;
/**
* @author yongfeili
* @email 290836576@qq.com
* @date 2024/09/07
* @description
* The SM3 cryptographic hash function can compute input data of arbitrary length into a fixed hash value of 32 bytes.
*/
public class SM3Digest extends MessageDigestSpi {
private final static int DIGEST_SIZE = GmSSLJNI.SM3_DIGEST_SIZE;
private long sm3_ctx = 0;
public SM3Digest() {
init();
}
/**
* You can call the update method multiple times. After all the data has been input, finally call the digest method to obtain the SM3 hash value of the entire data.
* @param input the input byte to be processed.
*/
@Override
protected void engineUpdate(byte input) {
byte[] data = new byte[]{input};
this.update(data, 0, data.length);
}
@Override
protected void engineUpdate(byte[] input, int offset, int len) {
this.update(input, offset, len);
}
@Override
protected byte[] engineDigest() {
return this.digest();
}
/**
* If you need to calculate different SM3 hash values for multiple sets of data, you can use the reset method to reset,
* and then call the update and digest methods again to compute the hash value of a new set of data.
*/
@Override
protected void engineReset() {
this.reset();
}
private void init(){
if ((sm3_ctx = GmSSLJNI.sm3_ctx_new()) == 0) {
throw new GmSSLException("");
}
if (GmSSLJNI.sm3_init(sm3_ctx) != 1) {
throw new GmSSLException("");
}
}
private void update(byte[] data, int offset, int len) {
if (data == null
|| offset < 0
|| len < 0
|| offset + len <= 0
|| data.length < offset + len) {
throw new GmSSLException("");
}
if (GmSSLJNI.sm3_update(sm3_ctx, data, offset, len) != 1) {
throw new GmSSLException("");
}
}
private byte[] digest() {
byte[] dgst = new byte[DIGEST_SIZE];
if (GmSSLJNI.sm3_finish(sm3_ctx, dgst) != 1) {
throw new GmSSLException("");
}
if (GmSSLJNI.sm3_init(sm3_ctx) != 1) {
throw new GmSSLException("");
}
return dgst;
}
private void reset() {
if (GmSSLJNI.sm3_init(sm3_ctx) != 1) {
throw new GmSSLException("");
}
}
}