-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathSm2Signature.java
More file actions
114 lines (96 loc) · 2.58 KB
/
Sm2Signature.java
File metadata and controls
114 lines (96 loc) · 2.58 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
/*
* Copyright 2014-2023 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;
public class Sm2Signature {
public final static String DEFAULT_ID = GmSSLJNI.SM2_DEFAULT_ID;
private long sm2_sign_ctx = 0;
private boolean inited = false;
private boolean do_sign = true;
public Sm2Signature(Sm2Key key, String id, boolean do_sign) {
if ((this.sm2_sign_ctx = GmSSLJNI.sm2_sign_ctx_new()) == 0) {
throw new GmSSLException("");
}
if (do_sign == true) {
if (GmSSLJNI.sm2_sign_init(this.sm2_sign_ctx, key.getPrivateKey(), id) != 1) {
throw new GmSSLException("");
}
} else {
if (GmSSLJNI.sm2_verify_init(sm2_sign_ctx, key.getPublicKey(), id) != 1) {
throw new GmSSLException("");
}
}
this.inited = true;
this.do_sign = do_sign;
}
public void reset(Sm2Key key, String id, boolean do_sign) {
if (do_sign == true) {
if (GmSSLJNI.sm2_sign_init(this.sm2_sign_ctx, key.getPrivateKey(), id) != 1) {
throw new GmSSLException("");
}
} else {
if (GmSSLJNI.sm2_verify_init(sm2_sign_ctx, key.getPublicKey(), id) != 1) {
throw new GmSSLException("");
}
}
this.inited = true;
this.do_sign = do_sign;
}
public void update(byte[] data, int offset, int len) {
if (this.inited == false) {
throw new GmSSLException("");
}
if (data == null
|| offset < 0
|| len < 0
|| offset + len <= 0
|| data.length < offset + len) {
throw new GmSSLException("");
}
if (this.do_sign == true) {
if (GmSSLJNI.sm2_sign_update(this.sm2_sign_ctx, data, offset, len) != 1) {
throw new GmSSLException("");
}
} else {
if (GmSSLJNI.sm2_verify_update(this.sm2_sign_ctx, data, offset, len) != 1) {
throw new GmSSLException("");
}
}
}
public void update(byte[] data) {
update(data, 0, data.length);
}
public byte[] sign() {
if (this.inited == false) {
throw new GmSSLException("");
}
if (this.do_sign == false) {
throw new GmSSLException("");
}
this.inited = false;
byte[] sig;
if ((sig = GmSSLJNI.sm2_sign_finish(this.sm2_sign_ctx)) == null) {
throw new GmSSLException("");
}
return sig;
}
public boolean verify(byte[] signature) {
if (this.sm2_sign_ctx == 0) {
throw new GmSSLException("");
}
if (this.do_sign == true) {
throw new GmSSLException("");
}
this.inited = false;
int ret;
if ((ret = GmSSLJNI.sm2_verify_finish(sm2_sign_ctx, signature)) != 1) {
return false;
}
return true;
}
}