forked from ofdrw/ofdrw
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
资源定位器无法获取根目录下文件的问题
- Loading branch information
Showing
11 changed files
with
199 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
ofdrw-sign/src/main/java/org/ofdrw/sign/verify/SESValidateContainer.java
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
ofdrw-sign/src/main/java/org/ofdrw/sign/verify/SignedDataValidateContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.ofdrw.sign.verify; | ||
|
||
import org.ofdrw.core.signatures.SigType; | ||
import org.ofdrw.sign.verify.exceptions.InvalidSignedValueException; | ||
|
||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
/** | ||
* 签名数据验证 | ||
* | ||
* @author 权观宇 | ||
* @since 2020-04-22 02:25:08 | ||
*/ | ||
public interface SignedDataValidateContainer { | ||
|
||
/** | ||
* 签名数据验证 | ||
* <p> | ||
* 如果验证不通过请抛出异常 | ||
* | ||
* @param type 电子签名类型(Sign/Seal) | ||
* @param signAlgName 签名算法名称或OID | ||
* @param tbsContent 待签章内容 | ||
* @param signedValue 电子签章数据或签名值 | ||
* @throws InvalidSignedValueException 电子签章数据失效 | ||
* @throws IOException IO异常 | ||
* @throws GeneralSecurityException 运算过程中异常 | ||
*/ | ||
void validate(SigType type, String signAlgName, byte[] tbsContent, byte[] signedValue) throws InvalidSignedValueException, IOException, GeneralSecurityException; | ||
} |
52 changes: 52 additions & 0 deletions
52
ofdrw-sign/src/main/java/org/ofdrw/sign/verify/container/DigitalSignContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.ofdrw.sign.verify.container; | ||
|
||
import org.bouncycastle.jcajce.provider.asymmetric.x509.CertificateFactory; | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
import org.ofdrw.core.signatures.SigType; | ||
import org.ofdrw.sign.verify.SignedDataValidateContainer; | ||
import org.ofdrw.sign.verify.exceptions.InvalidSignedValueException; | ||
|
||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PublicKey; | ||
import java.security.Signature; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.X509Certificate; | ||
|
||
/** | ||
* 数字签名验证容器 | ||
* | ||
* @author 权观宇 | ||
* @since 2020-04-22 03:22:22 | ||
*/ | ||
public class DigitalSignContainer implements SignedDataValidateContainer { | ||
/** | ||
* 验证使用的公钥 | ||
*/ | ||
public PublicKey pk; | ||
|
||
public DigitalSignContainer(PublicKey pk) { | ||
if (pk == null) { | ||
throw new IllegalArgumentException("验证使用的公钥参数(pk)不能为空"); | ||
} | ||
this.pk = pk; | ||
} | ||
|
||
public DigitalSignContainer(Certificate certificate) { | ||
this(certificate.getPublicKey()); | ||
} | ||
|
||
@Override | ||
public void validate(SigType type, String alg, byte[] tbsContent, byte[] signedValue) | ||
throws InvalidSignedValueException, GeneralSecurityException { | ||
if (type != SigType.Sign) { | ||
throw new IllegalArgumentException("签名类型(type)必须是 Sign,不支持电子印章验证"); | ||
} | ||
Signature sg = Signature.getInstance(alg, new BouncyCastleProvider()); | ||
sg.initVerify(pk); | ||
if (!sg.verify(signedValue)) { | ||
throw new InvalidSignedValueException("签名值不一致"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
ofdrw-sign/src/test/java/org/ofdrw/sign/signContainer/GMDigestSignatureContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.ofdrw.sign.signContainer; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.ofdrw.gm.cert.PKCS12Tools; | ||
import org.ofdrw.reader.OFDReader; | ||
import org.ofdrw.sign.OFDSigner; | ||
import org.ofdrw.sign.SignMode; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.security.GeneralSecurityException; | ||
import java.security.PrivateKey; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
* 数据签名演示 | ||
* | ||
* @author 权观宇 | ||
* @since 2020-04-22 03:16:01 | ||
*/ | ||
class GMDigestSignatureContainerTest { | ||
|
||
/** | ||
* OFD电子签名演示 | ||
*/ | ||
@Test | ||
void testDigestSign() throws GeneralSecurityException, IOException { | ||
Path userP12Path = Paths.get("src/test/resources", "USER.p12"); | ||
PrivateKey prvKey = PKCS12Tools.ReadPrvKey(userP12Path, "private", "777777"); | ||
|
||
Path src = Paths.get("src/test/resources", "helloworld.ofd"); | ||
Path out = Paths.get("target/DigestSign.ofd"); | ||
// 1. 构造签名引擎 | ||
try (OFDReader reader = new OFDReader(src); | ||
OFDSigner signer = new OFDSigner(reader, out)) { | ||
GMDigestSignatureContainer signContainer = new GMDigestSignatureContainer(prvKey); | ||
// 2. 设置签名模式 | ||
// signer.setSignMode(SignMode.WholeProtected); | ||
signer.setSignMode(SignMode.ContinueSign); | ||
// 3. 设置签名使用的扩展签名容器 | ||
signer.setSignContainer(signContainer); | ||
// 4. 执行签名 | ||
signer.exeSign(); | ||
// 5. 关闭签名引擎,生成文档。 | ||
} | ||
System.out.println(">> 生成文件位置: " + out.toAbsolutePath().toAbsolutePath()); | ||
} | ||
|
||
} |
Oops, something went wrong.