add signature check

This commit is contained in:
Jörg Prante 2024-05-27 16:48:45 +02:00
parent caa64baa49
commit 25fb5a0bb9
2 changed files with 27 additions and 0 deletions

View file

@ -22,6 +22,7 @@ test {
environment 'NOTIFY_SOCKET', '/run/systemd/notify'
testLogging {
events 'STARTED', 'PASSED', 'FAILED', 'SKIPPED'
showStandardStreams = true
}
afterSuite { desc, result ->
if (!desc.parent) {

View file

@ -9,9 +9,13 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.security.Signature;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class PrivateKeyReaderTest {
@ -27,6 +31,7 @@ public class PrivateKeyReaderTest {
PublicKey publicKey = keyPair.getPublic();
assertNotNull(publicKey);
assertEquals("DSA", publicKey.getAlgorithm());
match("SHA1withDSA", privateKey, publicKey);
}
}
@ -42,6 +47,7 @@ public class PrivateKeyReaderTest {
PublicKey publicKey = keyPair.getPublic();
assertNotNull(publicKey);
assertEquals("RSA", publicKey.getAlgorithm());
match("SHA256withRSA", privateKey, publicKey);
}
}
@ -57,6 +63,7 @@ public class PrivateKeyReaderTest {
PublicKey publicKey = keyPair.getPublic();
assertNotNull(publicKey);
assertEquals("EC", publicKey.getAlgorithm());
match("SHA256withECDSA", privateKey, publicKey);
}
}
@ -72,6 +79,7 @@ public class PrivateKeyReaderTest {
PublicKey publicKey = keyPair.getPublic();
assertNotNull(publicKey);
assertEquals("EdDSA", publicKey.getAlgorithm());
match("Ed25519", privateKey, publicKey);
}
}
@ -120,4 +128,22 @@ public class PrivateKeyReaderTest {
assertEquals("PKCS#8", privateKey.getFormat());
}
}
private static void match(String algo,
PrivateKey privateKey,
PublicKey publicKey) throws Exception {
String message = "Hello World";
Signature signature = Signature.getInstance(algo);
signature.initSign(privateKey);
signature.update(message.getBytes(StandardCharsets.UTF_8));
byte[] payload = signature.sign();
signature.initVerify(publicKey);
signature.update(message.getBytes(StandardCharsets.UTF_8));
assertTrue(signature.verify(payload));
logger.log(Level.INFO, "verified " + algo);
}
private static final Logger logger = Logger.getLogger(PrivateKeyReaderTest.class.getName());
}