use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class JsonKeysetReader method keysetFromJson.
private Keyset keysetFromJson(JSONObject json) throws JSONException {
validateKeyset(json);
Keyset.Builder builder = Keyset.newBuilder();
if (json.has("primaryKeyId")) {
builder.setPrimaryKeyId(json.getInt("primaryKeyId"));
}
JSONArray keys = json.getJSONArray("key");
for (int i = 0; i < keys.length(); i++) {
builder.addKey(keyFromJson(keys.getJSONObject(i)));
}
return builder.build();
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class JsonKeysetReader method keysetFromJson.
private Keyset keysetFromJson(JsonObject json) {
validateKeyset(json);
Keyset.Builder builder = Keyset.newBuilder();
if (json.has("primaryKeyId")) {
builder.setPrimaryKeyId(json.get("primaryKeyId").getAsInt());
}
JsonArray keys = json.getAsJsonArray("key");
for (int i = 0; i < keys.size(); i++) {
builder.addKey(keyFromJson(keys.get(i).getAsJsonObject()));
}
return builder.build();
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetHandle method getKeys.
/**
* Returns the keyset data as a list of {@link KeyHandle}s.
*/
public List<KeyHandle> getKeys() {
ArrayList<KeyHandle> result = new ArrayList<>();
for (Keyset.Key key : keyset.getKeyList()) {
KeyData keyData = key.getKeyData();
result.add(new InternalKeyHandle(new ProtoKey(keyData, KeyTemplate.fromProto(key.getOutputPrefixType())), key.getStatus(), key.getKeyId()));
}
return Collections.unmodifiableList(result);
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetHandle method decrypt.
/**
* Decrypts the encrypted keyset with the {@link Aead} master key.
*/
private static Keyset decrypt(EncryptedKeyset encryptedKeyset, Aead masterKey, byte[] associatedData) throws GeneralSecurityException {
try {
Keyset keyset = Keyset.parseFrom(masterKey.decrypt(encryptedKeyset.getEncryptedKeyset().toByteArray(), associatedData), ExtensionRegistryLite.getEmptyRegistry());
// check emptiness here too, in case the encrypted keys unwrapped to nothing?
assertEnoughKeyMaterial(keyset);
return keyset;
} catch (@SuppressWarnings("UnusedException") InvalidProtocolBufferException e) {
// Do not propagate InvalidProtocolBufferException to guarantee no key material is leaked
throw new GeneralSecurityException("invalid keyset, corrupted key material");
}
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class JsonKeysetWriter method toJson.
private JsonObject toJson(Keyset keyset) {
JsonObject json = new JsonObject();
json.addProperty("primaryKeyId", toUnsignedLong(keyset.getPrimaryKeyId()));
JsonArray keys = new JsonArray();
for (Keyset.Key key : keyset.getKeyList()) {
keys.add(toJson(key));
}
json.add("key", keys);
return json;
}
Aggregations