rootcert 完整性校验

This commit is contained in:
cheney
2026-05-14 17:31:43 +08:00
parent 22ca568e88
commit ee8c42dc8e
3 changed files with 147 additions and 40 deletions
@@ -11,66 +11,93 @@ import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
public class SM2UtilTest {
private static final String KEY_NAME_FINGERPRINT = "fingerprint";
private static final String KEY_NAME_MAC = "mac";
@Test
public void testGenerateAndSaveKeyPair() throws Exception {
KeyPair keyPair = SM2Util.generateKeyPair();
String privateKeyPEM = SM2Util.privateKeyToPEM(keyPair.getPrivate());
String publicKeyPEM = SM2Util.publicKeyToPEM(keyPair.getPublic());
public void testGenerateAndSaveKeyPairs() throws Exception {
KeyPair fingerprintKeyPair = SM2Util.generateKeyPair();
KeyPair macKeyPair = SM2Util.generateKeyPair();
String resourcesPath = "src/main/resources";
File resourcesDir = new File(resourcesPath);
if (!resourcesDir.exists()) {
resourcesDir.mkdirs();
}
File privateKeyFile = new File(resourcesPath + "/mac_sm2_private_key.pem");
try (Writer writer = new OutputStreamWriter(new FileOutputStream(privateKeyFile), StandardCharsets.UTF_8)) {
writer.write(privateKeyPEM);
File fingerprintPrivateKeyFile = new File(resourcesPath + "/sm2_" + KEY_NAME_FINGERPRINT + "_private_key.pem");
try (Writer writer = new OutputStreamWriter(new FileOutputStream(fingerprintPrivateKeyFile), StandardCharsets.UTF_8)) {
writer.write(SM2Util.privateKeyToPEM(fingerprintKeyPair.getPrivate()));
}
System.out.println("Private key saved to: " + privateKeyFile.getAbsolutePath());
File publicKeyFile = new File(resourcesPath + "/mac_sm2_public_key.pem");
try (Writer writer = new OutputStreamWriter(new FileOutputStream(publicKeyFile), StandardCharsets.UTF_8)) {
writer.write(publicKeyPEM);
System.out.println("Fingerprint private key saved to: " + fingerprintPrivateKeyFile.getAbsolutePath());
File fingerprintPublicKeyFile = new File(resourcesPath + "/sm2_" + KEY_NAME_FINGERPRINT + "_public_key.pem");
try (Writer writer = new OutputStreamWriter(new FileOutputStream(fingerprintPublicKeyFile), StandardCharsets.UTF_8)) {
writer.write(SM2Util.publicKeyToPEM(fingerprintKeyPair.getPublic()));
}
System.out.println("Public key saved to: " + publicKeyFile.getAbsolutePath());
System.out.println("\nGenerated Key Pair:");
System.out.println("Private Key:\n" + privateKeyPEM);
System.out.println("Public Key:\n" + publicKeyPEM);
System.out.println("Fingerprint public key saved to: " + fingerprintPublicKeyFile.getAbsolutePath());
File macPrivateKeyFile = new File(resourcesPath + "/sm2_" + KEY_NAME_MAC + "_private_key.pem");
try (Writer writer = new OutputStreamWriter(new FileOutputStream(macPrivateKeyFile), StandardCharsets.UTF_8)) {
writer.write(SM2Util.privateKeyToPEM(macKeyPair.getPrivate()));
}
System.out.println("MAC private key saved to: " + macPrivateKeyFile.getAbsolutePath());
File macPublicKeyFile = new File(resourcesPath + "/sm2_" + KEY_NAME_MAC + "_public_key.pem");
try (Writer writer = new OutputStreamWriter(new FileOutputStream(macPublicKeyFile), StandardCharsets.UTF_8)) {
writer.write(SM2Util.publicKeyToPEM(macKeyPair.getPublic()));
}
System.out.println("MAC public key saved to: " + macPublicKeyFile.getAbsolutePath());
System.out.println("\n=== Generated Fingerprint Key Pair ===");
System.out.println("Private Key:\n" + SM2Util.privateKeyToPEM(fingerprintKeyPair.getPrivate()));
System.out.println("Public Key:\n" + SM2Util.publicKeyToPEM(fingerprintKeyPair.getPublic()));
System.out.println("\n=== Generated MAC Key Pair ===");
System.out.println("Private Key:\n" + SM2Util.privateKeyToPEM(macKeyPair.getPrivate()));
System.out.println("Public Key:\n" + SM2Util.publicKeyToPEM(macKeyPair.getPublic()));
}
@Test
public void testSignAndVerify() throws Exception {
KeyPair keyPair = SM2Util.generateKeyPair();
String testData = "Hello, SM2!";
byte[] data = testData.getBytes(StandardCharsets.UTF_8);
byte[] signature = SM2Util.sign(data, keyPair.getPrivate());
System.out.println("Signature generated, length: " + signature.length + " bytes");
boolean verified = SM2Util.verify(data, signature, keyPair.getPublic());
System.out.println("Signature verified: " + verified);
assert verified : "Signature verification failed";
}
@Test
public void testSignAndVerifyWithKeyFiles() throws Exception {
testGenerateAndSaveKeyPair();
testGenerateAndSaveKeyPairs();
String testData = "Test data for SM2 signature";
byte[] data = testData.getBytes(StandardCharsets.UTF_8);
byte[] signature = SM2Util.sign(data, "/sm2_private_key.pem");
System.out.println("Signature generated from file, length: " + signature.length + " bytes");
boolean verified = SM2Util.verify(data, signature, "/sm2_public_key.pem");
System.out.println("Signature verified from file: " + verified);
assert verified : "Signature verification with key files failed";
byte[] fingerprintSignature = SM2Util.sign(data, "/sm2_fingerprint_private_key.pem");
System.out.println("Fingerprint signature generated, length: " + fingerprintSignature.length + " bytes");
boolean fingerprintVerified = SM2Util.verify(data, fingerprintSignature, "/sm2_fingerprint_public_key.pem");
System.out.println("Fingerprint signature verified: " + fingerprintVerified);
assert fingerprintVerified : "Fingerprint signature verification failed";
byte[] macSignature = SM2Util.sign(data, "/sm2_mac_private_key.pem");
System.out.println("MAC signature generated, length: " + macSignature.length + " bytes");
boolean macVerified = SM2Util.verify(data, macSignature, "/sm2_mac_public_key.pem");
System.out.println("MAC signature verified: " + macVerified);
assert macVerified : "MAC signature verification failed";
boolean crossVerified = SM2Util.verify(data, fingerprintSignature, "/sm2_mac_public_key.pem");
System.out.println("Cross verification (fingerprint signature with MAC key): " + crossVerified);
assert !crossVerified : "Cross verification should fail";
}
}