-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7551623
commit 3e196a3
Showing
8 changed files
with
154 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/bin/bash | ||
curl -k "https://localhost:8080/api/filter" \ | ||
curl "http://localhost:8080/api/filter" \ | ||
--data "George Washington was president and his ssn was 123-45-6789 and he lived in 90210." -H "Content-type: text/plain" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
philter-api/src/main/java/ai/philterd/philter/api/WebMvcConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ai.philterd.philter.api; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class WebMvcConfiguration implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { | ||
configurer.defaultContentType(MediaType.TEXT_PLAIN); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
philter-api/src/main/java/ai/philterd/philter/api/model/SignedFilterResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ai.philterd.philter.api.model; | ||
|
||
import ai.philterd.phileas.model.responses.FilterResponse; | ||
|
||
public class SignedFilterResponse extends FilterResponse { | ||
|
||
private final String signature; | ||
|
||
public SignedFilterResponse(final FilterResponse filterResponse, String signature) { | ||
super(filterResponse.getFilteredText(), filterResponse.getContext(), filterResponse.getDocumentId(), filterResponse.getPiece(), filterResponse.getExplanation(), filterResponse.getAttributes()); | ||
this.signature = signature; | ||
} | ||
|
||
public String getSignature() { | ||
return signature; | ||
} | ||
|
||
} |
94 changes: 94 additions & 0 deletions
94
philter-api/src/test/java/ai/philterd/test/philter/api/SignTextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package ai.philterd.test.philter.api; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.FileInputStream; | ||
import java.security.KeyStore; | ||
import java.security.PrivateKey; | ||
import java.security.PublicKey; | ||
import java.security.Signature; | ||
import java.security.cert.Certificate; | ||
import java.util.Base64; | ||
|
||
public class SignTextTest { | ||
|
||
@Test | ||
public void test() throws Exception { | ||
|
||
// Have to create the private/public keys. | ||
// Have to sign the text and send back the signature | ||
|
||
// Load private key for signing: | ||
// keytool -genkeypair -alias senderKeyPair -keyalg RSA -keysize 2048 -dname "CN=Baeldung" -validity 365 -storetype JKS -keystore sender_keystore.jks -storepass changeit | ||
|
||
// Export public key: | ||
// keytool -exportcert -alias senderKeyPair -storetype JKS -keystore sender_keystore.jks -file sender_certificate.cer -rfc -storepass changeit | ||
|
||
final KeyStore keyStore = KeyStore.getInstance("JKS"); | ||
keyStore.load(new FileInputStream("/tmp/sender_keystore.jks"), "changeit".toCharArray()); | ||
|
||
final PrivateKey privateKey = (PrivateKey) keyStore.getKey("senderKeyPair", "changeit".toCharArray()); | ||
|
||
// Create empty key store on the receiver side. | ||
// keytool -genkeypair -alias receiverKeyPair -keyalg RSA -keysize 2048 -dname "CN=Baeldung" -validity 365 -storetype JKS -keystore receiver_keystore.jks -storepass changeit | ||
// keytool -delete -alias receiverKeyPair -storepass changeit -keystore receiver_keystore.jks | ||
|
||
// Import the public key into the empty key store. | ||
// keytool -importcert -alias receiverKeyPair -storetype JKS -keystore receiver_keystore.jks -file sender_certificate.cer -rfc -storepass changeit | ||
|
||
// Load the public key | ||
|
||
final KeyStore keyStore2 = KeyStore.getInstance("JKS"); | ||
keyStore2.load(new FileInputStream("/tmp/receiver_keystore.jks"), "changeit".toCharArray()); | ||
final Certificate certificate = keyStore2.getCertificate("receiverKeyPair"); | ||
final PublicKey publicKey = certificate.getPublicKey(); | ||
|
||
//final String encodedPublicKey = Base64.getEncoder().encodeToString(publicKey.getEncoded()); | ||
//System.out.println(encodedPublicKey); | ||
|
||
// --------------------------- | ||
|
||
final Signature signature = Signature.getInstance("SHA256withRSA"); | ||
signature.initSign(privateKey); | ||
|
||
final byte[] messageBytes = "hello world".getBytes(); | ||
|
||
signature.update(messageBytes); | ||
final byte[] digitalSignature = signature.sign(); | ||
|
||
// Convert the signature to a string to be returned. | ||
final String encodedDigitalSignature = Base64.getEncoder().encodeToString(digitalSignature); | ||
//System.out.println(encodedDigitalSignature); | ||
|
||
boolean valid = verify(encodedDigitalSignature, "hello world"); | ||
|
||
Assert.assertTrue(valid); | ||
|
||
} | ||
|
||
public boolean verify(final String digitalSignature, final String redactedText) throws Exception { | ||
|
||
// Load the public key | ||
|
||
final KeyStore keyStore2 = KeyStore.getInstance("JKS"); | ||
keyStore2.load(new FileInputStream("/tmp/receiver_keystore.jks"), "changeit".toCharArray()); | ||
|
||
final Certificate certificate = keyStore2.getCertificate("receiverKeyPair"); | ||
final PublicKey publicKey = certificate.getPublicKey(); | ||
|
||
final String encodedPublicKey = Base64.getEncoder().encodeToString(publicKey.getEncoded()); | ||
//System.out.println(encodedPublicKey); | ||
|
||
final Signature signature = Signature.getInstance("SHA256withRSA"); | ||
signature.initVerify(publicKey); | ||
|
||
byte[] messageBytes = "hello world".getBytes(); | ||
|
||
signature.update(messageBytes); | ||
|
||
return signature.verify(Base64.getDecoder().decode(digitalSignature)); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters