SM2 版本工具
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package com.sunyard.cisd;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyPair;
|
||||
|
||||
public class SM2UtilTest {
|
||||
|
||||
@Test
|
||||
public void testGenerateAndSaveKeyPair() throws Exception {
|
||||
KeyPair keyPair = SM2Util.generateKeyPair();
|
||||
|
||||
String privateKeyPEM = SM2Util.privateKeyToPEM(keyPair.getPrivate());
|
||||
String publicKeyPEM = SM2Util.publicKeyToPEM(keyPair.getPublic());
|
||||
|
||||
String resourcesPath = "src/main/resources";
|
||||
File resourcesDir = new File(resourcesPath);
|
||||
if (!resourcesDir.exists()) {
|
||||
resourcesDir.mkdirs();
|
||||
}
|
||||
|
||||
File privateKeyFile = new File(resourcesPath + "/sm2_private_key.pem");
|
||||
try (Writer writer = new OutputStreamWriter(new FileOutputStream(privateKeyFile), StandardCharsets.UTF_8)) {
|
||||
writer.write(privateKeyPEM);
|
||||
}
|
||||
System.out.println("Private key saved to: " + privateKeyFile.getAbsolutePath());
|
||||
|
||||
File publicKeyFile = new File(resourcesPath + "/sm2_public_key.pem");
|
||||
try (Writer writer = new OutputStreamWriter(new FileOutputStream(publicKeyFile), StandardCharsets.UTF_8)) {
|
||||
writer.write(publicKeyPEM);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@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();
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user