Bael 5190: Verify Digital Signatures in Java article (#13439)
* #BAEL-5190: add keystore and certificate files * #BAEL-5190: add keystore address * #BAEL-5190: update hashing * #BAEL-5190: delete main classes * #BAEL-5190: move keystore files to test directory * #BAEL-5190: rename to DigitalSignatureUtils * #BAEL-5190: main source code * #BAEL-5190: main test source * #BAEL-5190: update for testing * #BAEL-5190: update keystore type * #BAEL-5190: remove p12 keystores * #BAEL-5190: add jks keystores --------- Co-authored-by: h_sharifi <h_sharifi@modernisc.com>
This commit is contained in:
committed by
GitHub
parent
6abe6473da
commit
17c26f91c9
+76
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.digitalsignature;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class DigitalSignatureUnitTest {
|
||||
|
||||
String messagePath = "src/test/resources/digitalsignature/message.txt";
|
||||
String senderKeyStore = "src/test/resources/digitalsignature/sender_keystore.jks";
|
||||
String receiverKeyStore = "src/test/resources/digitalsignature/receiver_keystore.jks";
|
||||
String storeType = "JKS";
|
||||
String senderAlias = "senderKeyPair";
|
||||
String receiverAlias = "receiverKeyPair";
|
||||
char[] password = "changeit".toCharArray();
|
||||
String signingAlgorithm = "SHA256withRSA";
|
||||
String hashingAlgorithm = "SHA-256";
|
||||
|
||||
@Test
|
||||
public void givenMessageData_whenSignWithSignatureSigning_thenVerify() throws Exception {
|
||||
PrivateKey privateKey = DigitalSignatureUtils.getPrivateKey(senderKeyStore, password, storeType, senderAlias);
|
||||
byte[] messageBytes = Files.readAllBytes(Paths.get(messagePath));
|
||||
|
||||
byte[] digitalSignature = DigitalSignatureUtils.sign(messageBytes, signingAlgorithm, privateKey);
|
||||
|
||||
PublicKey publicKey = DigitalSignatureUtils.getPublicKey(receiverKeyStore, password, storeType, receiverAlias);
|
||||
boolean isCorrect = DigitalSignatureUtils.verify(messageBytes, signingAlgorithm, publicKey, digitalSignature);
|
||||
|
||||
assertTrue(isCorrect);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMessageData_whenSignWithMessageDigestAndCipher_thenVerify() throws Exception {
|
||||
PrivateKey privateKey = DigitalSignatureUtils.getPrivateKey(senderKeyStore, password, storeType, senderAlias);
|
||||
byte[] messageBytes = Files.readAllBytes(Paths.get(messagePath));
|
||||
|
||||
byte[] encryptedMessageHash = DigitalSignatureUtils.signWithMessageDigestAndCipher(messageBytes, hashingAlgorithm, privateKey);
|
||||
|
||||
PublicKey publicKey = DigitalSignatureUtils.getPublicKey(receiverKeyStore, password, storeType, receiverAlias);
|
||||
boolean isCorrect = DigitalSignatureUtils.verifyWithMessageDigestAndCipher(messageBytes, hashingAlgorithm, publicKey, encryptedMessageHash);
|
||||
|
||||
assertTrue(isCorrect);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMessageData_whenSignWithSignatureSigning_thenVerifyWithMessageDigestAndCipher() throws Exception {
|
||||
PrivateKey privateKey = DigitalSignatureUtils.getPrivateKey(senderKeyStore, password, storeType, senderAlias);
|
||||
byte[] messageBytes = Files.readAllBytes(Paths.get(messagePath));
|
||||
|
||||
byte[] digitalSignature = DigitalSignatureUtils.sign(messageBytes, signingAlgorithm, privateKey);
|
||||
|
||||
PublicKey publicKey = DigitalSignatureUtils.getPublicKey(receiverKeyStore, password, storeType, receiverAlias);
|
||||
boolean isCorrect = DigitalSignatureUtils.verifyWithMessageDigestAndCipher(messageBytes, hashingAlgorithm, publicKey, digitalSignature);
|
||||
|
||||
assertTrue(isCorrect);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMessageData_whenSignWithMessageDigestAndCipher_thenVerifyWithSignature() throws Exception {
|
||||
PrivateKey privateKey = DigitalSignatureUtils.getPrivateKey(senderKeyStore, password, storeType, senderAlias);
|
||||
byte[] messageBytes = Files.readAllBytes(Paths.get(messagePath));
|
||||
|
||||
byte[] encryptedMessageHash = DigitalSignatureUtils.signWithMessageDigestAndCipher(messageBytes, hashingAlgorithm, privateKey);
|
||||
|
||||
PublicKey publicKey = DigitalSignatureUtils.getPublicKey(receiverKeyStore, password, storeType, receiverAlias);
|
||||
boolean isCorrect = DigitalSignatureUtils.verify(messageBytes, signingAlgorithm, publicKey, encryptedMessageHash);
|
||||
|
||||
assertTrue(isCorrect);
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,17 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIICxTCCAa2gAwIBAgIEala2gjANBgkqhkiG9w0BAQsFADATMREwDwYDVQQDEwhC
|
||||
YWVsZHVuZzAeFw0yMzAyMTkwNjA5NTdaFw0yNDAyMTkwNjA5NTdaMBMxETAPBgNV
|
||||
BAMTCEJhZWxkdW5nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAifku
|
||||
JnJM3U/x3jWpjpUZeSVpbdUTirdB2Ta0mwXXaZmGwtrwZvS8pXdmegFUMnYB92RJ
|
||||
98j4iYjivXElwwjFIc4YRa7hQicqMfa1H3BUtDwIpqlXM1jISr5TYAE/t/wpkrVH
|
||||
A1QPNv7Fb07ormWKwktTMWyUoLo0chInv07Ip3m6F3X3O0jZFjE8N+7Fnv9oMdsN
|
||||
sAAq+f/7jJSdzo/vzHebR0XUxB1YP6sTWRH6nlNw2h+0kTMf33CkXyDG1Y1qsBRK
|
||||
MoOia10bi21B7Yd+lJo0ZnT1JNei4eEdPYxWQa43JMY6PnpJI9d5WKvye2NewXvO
|
||||
pLap8WR3dgX6n6bUtwIDAQABoyEwHzAdBgNVHQ4EFgQUQVqwZ6AlNlPeeUOmw89A
|
||||
u86n09gwDQYJKoZIhvcNAQELBQADggEBAGoV1ECn0h0IYEoQ8pxF1zbXnt35XEO4
|
||||
ZPbq8cPuYj92X3c9TWeHJIHGO3ZYMS7eaBRL5HhCTqG3YjAsm6+bea9cBffeZwG3
|
||||
EAl0u7e8CI6Ri6065Og4s0c7Y3ZJIJ4i6c5bVqPep8Oj3IFUUAwxz+95c2LX9cfL
|
||||
hxzH8N2RzWvGoJBrmWNeQUuKVlMBVBX6n/EcWmCS/VYORw0mwJ9vdmPhGU3hGggG
|
||||
S0rAVnQlIdvzWsaNllNWf6ETrrHceCflKsOuettjODZUAqiZ9aEd9WMDGHLtZw94
|
||||
zONYICWg2o3Sx9/F26wHdjHn+gxB2Z45Dvd0rBMuCHqwJELxyvofc1E=
|
||||
-----END CERTIFICATE-----
|
||||
Binary file not shown.
Reference in New Issue
Block a user