Created
June 12, 2017 07:39
-
-
Save stunstunstun/5ea5670b5bfd6459cbebdfbfd4674063 to your computer and use it in GitHub Desktop.
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
package com.holaxapps.chomdoi.support; | |
import org.junit.Test; | |
import java.security.KeyPair; | |
import java.security.NoSuchAlgorithmException; | |
import static org.assertj.core.api.Assertions.assertThat; | |
/** | |
* @author minhyeok | |
*/ | |
public class SecurityTests { | |
@Test | |
public void generateKeyPair() throws NoSuchAlgorithmException { | |
KeyPair keyPair = Security.generateKeyPair(); | |
assertThat(keyPair.getPrivate()).isNotNull(); | |
assertThat(keyPair.getPublic()).isNotNull(); | |
} | |
@Test | |
public void encryptAndDecrypt() throws NoSuchAlgorithmException { | |
String plainText = "{}"; | |
KeyPair keyPair = Security.generateKeyPair(); | |
byte[] encodedPublicKey = keyPair.getPublic().getEncoded(); | |
byte[] encodedPrivateKey = keyPair.getPrivate().getEncoded(); | |
String cipherText = Security.encrypt(plainText, encodedPublicKey); | |
assertThat(plainText).isEqualTo(Security.decrypt(cipherText, encodedPrivateKey)); | |
} | |
@Test | |
public void signAndVerify() throws NoSuchAlgorithmException { | |
String plainText = "{}"; | |
KeyPair keyPair = Security.generateKeyPair(); | |
byte[] encodedPrivateKey = keyPair.getPrivate().getEncoded(); | |
byte[] encodedPublicKey = keyPair.getPublic().getEncoded(); | |
String signature = Security.sign(plainText, encodedPrivateKey); | |
System.out.println("signature = " + signature); | |
assertThat(signature).isNotNull(); | |
boolean result = Security.verify(plainText, signature, encodedPublicKey); | |
assertThat(result).isTrue(); | |
} | |
@Test(expected = InvalidSignatureException.class) | |
public void signAndVerifyButGoogleSecurityException() throws NoSuchAlgorithmException { | |
String plainText = "{}"; | |
KeyPair keyPair = Security.generateKeyPair(); | |
KeyPair otherKeyPair = Security.generateKeyPair(); | |
byte[] encodedPrivateKey = keyPair.getPrivate().getEncoded(); | |
byte[] encodedPublicKey = otherKeyPair.getPublic().getEncoded(); | |
String signature = Security.sign(plainText, encodedPrivateKey); | |
assertThat(signature).isNotNull(); | |
Security.verify(plainText, signature, encodedPublicKey); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment