Skip to content
Snippets Groups Projects
Commit 314c9a6e authored by Thai Duong's avatar Thai Duong
Browse files

Clean up SubtleUtil and TinkeyUtil.

Change-Id: I80c222652b882731a231b00349f71ff0780f89f6
ORIGINAL_AUTHOR=Thai Duong <thaidn@google.com>
GitOrigin-RevId: e72de97e04a45a75e4afd3c5fe67bf1e2175804e
parent afc766ee
No related branches found
No related tags found
No related merge requests found
......@@ -126,11 +126,11 @@ public final class SubtleUtil {
private static final String TYPE_URL_PREFIX = "type.googleapis.com/";
/**
* @throws IllegalArgumentException if {@code typeUrl} is in invalid format.
* @throws GeneralSecurityException if {@code typeUrl} is in invalid format.
*/
public static void validate(String typeUrl) throws IllegalArgumentException {
public static void validateTypeUrl(String typeUrl) throws GeneralSecurityException {
if (!typeUrl.startsWith(TYPE_URL_PREFIX)) {
throw new IllegalArgumentException(
throw new GeneralSecurityException(
String.format(
"Error: type URL %s is invalid; it must start with %s\n",
typeUrl,
......@@ -145,15 +145,4 @@ public final class SubtleUtil {
System.err.print(String.format("Error: %s\n", error));
System.exit(1);
}
/**
* @return the class name of a proto from its type url. For example, return AesGcmKey
* if the type url is type.googleapis.com/google.crypto.tink.AesGcmKey.
* @throws IllegalArgumentException if {@code typeUrl} is in invalid format.
*/
public static String getProtoClassName(String typeUrl) throws IllegalArgumentException {
validate(typeUrl);
int dot = typeUrl.lastIndexOf(".");
return typeUrl.substring(dot + 1);
}
}
......@@ -50,7 +50,7 @@ class CreateKeyTemplateOptions {
outputStream = System.out;
}
try {
SubtleUtil.validate(typeUrlValue);
SubtleUtil.validateTypeUrl(typeUrlValue);
} catch (Exception e) {
SubtleUtil.die(e.toString());
}
......
......@@ -23,8 +23,6 @@ import com.google.common.reflect.ClassPath.ClassInfo;
import com.google.crypto.tink.CleartextKeysetHandle;
import com.google.crypto.tink.KeysetHandle;
import com.google.crypto.tink.Registry;
import com.google.crypto.tink.proto.GcpKmsAeadKey;
import com.google.crypto.tink.proto.KeyData;
import com.google.crypto.tink.proto.KeyTemplate;
import com.google.crypto.tink.proto.Keyset;
import com.google.crypto.tink.subtle.SubtleUtil;
......@@ -70,7 +68,7 @@ public class TinkeyUtil {
throws IllegalArgumentException {
try {
// To parse {@code keyFormat}, we need to find the corresponding proto class.
String keyFormatName = SubtleUtil.getProtoClassName(typeUrl) + KEY_FORMAT_SUFFIX;
String keyFormatName = getProtoClassName(typeUrl) + KEY_FORMAT_SUFFIX;
keyFormatName = keyFormatName.replace(PRIVATE, "");
Class<?> keyFormatClass = loadClass(keyFormatName);
Builder builder = getBuilder(keyFormatClass);
......@@ -120,29 +118,14 @@ public class TinkeyUtil {
}
/**
* @return a {@code KeyData} from a specified key.
* @return the class name of a proto from its type url. For example, return AesGcmKey
* if the type url is type.googleapis.com/google.crypto.tink.AesGcmKey.
* @throws GeneralSecurityException if {@code typeUrl} is in invalid format.
*/
public static KeyData createKeyData(Message key, String typeUrl, KeyData.KeyMaterialType type)
throws Exception {
return KeyData.newBuilder()
.setValue(key.toByteString())
.setTypeUrl(typeUrl)
.setKeyMaterialType(type)
.build();
}
/**
* @return a {@code KeyData} containing a {@code GcpKmsAeadKey}.
*/
public static KeyData createGcpKmsAeadKeyData(String kmsKeyUri)
throws Exception {
GcpKmsAeadKey keyProto = GcpKmsAeadKey.newBuilder()
.setKmsKeyUri(kmsKeyUri)
.build();
return createKeyData(
keyProto,
"type.googleapis.com/google.crypto.tink.GcpKmsAeadKey",
KeyData.KeyMaterialType.REMOTE);
private static String getProtoClassName(String typeUrl) throws GeneralSecurityException {
SubtleUtil.validateTypeUrl(typeUrl);
int dot = typeUrl.lastIndexOf(".");
return typeUrl.substring(dot + 1);
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment