SenderIntermediateCertFactory { public String create() throws GeneralSecurityException { try { String signedKey = new JSONObject() .put(PaymentMethodTokenConstants.JSON_KEY_VALUE_KEY, intermediateSigningKey) .put(PaymentMethodTokenConstants.JSON_KEY_EXPIRATION_KEY, Long.toString(expiration)) .toString(); byte[] toSignBytes = PaymentMethodTokenUtil.toLengthValue( senderId, protocolVersion, signedKey); JSONArray signatures = new JSONArray(); for (PublicKeySign signer : signers) { byte[] signature = signer.sign(toSignBytes); signatures.put(Base64.encode(signature)); } return new JSONObject() .put(PaymentMethodTokenConstants.JSON_SIGNED_KEY_KEY, signedKey) .put(PaymentMethodTokenConstants.JSON_SIGNATURES_KEY, signatures) .toString(); } catch (JSONException e) { throw new RuntimeException("Failed to perform JSON encoding", e); } } private  SenderIntermediateCertFactory(
      String protocolVersion,
      String senderId,
      List<ECPrivateKey> senderSigningKeys,
      String intermediateSigningKey,
      long expiration); String create();  }
@Test public void shouldProduceSenderIntermediateCertJson() throws Exception { String encoded = new SenderIntermediateCertFactory.Builder() .protocolVersion(PaymentMethodTokenConstants.PROTOCOL_VERSION_EC_V2) .senderIntermediateSigningKey(GOOGLE_SIGNING_EC_V2_INTERMEDIATE_PUBLIC_KEY_X509_BASE64) .addSenderSigningKey(GOOGLE_SIGNING_EC_V2_PRIVATE_KEY_PKCS8_BASE64) .expiration(123456) .build() .create(); JSONObject decodedSignedIntermediateSigningKey = new JSONObject(encoded); assertTrue(decodedSignedIntermediateSigningKey.get("signedKey") instanceof String); assertTrue(decodedSignedIntermediateSigningKey.get("signatures") instanceof JSONArray); assertEquals(2, decodedSignedIntermediateSigningKey.length()); JSONObject signedKey = new JSONObject(decodedSignedIntermediateSigningKey.getString("signedKey")); assertTrue(signedKey.get("keyValue") instanceof String); assertTrue(signedKey.get("keyExpiration") instanceof String); assertEquals(2, signedKey.length()); assertEquals( GOOGLE_SIGNING_EC_V2_INTERMEDIATE_PUBLIC_KEY_X509_BASE64, signedKey.getString("keyValue")); assertEquals("123456", signedKey.getString("keyExpiration")); assertEquals(1, decodedSignedIntermediateSigningKey.getJSONArray("signatures").length()); assertNotEquals( 0, decodedSignedIntermediateSigningKey.getJSONArray("signatures").getString(0).length()); }