/** * Static method to create a JsonKeysetWriter that writes to a file path. * * <p>This method only works on Android API level 26 or newer. */ public static KeysetWriter withPath(Path path) throws IOException { return withFile(path.toFile()); }
/** Static method to create a JsonKeysetWriter that writes to an {@link OutputStream}. */ public static KeysetWriter withOutputStream(OutputStream stream) { return new JsonKeysetWriter(stream); }
@Override public void write(Keyset keyset) throws IOException { try { outputStream.write(toJson(keyset).toString(4).getBytes(UTF_8)); } catch (JSONException e) { throw new IOException(e); } }
public static void printKeyset(String type, KeysetHandle keysetHandle) { try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); CleartextKeysetHandle.write(keysetHandle, JsonKeysetWriter.withOutputStream(outputStream)); log.info("{}: {}", type, new String(outputStream.toByteArray())); } catch (IOException ex) { log.error("Failed to write keyset", ex); } }
/** Static method to create a JsonKeysetWriter that writes to a file path. */ public static KeysetWriter withPath(String path) throws IOException { return withFile(new File(path)); }
@Override public void write(EncryptedKeyset keyset) throws IOException { try { outputStream.write(toJson(keyset).toString(4).getBytes(UTF_8)); } catch (JSONException e) { throw new IOException(e); } }
/** Static method to create a JsonKeysetWriter that writes to a file. */ public static KeysetWriter withFile(File file) throws IOException { return new JsonKeysetWriter(new FileOutputStream(file)); }
/** * Stores the public keyset in the projects resources/keysets directory if it does not exist yet. * * @throws IOException Failure during saving * @throws GeneralSecurityException Failure during keyset generation */ private void generateAndStorePublicKey(KeysetHandle privateKeysetHandle) throws IOException, GeneralSecurityException { KeysetHandle keysetHandle = privateKeysetHandle.getPublicKeysetHandle(); File keysetFile = new File(PUBLIC_KEYSET_FILENAME); if (!keysetFile.exists()) { CleartextKeysetHandle.write(keysetHandle, JsonKeysetWriter.withFile(keysetFile)); } }
private JSONObject toJson(Keyset keyset) throws JSONException { JSONObject json = new JSONObject(); json.put("primaryKeyId", keyset.getPrimaryKeyId()); JSONArray keys = new JSONArray(); for (Key key : keyset.getKeyList()) { keys.put(toJson(key)); } json.put("key", keys); return json; }
/** * Stores the private keyset in the projects resources/keysets directory if it does not exist yet. * * @throws IOException Failure during saving * @throws GeneralSecurityException Failure during keyset generation */ private void generateAndStorePrivateKey() throws IOException, GeneralSecurityException { KeysetHandle keysetHandle = KeysetHandle.generateNew(HybridKeyTemplates.ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM); File keysetFile = new File(PRIVATE_KEYSET_FILENAME); if (!keysetFile.exists()) { CleartextKeysetHandle.write(keysetHandle, JsonKeysetWriter.withFile(keysetFile)); } }
private JSONObject toJson(KeysetInfo keysetInfo) throws JSONException { JSONObject json = new JSONObject(); json.put("primaryKeyId", keysetInfo.getPrimaryKeyId()); JSONArray keyInfos = new JSONArray(); for (KeyInfo keyInfo : keysetInfo.getKeyInfoList()) { keyInfos.put(toJson(keyInfo)); } json.put("keyInfo", keyInfos); return json; }
/** * Stores the keyset in the projects resources/keysets directory if it does not exist yet. * * @throws IOException Failure during saving * @throws GeneralSecurityException Failure during keyset generation */ private void generateAndStoreKey() throws IOException, GeneralSecurityException { KeysetHandle keysetHandle = KeysetHandle.generateNew(AeadKeyTemplates.AES128_GCM); File keysetFile = new File(KEYSET_FILENAME); if (!keysetFile.exists()) { CleartextKeysetHandle.write(keysetHandle, JsonKeysetWriter.withFile(keysetFile)); } }
private JSONObject toJson(EncryptedKeyset keyset) throws JSONException { return new JSONObject() .put("encryptedKeyset", Base64.encode(keyset.getEncryptedKeyset().toByteArray())) .put("keysetInfo", toJson(keyset.getKeysetInfo())); }
private JSONObject toJson(Key key) throws JSONException { return new JSONObject() .put("keyData", toJson(key.getKeyData())) .put("status", key.getStatus().toString()) .put("keyId", key.getKeyId()) .put("outputPrefixType", key.getOutputPrefixType().toString()); }