Skip to content

Instantly share code, notes, and snippets.

@nfisher
Created April 20, 2017 20:34
Show Gist options
  • Save nfisher/5a60447d4751b14274568ec4bdb44a98 to your computer and use it in GitHub Desktop.
Save nfisher/5a60447d4751b14274568ec4bdb44a98 to your computer and use it in GitHub Desktop.
Crypto Test
package com.markel.avro.crypto;
import org.apache.avro.file.Codec;
import org.junit.Test;
import javax.crypto.KeyAgreement;
import javax.crypto.NoSuchPaddingException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
public class CryptoCodecFactoryTest {
@Test
public void shouldGenerateSameSecretGivenPeersPublicKey() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, IOException {
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "SunEC");
keyPairGenerator.initialize(256);
final KeyPair keyPair1 = keyPairGenerator.generateKeyPair();
final KeyPair keyPair2 = keyPairGenerator.generateKeyPair();
final KeyAgreement keyAgreement1 = KeyAgreement.getInstance("ECDH", "SunEC");
final KeyAgreement keyAgreement2 = KeyAgreement.getInstance("ECDH", "SunEC");
keyAgreement1.init(keyPair1.getPrivate());
keyAgreement1.doPhase(keyPair2.getPublic(), true);
byte[] sharedSecret1 = keyAgreement1.generateSecret();
keyAgreement2.init(keyPair2.getPrivate());
keyAgreement2.doPhase(keyPair1.getPublic(), true);
byte[] sharedSecret2 = keyAgreement2.generateSecret();
assertThat(sharedSecret1, is(sharedSecret2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment