Skip to content

Commit

Permalink
manual wire serialization (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellisjoe authored Feb 9, 2017
1 parent e6259fd commit b0eece5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,9 @@ public static KeyMaterial generateKeyMaterial(String keyAlgorithm, int keySize,
return KeyMaterial.of(key, iv);
}

public static SerializableKeyMaterial serialize(KeyMaterial keyMaterial) {
SecretKey key = keyMaterial.getSecretKey();
return SerializableKeyMaterial.of(key.getAlgorithm(), key.getEncoded(), keyMaterial.getIv());
}

public static KeyMaterial deserialize(SerializableKeyMaterial keyMaterial) {
String algorithm = keyMaterial.getAlgorithm();
byte[] encodedKey = keyMaterial.getEncodedKey();
byte[] iv = keyMaterial.getIv();
return KeyMaterial.of(new SecretKeySpec(encodedKey, algorithm), iv);
public static KeyMaterial from(String keyAlgorithm, byte[] encodedKey, byte[] iv) {
SecretKeySpec secretKey = new SecretKeySpec(encodedKey, keyAlgorithm);
return KeyMaterial.of(secretKey, iv);
}

public static byte[] wrap(KeyMaterial keyMaterial, PublicKey key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.crypto2;

import com.google.common.base.Throwables;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

/**
* Convenient {@link PublicKey} constructors.
*/
public final class PublicKeys {

private PublicKeys() {}

public static PublicKey from(String algorithm, byte[] encodedKey) {
try {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw Throwables.propagate(e);
}
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ public void testUnwrap_wrongVersion() {

@Test
public void testSerializeDeserialize() {
SerializableKeyMaterial skm = KeyMaterials.serialize(keyMaterial);
assertThat(KeyMaterials.deserialize(skm), is(keyMaterial));
SecretKey secretKey = keyMaterial.getSecretKey();
String algorithm = secretKey.getAlgorithm();
byte[] encoded = secretKey.getEncoded();
byte[] iv = keyMaterial.getIv();
assertThat(KeyMaterials.from(algorithm, encoded, iv), is(keyMaterial));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
* Copyright 2017 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,17 +19,16 @@
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import java.security.KeyPair;
import java.security.PublicKey;
import org.junit.Test;

public final class SerializablePublicKeyTest {
public final class PublicKeysTest {

@Test
public void testDeserialization() {
KeyPair keyPair = TestKeyPairs.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
assertThat(SerializablePublicKey.of(publicKey).deserialize(), is(publicKey));
public void testFrom_generatesEquivalent() {
PublicKey expectedKey = TestKeyPairs.generateKeyPair().getPublic();
PublicKey actualKey = PublicKeys.from(expectedKey.getAlgorithm(), expectedKey.getEncoded());
assertThat(actualKey, is(expectedKey));
}

}

0 comments on commit b0eece5

Please sign in to comment.