/** * Static method to create a JsonKeysetReader from a {@link Path}. * * <p>Note: the file path won't be read until {@link JsonKeysetReader#read} or {@link * JsonKeysetReader#readEncrypted} is called. * * <p>This method only works on Android API level 26 or newer. */ public static JsonKeysetReader withPath(Path path) throws IOException { return withFile(path.toFile()); }
/** * Static method to create a JsonKeysetReader from an {@link InputStream}. * * <p>Note: the input stream won't be read until {@link JsonKeysetReader#read} or {@link * JsonKeysetReader#readEncrypted} is called. */ public static KeysetReader withInputStream(InputStream input) throws IOException { return new JsonKeysetReader(input); }
private Key keyFromJson(JSONObject json) throws JSONException { validateKey(json); return Key.newBuilder() .setStatus(getStatus(json.getString("status"))) .setKeyId(json.getInt("keyId")) .setOutputPrefixType(getOutputPrefixType(json.getString("outputPrefixType"))) .setKeyData(keyDataFromJson(json.getJSONObject("keyData"))) .build(); }
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(); }
private KeyInfo keyInfoFromJson(JSONObject json) throws JSONException { return KeyInfo.newBuilder() .setStatus(getStatus(json.getString("status"))) .setKeyId(json.getInt("keyId")) .setOutputPrefixType(getOutputPrefixType(json.getString("outputPrefixType"))) .setTypeUrl(json.getString("typeUrl")) .build(); }
private KeyData keyDataFromJson(JSONObject json) throws JSONException { validateKeyData(json); byte[] value; if (urlSafeBase64) { value = Base64.urlSafeDecode(json.getString("value")); } else { value = Base64.decode(json.getString("value")); } return KeyData.newBuilder() .setTypeUrl(json.getString("typeUrl")) .setValue(ByteString.copyFrom(value)) .setKeyMaterialType(getKeyMaterialType(json.getString("keyMaterialType"))) .build(); }
@Override public Keyset read() throws IOException { try { if (json != null) { return keysetFromJson(json); } else { return keysetFromJson(new JSONObject( new String(Util.readAll(inputStream), UTF_8))); } } catch (JSONException e) { throw new IOException(e); } }
@Override public EncryptedKeyset readEncrypted() throws IOException { try { if (json != null) { return encryptedKeysetFromJson(json); } else { return encryptedKeysetFromJson(new JSONObject( new String(Util.readAll(inputStream), UTF_8))); } } catch (JSONException e) { throw new IOException(e); } }
private KeysetInfo keysetInfoFromJson(JSONObject json) throws JSONException { KeysetInfo.Builder builder = KeysetInfo.newBuilder(); if (json.has("primaryKeyId")) { builder.setPrimaryKeyId(json.getInt("primaryKeyId")); } if (json.has("keyInfo")) { JSONArray keyInfos = json.getJSONArray("keyInfo"); for (int i = 0; i < keyInfos.length(); i++) { builder.addKeyInfo(keyInfoFromJson(keyInfos.getJSONObject(i))); } } return builder.build(); }
/** * Static method to create a JsonKeysetReader from a {@link Path}. * * <p>Note: the file path won't be read until {@link JsonKeysetReader#read} or {@link * JsonKeysetReader#readEncrypted} is called. * * <p>This method only works on Android API level 26 or newer. */ public static JsonKeysetReader withPath(String path) throws IOException { return withFile(new File(path)); }
/** Static method to create a JsonKeysetReader from an {@link JSONObject}. */ public static JsonKeysetReader withJsonObject(JSONObject input) { return new JsonKeysetReader(input); }
private KeysetHandle loadPrivateKey() throws IOException, GeneralSecurityException { return CleartextKeysetHandle.read(JsonKeysetReader.withFile(new File(PRIVATE_KEYSET_FILENAME))); }
/** * Static method to create a JsonKeysetReader from a file. * * <p>Note: the file won't be read until {@link JsonKeysetReader#read} or {@link * JsonKeysetReader#readEncrypted} is called. */ public static JsonKeysetReader withFile(File file) throws IOException { return new JsonKeysetReader(new FileInputStream(file)); }
private KeysetHandle loadPublicKey() throws IOException, GeneralSecurityException { return CleartextKeysetHandle.read(JsonKeysetReader.withFile(new File(PUBLIC_KEYSET_FILENAME))); }
/** Static method to create a JsonKeysetReader from a byte array. */ public static JsonKeysetReader withBytes(final byte[] bytes) { return new JsonKeysetReader(new ByteArrayInputStream(bytes)); }
private KeysetHandle loadKey() throws IOException, GeneralSecurityException { return CleartextKeysetHandle.read(JsonKeysetReader.withFile(new File(KEYSET_FILENAME))); }
/** Static method to create a JsonKeysetReader from a string. */ public static JsonKeysetReader withString(String input) { return new JsonKeysetReader(new ByteArrayInputStream(input.getBytes(UTF_8))); }