本页内容
验证数字签名
验证数字签名包含两个部分:验证签名有效和验证证书可信。
签名有效代表文档未被篡改,证书可信代表签名者是可信任的。一般来说,签名有效且证书可信才代表一个签名验证通过。
验证数字签名关键代码如下:
swift
if let url = URL(string: pdfPath) {
let document = CPDFDocument(url: url)
if let signatures = document?.signatures {
// 遍历所有数字签名
for signature in signatures {
signature.verifySignature(with: document)
if let signer = signature.signers.first, let certificate = signer.certificates.first {
var isSignVerified = true
var isCertTrusted = true
if !signer.isCertTrusted {
isCertTrusted = false
}
if !signer.isSignVerified {
isSignVerified = false
}
if isSignVerified && isCertTrusted {
//签名有效,证书受信任
//执行相应的操作
} else if isSignVerified && !isCertTrusted {
//签名有效,但证书不受信任
//执行相应的操作
} else if !isSignVerified && !isCertTrusted {
// 签名无效
// 执行相应的操作
} else {
// 签名无效
// 执行相应的操作
}
}
}
}
}
objective-c
NSURL *url = [NSURL fileURLWithPath:@"file path"];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
NSArray *signatures = [document signatures];
// 遍历所有数字签名
for (CPDFSignature *signature in signatures) {
[sign verifySignatureWithDocument:document];
CPDFSigner *signer = signature.signers.firstObject;
CPDFSignatureCertificate * cer = signer.certificates.firstObject;
BOOL isSignVerified = YES;
BOOL isCertTrusted = YES;
if (!signer.isCertTrusted) {
isCertTrusted = NO;
}
if (!signer.isSignVerified) {
isSignVerified = NO;
}
if (isSignVerified && isCertTrusted) {
//签名有效,证书受信任
//执行相应的操作
} else if(isSignVerified && !isCertTrusted) {
//签名有效,但证书不受信任
//执行相应的操作
} else if(!isSignVerified && !isCertTrusted){
} else {
//签名无效
//执行相应的操作
}
}