diff --git a/scfs-api-sm/src/test/java/com/czcb/scfs/api/sm/SmProfileTest.java b/scfs-api-sm/src/test/java/com/czcb/scfs/api/sm/SmProfileTest.java index e077880..9b82c04 100644 --- a/scfs-api-sm/src/test/java/com/czcb/scfs/api/sm/SmProfileTest.java +++ b/scfs-api-sm/src/test/java/com/czcb/scfs/api/sm/SmProfileTest.java @@ -1,11 +1,16 @@ package com.czcb.scfs.api.sm; import com.czcb.scfs.api.core.DefaultChannel; +import com.czcb.scfs.api.core.http.DefaultHttpProfile; import com.czcb.scfs.api.core.util.PemFile; import com.tencent.kona.KonaProvider; +import org.assertj.core.util.Lists; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.nio.charset.StandardCharsets; import static com.czcb.scfs.api.sm.SMKeyText.CERTIFICATE_TEXT_SM2; @@ -46,4 +51,106 @@ class SmProfileTest { Assertions.assertNotNull(profile.getPrivacy().getDecryptor().getPrivateKey()); Assertions.assertNotNull(profile.getPrivacy().getEncryptor().getPublicKey()); } + + @Test + void test2() throws IOException { + File privateFile = File.createTempFile("private_text_sm2.", ".pem"); + write(privateFile, PRIVATE_TEXT_SM2); + + File certificateFile = File.createTempFile("certificate_text_sm2.", ".pem"); + write(certificateFile, CERTIFICATE_TEXT_SM2); + + SmProfile profile = new SmProfile.Builder() + .channel(new DefaultChannel.Builder() + .channelNo("0000") + .appNo("100001") + .build()) + .privateKey(privateFile.getAbsolutePath()) + .addCertificate(certificateFile.getAbsolutePath()) + .certificateSerial("B47BF250D13D6DC72D2869D340CDF8B830F2CFD320B652586C3D4861F86CA355") + .build(); + + privateFile.delete(); + certificateFile.delete(); + + Assertions.assertNotNull(profile); + + String message = "1234567"; + String signResult = profile.getSignature().getSigner().sign(message).getSignature(); + Assertions.assertTrue(profile.getSignature().getVerifier().verify("B47BF250D13D6DC72D2869D340CDF8B830F2CFD320B652586C3D4861F86CA355", message, signResult)); + + Assertions.assertEquals("0000", profile.getChannel().getChannelNo()); + Assertions.assertEquals("100001", profile.getChannel().getAppNo()); + + String text = "1234567890"; + String ciphertext = profile.getPrivacy().getEncryptor().encrypt(text); + Assertions.assertEquals(text, profile.getPrivacy().getDecryptor().decrypt(ciphertext)); + + String secretKey = "abcdef1234567890"; + String encrypt = profile.getPrivacy().getSecretCipher().encrypt(secretKey.getBytes(StandardCharsets.UTF_8), text.getBytes(StandardCharsets.UTF_8)); + String decrypt = profile.getPrivacy().getSecretCipher().decrypt(secretKey.getBytes(StandardCharsets.UTF_8), encrypt.getBytes(StandardCharsets.UTF_8)); + Assertions.assertEquals(text, decrypt); + + Assertions.assertNotNull(profile.getPrivacy().getDecryptor().getPrivateKey()); + Assertions.assertNotNull(profile.getPrivacy().getEncryptor().getPublicKey()); + } + + public void write(File file, String text) throws IOException { + FileOutputStream outputStream = new FileOutputStream(file); + outputStream.write(text.getBytes(StandardCharsets.UTF_8)); + outputStream.flush(); + outputStream.close(); + } + + @Test + void test3() { + SmProfile profile = new SmProfile.Builder() + .channel(new DefaultChannel.Builder() + .channelNo("0000") + .appNo("100001") + .build()) + .privateKey(PemFile.loadPrivateKeyFromString(PRIVATE_TEXT_SM2, "EC", KonaProvider.NAME)) + .addCertificates(Lists.list(PemFile.loadX509FromString(CERTIFICATE_TEXT_SM2, KonaProvider.NAME))) + .certificateSerial("B47BF250D13D6DC72D2869D340CDF8B830F2CFD320B652586C3D4861F86CA355") + .build(); + + Assertions.assertNotNull(profile); + + String message = "1234567"; + String signResult = profile.getSignature().getSigner().sign(message).getSignature(); + Assertions.assertTrue(profile.getSignature().getVerifier().verify("B47BF250D13D6DC72D2869D340CDF8B830F2CFD320B652586C3D4861F86CA355", message, signResult)); + + Assertions.assertEquals("0000", profile.getChannel().getChannelNo()); + Assertions.assertEquals("100001", profile.getChannel().getAppNo()); + + String text = "1234567890"; + String ciphertext = profile.getPrivacy().getEncryptor().encrypt(text); + Assertions.assertEquals(text, profile.getPrivacy().getDecryptor().decrypt(ciphertext)); + + String secretKey = "abcdef1234567890"; + String encrypt = profile.getPrivacy().getSecretCipher().encrypt(secretKey.getBytes(StandardCharsets.UTF_8), text.getBytes(StandardCharsets.UTF_8)); + String decrypt = profile.getPrivacy().getSecretCipher().decrypt(secretKey.getBytes(StandardCharsets.UTF_8), encrypt.getBytes(StandardCharsets.UTF_8)); + Assertions.assertEquals(text, decrypt); + + Assertions.assertNotNull(profile.getPrivacy().getDecryptor().getPrivateKey()); + Assertions.assertNotNull(profile.getPrivacy().getEncryptor().getPublicKey()); + } + + @Test + void test4() { + SmProfile profile = new SmProfile.Builder() + .channel(new DefaultChannel.Builder() + .channelNo("0000") + .appNo("100001") + .build()) + .httpProfile(new DefaultHttpProfile.Builder() + .host("test") + .build()) + .privateKey(PemFile.loadPrivateKeyFromString(PRIVATE_TEXT_SM2, "EC", KonaProvider.NAME)) + .addCertificates(Lists.list(PemFile.loadX509FromString(CERTIFICATE_TEXT_SM2, KonaProvider.NAME))) + .certificateSerial("B47BF250D13D6DC72D2869D340CDF8B830F2CFD320B652586C3D4861F86CA355") + .build(); + + Assertions.assertEquals("test", profile.getHttpProfile().getHost()); + } } \ No newline at end of file