增加二维码、条形码签名验签测试
This commit is contained in:
@@ -46,12 +46,14 @@ public class CertUtil {
|
||||
return (X509Certificate) CF.generateCertificate(new ByteArrayInputStream(certificateString.getBytes())); // 将文件流的证书转化为证书类
|
||||
}
|
||||
|
||||
public static String convertToPem(X509Certificate cert) throws CertificateEncodingException {
|
||||
public static String convertToPem(String cert) throws CertificateEncodingException {
|
||||
|
||||
String cert_begin = "-----BEGIN CERTIFICATE-----\n";
|
||||
String end_cert = "\n-----END CERTIFICATE-----";
|
||||
|
||||
byte[] derCert = cert.getEncoded();
|
||||
// byte[] derCert = cert.getEncoded();
|
||||
|
||||
byte[] derCert = cert.getBytes();
|
||||
String pemCertPre = new String(Base64.encode(derCert, true));
|
||||
String pemCert = cert_begin + pemCertPre + end_cert;
|
||||
return pemCert;
|
||||
@@ -98,6 +100,25 @@ public class CertUtil {
|
||||
return signAlg.toUpperCase().indexOf("SM2") >= 0;
|
||||
}
|
||||
|
||||
|
||||
public static String getDN(String cert) {
|
||||
String pemCert;
|
||||
if(cert.contains("-----BEGIN CERTIFICATE-----")){
|
||||
pemCert =cert;
|
||||
}else {
|
||||
String cert_begin = "-----BEGIN CERTIFICATE-----\n";
|
||||
String end_cert = "\n-----END CERTIFICATE-----";
|
||||
pemCert = cert_begin + cert + end_cert;
|
||||
}
|
||||
X509Certificate certificate = null;
|
||||
try {
|
||||
certificate = convertToX509Cert(pemCert);
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException( 0xC101, e );
|
||||
}
|
||||
return certificate.getSubjectDN().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断 p7 是否是国密证书签的
|
||||
*
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
package com.sunyard.util;
|
||||
|
||||
import com.google.zxing.*;
|
||||
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
|
||||
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.common.HybridBinarizer;
|
||||
import com.google.zxing.qrcode.QRCodeReader;
|
||||
import com.sunyard.SydApiException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Hashtable;
|
||||
|
||||
public class QRCodeUtil {
|
||||
|
||||
public static void genQRCode(String data,String filepath){
|
||||
// 图片的尺寸(宽度和高度)
|
||||
int width = 300;
|
||||
int height = 300;
|
||||
|
||||
// 设置二维码的相关参数
|
||||
Hashtable<EncodeHintType, Object> hintMap = new Hashtable<>();
|
||||
hintMap.put(EncodeHintType.MARGIN, 1); // 设置二维码的空白边距
|
||||
|
||||
try {
|
||||
// 创建二维码矩阵
|
||||
BitMatrix bitMatrix = new MultiFormatWriter().encode(
|
||||
data,
|
||||
BarcodeFormat.QR_CODE,
|
||||
width,
|
||||
height,
|
||||
hintMap
|
||||
);
|
||||
|
||||
// 将二维码矩阵转换为图片,并保存
|
||||
File outputFile = new File(filepath);
|
||||
MatrixToImageWriter.writeToFile(bitMatrix, "PNG", outputFile);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error occurred while generating QR code: " + e.getMessage());
|
||||
throw new SydApiException(1001,e);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void genBarCode(String data,String filepath){
|
||||
// 图片的尺寸(宽度和高度)
|
||||
int width = 300;
|
||||
int height = 300;
|
||||
|
||||
// 设置二维码的相关参数
|
||||
Hashtable<EncodeHintType, Object> hintMap = new Hashtable<>();
|
||||
hintMap.put(EncodeHintType.MARGIN, 1); // 设置二维码的空白边距
|
||||
|
||||
try {
|
||||
// 生成 Code 128 条形码
|
||||
BitMatrix bitMatrix= new MultiFormatWriter().encode(
|
||||
data,
|
||||
BarcodeFormat.PDF_417,
|
||||
width,
|
||||
height,
|
||||
hintMap
|
||||
);
|
||||
|
||||
// 将二维码矩阵转换为图片,并保存
|
||||
File outputFile = new File(filepath);
|
||||
MatrixToImageWriter.writeToFile(bitMatrix, "PNG", outputFile);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error occurred while generating QR code: " + e.getMessage());
|
||||
throw new SydApiException(1001,e);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static String readQRCode(String filepath){
|
||||
// 加载二维码图像
|
||||
File qrCodeImage = new File(filepath);
|
||||
|
||||
// 读取二维码图像
|
||||
BufferedImage bufferedImage = null;
|
||||
try {
|
||||
bufferedImage = ImageIO.read(qrCodeImage);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// 使用 ZXing 的 QRCodeReader 进行解码
|
||||
QRCodeReader reader = new QRCodeReader();
|
||||
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
|
||||
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
|
||||
|
||||
try {
|
||||
// 解码二维码
|
||||
Result result = reader.decode(bitmap);
|
||||
|
||||
// 提取二维码中的数据
|
||||
return result.getText();
|
||||
|
||||
} catch (NotFoundException e) {
|
||||
throw new SydApiException(1003,e);
|
||||
} catch (ChecksumException e) {
|
||||
throw new SydApiException(1004,e);
|
||||
} catch (FormatException e) {
|
||||
throw new SydApiException(1005,e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String readBarCode(String filepath){
|
||||
// 加载二维码图像
|
||||
File BarCodeImage = new File(filepath);
|
||||
|
||||
try {
|
||||
// 读取条形码图像
|
||||
BufferedImage bufferedImage = ImageIO.read(BarCodeImage);
|
||||
|
||||
// 使用 ZXing 的 MultiFormatReader 进行解码
|
||||
MultiFormatReader reader = new MultiFormatReader();
|
||||
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
|
||||
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
|
||||
|
||||
// 解码条形码
|
||||
Result result = reader.decode(bitmap);
|
||||
|
||||
// 提取条形码中的数据
|
||||
return result.getText();
|
||||
} catch (NotFoundException e) {
|
||||
throw new SydApiException(1006,e);
|
||||
} catch (IOException e) {
|
||||
throw new SydApiException(1007,e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user