Skip to content

Commit

Permalink
Merge pull request #23 from palantir/feature/kwa-serialization
Browse files Browse the repository at this point in the history
make KeyWithAlgorithm json-serializable
  • Loading branch information
markelliot committed Feb 24, 2016
2 parents 5e57983 + bff50da commit 1f804d0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static com.google.common.base.Preconditions.checkArgument;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.io.BaseEncoding;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -34,6 +36,7 @@ public abstract class KeyWithAlgorithm {
public abstract byte[] getKey();

@Override
@JsonValue
public final String toString() {
String encodedKeyString = BaseEncoding.base64().encode(getKey());
return getAlgorithm() + ":" + encodedKeyString;
Expand All @@ -51,6 +54,7 @@ public static KeyWithAlgorithm from(String algorithm, byte[] key) {
.build();
}

@JsonCreator
public static KeyWithAlgorithm fromString(String keyWithAlgorithm) {
checkArgument(keyWithAlgorithm.contains(":"), "Key must be in the format <algorithm>:<key in base64>");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2015 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.config.crypto;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import org.junit.Test;

public final class KeyWithAlgorithmTest {
private static final ObjectMapper mapper = new ObjectMapper();
private static final String kwaString = "AES:rqrvWpLld+wKLOyxJYxQVg==";
private static final KeyWithAlgorithm kwa = KeyWithAlgorithm.fromString(kwaString);

@Test
public void testSerialization()
throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
String serialized = mapper.writeValueAsString(kwa);

String expectedSerialization = String.format("\"%s\"", kwaString);
assertThat(serialized, is(expectedSerialization));

KeyWithAlgorithm deserialized = mapper.readValue(serialized, KeyWithAlgorithm.class);
assertThat(deserialized, is(kwa));
}
}

0 comments on commit 1f804d0

Please sign in to comment.