PaymentMethodTokenSender { public String seal(final String message) throws GeneralSecurityException { if (protocolVersion.equals(PaymentMethodTokenConstants.PROTOCOL_VERSION_EC_V1) || protocolVersion.equals(PaymentMethodTokenConstants.PROTOCOL_VERSION_EC_V2) || protocolVersion.equals( PaymentMethodTokenConstants.PROTOCOL_VERSION_EC_V2_SIGNING_ONLY)) { return sealV1OrV2(message); } throw new GeneralSecurityException("Unsupported version: " + protocolVersion); } PaymentMethodTokenSender(Builder builder); String seal(final String message);  }
@Test public void testECV1WithPrecomputedKeys() throws Exception { PaymentMethodTokenSender sender = new PaymentMethodTokenSender.Builder() .senderSigningKey(GOOGLE_SIGNING_EC_V1_PRIVATE_KEY_PKCS8_BASE64) .recipientId(RECIPIENT_ID) .rawUncompressedRecipientPublicKey(MERCHANT_PUBLIC_KEY_BASE64) .build(); PaymentMethodTokenRecipient recipient = new PaymentMethodTokenRecipient.Builder() .senderVerifyingKeys(GOOGLE_VERIFYING_PUBLIC_KEYS_JSON) .recipientId(RECIPIENT_ID) .addRecipientPrivateKey(MERCHANT_PRIVATE_KEY_PKCS8_BASE64) .build(); String plaintext = "blah"; assertEquals(plaintext, recipient.unseal(sender.seal(plaintext))); }
@Test public void testECV2WithPrecomputedKeys() throws Exception { PaymentMethodTokenSender sender = new PaymentMethodTokenSender.Builder() .protocolVersion(PaymentMethodTokenConstants.PROTOCOL_VERSION_EC_V2) .senderIntermediateSigningKey( GOOGLE_SIGNING_EC_V2_INTERMEDIATE_PRIVATE_KEY_PKCS8_BASE64) .senderIntermediateCert( new SenderIntermediateCertFactory.Builder() .protocolVersion(PaymentMethodTokenConstants.PROTOCOL_VERSION_EC_V2) .addSenderSigningKey(GOOGLE_SIGNING_EC_V2_PRIVATE_KEY_PKCS8_BASE64) .senderIntermediateSigningKey( GOOGLE_SIGNING_EC_V2_INTERMEDIATE_PUBLIC_KEY_X509_BASE64) .expiration(Instant.now().plus(Duration.standardDays(1)).getMillis()) .build() .create()) .recipientId(RECIPIENT_ID) .rawUncompressedRecipientPublicKey(MERCHANT_PUBLIC_KEY_BASE64) .build(); PaymentMethodTokenRecipient recipient = new PaymentMethodTokenRecipient.Builder() .protocolVersion(PaymentMethodTokenConstants.PROTOCOL_VERSION_EC_V2) .senderVerifyingKeys(GOOGLE_VERIFYING_PUBLIC_KEYS_JSON) .recipientId(RECIPIENT_ID) .addRecipientPrivateKey(MERCHANT_PRIVATE_KEY_PKCS8_BASE64) .build(); String plaintext = "blah"; assertEquals(plaintext, recipient.unseal(sender.seal(plaintext))); }
@Test public void testECV2SigningOnlyWithPrecomputedKeys() throws Exception { PaymentMethodTokenSender sender = new PaymentMethodTokenSender.Builder() .protocolVersion(PaymentMethodTokenConstants.PROTOCOL_VERSION_EC_V2_SIGNING_ONLY) .senderIntermediateSigningKey( GOOGLE_SIGNING_EC_V2_SIGNING_ONLY_INTERMEDIATE_PRIVATE_KEY_PKCS8_BASE64) .senderIntermediateCert( new SenderIntermediateCertFactory.Builder() .protocolVersion( PaymentMethodTokenConstants.PROTOCOL_VERSION_EC_V2_SIGNING_ONLY) .addSenderSigningKey(GOOGLE_SIGNING_EC_V2_SIGNING_ONLY_PRIVATE_KEY_PKCS8_BASE64) .senderIntermediateSigningKey( GOOGLE_SIGNING_EC_V2_SIGNING_ONLY_INTERMEDIATE_PUBLIC_KEY_X509_BASE64) .expiration(Instant.now().plus(Duration.standardDays(1)).getMillis()) .build() .create()) .recipientId(RECIPIENT_ID) .build(); PaymentMethodTokenRecipient recipient = new PaymentMethodTokenRecipient.Builder() .protocolVersion(PaymentMethodTokenConstants.PROTOCOL_VERSION_EC_V2_SIGNING_ONLY) .senderVerifyingKeys(GOOGLE_VERIFYING_PUBLIC_KEYS_JSON) .recipientId(RECIPIENT_ID) .build(); String plaintext = "blah"; assertEquals(plaintext, recipient.unseal(sender.seal(plaintext))); }
@Test public void testSendReceive() throws Exception { ECParameterSpec spec = EllipticCurves.getNistP256Params(); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC"); keyGen.initialize(spec); String senderId = "foo"; String recipientId = "bar"; KeyPair senderKey = keyGen.generateKeyPair(); ECPublicKey senderPublicKey = (ECPublicKey) senderKey.getPublic(); ECPrivateKey senderPrivateKey = (ECPrivateKey) senderKey.getPrivate(); KeyPair recipientKey = keyGen.generateKeyPair(); ECPublicKey recipientPublicKey = (ECPublicKey) recipientKey.getPublic(); ECPrivateKey recipientPrivateKey = (ECPrivateKey) recipientKey.getPrivate(); PaymentMethodTokenSender sender = new PaymentMethodTokenSender.Builder() .senderId(senderId) .senderSigningKey(senderPrivateKey) .recipientId(recipientId) .recipientPublicKey(recipientPublicKey) .build(); PaymentMethodTokenRecipient recipient = new PaymentMethodTokenRecipient.Builder() .senderId(senderId) .addSenderVerifyingKey(senderPublicKey) .recipientId(recipientId) .addRecipientPrivateKey(recipientPrivateKey) .build(); String plaintext = "blah"; assertEquals(plaintext, recipient.unseal(sender.seal(plaintext))); }
@Test public void testWithKeysGeneratedByRecipientKeyGen() throws Exception { ECParameterSpec spec = EllipticCurves.getNistP256Params(); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC"); keyGen.initialize(spec); String senderId = "foo"; String recipientId = "bar"; KeyPair senderKey = keyGen.generateKeyPair(); ECPublicKey senderPublicKey = (ECPublicKey) senderKey.getPublic(); ECPrivateKey senderPrivateKey = (ECPrivateKey) senderKey.getPrivate(); String recipientPrivateKey = "MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCAP5/1502pXYdMion22yiWK" + "GoTBJN/wAAfdjBU6puyEMw=="; String recipientPublicKey = "BJ995jnw2Ppn4BMP/ZKtlTOOIBQC+/L3PDcFRjowZuCkRqUZ/kGWE8c+zimZNHOZPzLB" + "NVGJ3V8M/fM4g4o02Mc="; PaymentMethodTokenSender sender = new PaymentMethodTokenSender.Builder() .senderId(senderId) .senderSigningKey(senderPrivateKey) .recipientId(recipientId) .rawUncompressedRecipientPublicKey(recipientPublicKey) .build(); PaymentMethodTokenRecipient recipient = new PaymentMethodTokenRecipient.Builder() .senderId(senderId) .addSenderVerifyingKey(senderPublicKey) .recipientId(recipientId) .addRecipientPrivateKey(recipientPrivateKey) .build(); String plaintext = "blah"; assertEquals(plaintext, recipient.unseal(sender.seal(plaintext))); }