forked from ofdrw/ofdrw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoSealSignTest.java
69 lines (61 loc) · 2.54 KB
/
NoSealSignTest.java
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
package org.ofdrw.sign.signContainer;
import org.junit.jupiter.api.Test;
import org.ofdrw.gm.cert.PKCS12Tools;
import org.ofdrw.gm.ses.v1.SESeal;
import org.ofdrw.reader.OFDReader;
import org.ofdrw.sign.OFDSigner;
import org.ofdrw.sign.SignMode;
import org.ofdrw.sign.stamppos.NormalStampPos;
import org.ofdrw.sign.verify.OFDValidator;
import org.ofdrw.sign.verify.container.SESV1ValidateContainer;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.PrivateKey;
import java.security.cert.Certificate;
/**
* 没有提供电子印章签章测试
*
* @author 权观宇
* @since 2020-05-18 19:48:29
*/
public class NoSealSignTest {
@Test
public void testSign() throws Exception{
Path userP12Path = Paths.get("src/test/resources", "USER.p12");
Path sealPath = Paths.get("src/test/resources", "UserV1.esl");
PrivateKey prvKey = PKCS12Tools.ReadPrvKey(userP12Path, "private", "777777");
Certificate signCert = PKCS12Tools.ReadUserCert(userP12Path, "private", "777777");
SESeal seal = SESeal.getInstance(Files.readAllBytes(sealPath));
Path src = Paths.get("src/test/resources", "helloworld.ofd");
Path out = Paths.get("target/NoSealV1Sign.ofd");
// 1. 构造签名引擎
try (OFDReader reader = new OFDReader(src);
OFDSigner signer = new OFDSigner(reader, out)) {
SESV1Container signContainer = new SESV1ContainerNoSeal(prvKey, seal, signCert);
// 2. 设置签名模式
// signer.setSignMode(SignMode.WholeProtected);
signer.setSignMode(SignMode.ContinueSign);
// 3. 设置签名使用的扩展签名容器
signer.setSignContainer(signContainer);
// 4. 设置显示位置
signer.addApPos(new NormalStampPos(1, 50, 50, 40, 40));
// 5. 执行签名
signer.exeSign();
// 6. 关闭签名引擎,生成文档。
}
System.out.println(">> 生成文件位置: " + out.toAbsolutePath().toAbsolutePath());
}
@Test
void validate() throws IOException, GeneralSecurityException {
Path src = Paths.get("target/NoSealV1Sign.ofd");
try (OFDReader reader = new OFDReader(src);
OFDValidator validator = new OFDValidator(reader)) {
validator.setValidator(new SESV1ValidateContainer());
validator.exeValidate();
System.out.println(">> 验证通过");
}
}
}