{"action":"created","comment":{"url":"https://api.github.com/repos/palantir/encrypted-config-value/pulls/comments/47866581","id":47866581,"diff_hunk":"@@ -0,0 +1,118 @@\n+/*\n+ * Copyright 2015 Palantir Technologies, Inc. All rights reserved.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *     http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package com.palantir.config.crypto.algorithm;\n+\n+import static com.google.common.base.Preconditions.checkArgument;\n+\n+import com.palantir.config.crypto.EncryptedValue;\n+import com.palantir.config.crypto.KeyPair;\n+import com.palantir.config.crypto.KeyWithAlgorithm;\n+import java.nio.charset.Charset;\n+import java.nio.charset.StandardCharsets;\n+import java.security.KeyFactory;\n+import java.security.KeyPairGenerator;\n+import java.security.NoSuchAlgorithmException;\n+import java.security.NoSuchProviderException;\n+import java.security.PrivateKey;\n+import java.security.PublicKey;\n+import java.security.spec.InvalidKeySpecException;\n+import java.security.spec.PKCS8EncodedKeySpec;\n+import java.security.spec.X509EncodedKeySpec;\n+import java.util.Base64;\n+import javax.crypto.Cipher;\n+import javax.crypto.NoSuchPaddingException;\n+\n+public final class RsaAlgorithm implements Algorithm {\n+\n+    public static final String ALGORITHM_TYPE = \"RSA\";\n+    private static final Charset charset = StandardCharsets.UTF_8;\n+\n+    private Cipher getUninitializedCipher() throws NoSuchAlgorithmException,\n+        NoSuchPaddingException, NoSuchProviderException {\n+        return Cipher.getInstance(ALGORITHM_TYPE);\n+    }\n+\n+    private int getKeyLength() throws NoSuchAlgorithmException {\n+        return Math.min(Cipher.getMaxAllowedKeyLength(ALGORITHM_TYPE), 512);\n+    }\n+\n+    @Override\n+    public KeyPair generateKey() {\n+        try {\n+            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM_TYPE);\n+            keyPairGenerator.initialize(getKeyLength());\n+\n+            java.security.KeyPair keyPair = keyPairGenerator.generateKeyPair();\n+            KeyWithAlgorithm publicKey = KeyWithAlgorithm.from(ALGORITHM_TYPE, keyPair.getPublic().getEncoded());\n+            KeyWithAlgorithm privateKey = KeyWithAlgorithm.from(ALGORITHM_TYPE, keyPair.getPrivate().getEncoded());\n+\n+            return KeyPair.of(publicKey, privateKey);\n+        } catch (NoSuchAlgorithmException e) {\n+            throw new RuntimeException(\"No RSA algorithm\", e);\n+        }\n+    }\n+\n+    @Override\n+    public EncryptedValue getEncryptedValue(String plaintext, KeyWithAlgorithm kwa) {\n+        checkArgument(kwa.getAlgorithm().equals(ALGORITHM_TYPE),\n+                \"key must be for RSA algorithm but was \" + kwa.getAlgorithm());\n+\n+        return EncryptedValueSupplier.silently(() -> {\n+            Cipher cipher = getUninitializedCipher();\n+            PublicKey publicKey = getPublicKey(kwa);\n+\n+            cipher.init(Cipher.ENCRYPT_MODE, publicKey);\n+            byte[] encrypted = cipher.doFinal(plaintext.getBytes(charset));\n+\n+            String encryptedString =  Base64.getEncoder().encodeToString(encrypted);\n+            return EncryptedValue.fromEncryptedString(encryptedString);\n+        });\n+    }\n+\n+    @Override\n+    public String getDecryptedString(EncryptedValue encryptedValue, KeyWithAlgorithm kwa) {\n+        checkArgument(kwa.getAlgorithm().equals(ALGORITHM_TYPE),\n+                \"key must be for RSA algorithm but was \" + kwa.getAlgorithm());\n+\n+        return DecryptedStringSupplier.silently(() -> {\n+            Cipher cipher = getUninitializedCipher();\n+            PrivateKey privateKey = getPrivateKey(kwa);\n+\n+            String ciphertext = encryptedValue.encryptedValue();\n+            byte[] cipherBytes = Base64.getDecoder().decode(ciphertext);\n+\n+            cipher.init(Cipher.DECRYPT_MODE, privateKey);\n+\n+            byte[] decrypted = cipher.doFinal(cipherBytes);\n+            return new String(decrypted, charset);\n+        });\n+    }\n+\n+    private static PublicKey getPublicKey(KeyWithAlgorithm key)","path":"encrypted-config-value/src/main/java/com/palantir/config/crypto/algorithm/RsaAlgorithm.java","position":null,"original_position":105,"commit_id":"afc706afa75472587f74b01ab1345c340d17dae0","original_commit_id":"5fa9a6b45b5a6d03721fd8380264a331b4574f60","user":{"login":"bavardage","id":24711,"avatar_url":"https://avatars.githubusercontent.com/u/24711?v=3","gravatar_id":"","url":"https://api.github.com/users/bavardage","html_url":"https://github.com/bavardage","followers_url":"https://api.github.com/users/bavardage/followers","following_url":"https://api.github.com/users/bavardage/following{/other_user}","gists_url":"https://api.github.com/users/bavardage/gists{/gist_id}","starred_url":"https://api.github.com/users/bavardage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bavardage/subscriptions","organizations_url":"https://api.github.com/users/bavardage/orgs","repos_url":"https://api.github.com/users/bavardage/repos","events_url":"https://api.github.com/users/bavardage/events{/privacy}","received_events_url":"https://api.github.com/users/bavardage/received_events","type":"User","site_admin":false},"body":"done","created_at":"2015-12-17T03:48:23Z","updated_at":"2015-12-17T03:48:23Z","html_url":"https://github.com/palantir/encrypted-config-value/pull/5#discussion_r47866581","pull_request_url":"https://api.github.com/repos/palantir/encrypted-config-value/pulls/5","_links":{"self":{"href":"https://api.github.com/repos/palantir/encrypted-config-value/pulls/comments/47866581"},"html":{"href":"https://github.com/palantir/encrypted-config-value/pull/5#discussion_r47866581"},"pull_request":{"href":"https://api.github.com/repos/palantir/encrypted-config-value/pulls/5"}}},"pull_request":{"url":"https://api.github.com/repos/palantir/encrypted-config-value/pulls/5","id":53909708,"html_url":"https://github.com/palantir/encrypted-config-value/pull/5","diff_url":"https://github.com/palantir/encrypted-config-value/pull/5.diff","patch_url":"https://github.com/palantir/encrypted-config-value/pull/5.patch","issue_url":"https://api.github.com/repos/palantir/encrypted-config-value/issues/5","number":5,"state":"open","locked":false,"title":"RSA algorithm, better key length for AES, other clean-up","user":{"login":"bavardage","id":24711,"avatar_url":"https://avatars.githubusercontent.com/u/24711?v=3","gravatar_id":"","url":"https://api.github.com/users/bavardage","html_url":"https://github.com/bavardage","followers_url":"https://api.github.com/users/bavardage/followers","following_url":"https://api.github.com/users/bavardage/following{/other_user}","gists_url":"https://api.github.com/users/bavardage/gists{/gist_id}","starred_url":"https://api.github.com/users/bavardage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bavardage/subscriptions","organizations_url":"https://api.github.com/users/bavardage/orgs","repos_url":"https://api.github.com/users/bavardage/repos","events_url":"https://api.github.com/users/bavardage/events{/privacy}","received_events_url":"https://api.github.com/users/bavardage/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-12-16T22:42:01Z","updated_at":"2015-12-17T03:48:23Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2d8c4c3db9982f9bd6d10b50a8b60d0d3d3a0bbc","assignee":{"login":"markelliot","id":685891,"avatar_url":"https://avatars.githubusercontent.com/u/685891?v=3","gravatar_id":"","url":"https://api.github.com/users/markelliot","html_url":"https://github.com/markelliot","followers_url":"https://api.github.com/users/markelliot/followers","following_url":"https://api.github.com/users/markelliot/following{/other_user}","gists_url":"https://api.github.com/users/markelliot/gists{/gist_id}","starred_url":"https://api.github.com/users/markelliot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markelliot/subscriptions","organizations_url":"https://api.github.com/users/markelliot/orgs","repos_url":"https://api.github.com/users/markelliot/repos","events_url":"https://api.github.com/users/markelliot/events{/privacy}","received_events_url":"https://api.github.com/users/markelliot/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/palantir/encrypted-config-value/pulls/5/commits","review_comments_url":"https://api.github.com/repos/palantir/encrypted-config-value/pulls/5/comments","review_comment_url":"https://api.github.com/repos/palantir/encrypted-config-value/pulls/comments{/number}","comments_url":"https://api.github.com/repos/palantir/encrypted-config-value/issues/5/comments","statuses_url":"https://api.github.com/repos/palantir/encrypted-config-value/statuses/afc706afa75472587f74b01ab1345c340d17dae0","head":{"label":"palantir:feature/keypairs","ref":"feature/keypairs","sha":"afc706afa75472587f74b01ab1345c340d17dae0","user":{"login":"palantir","id":303157,"avatar_url":"https://avatars.githubusercontent.com/u/303157?v=3","gravatar_id":"","url":"https://api.github.com/users/palantir","html_url":"https://github.com/palantir","followers_url":"https://api.github.com/users/palantir/followers","following_url":"https://api.github.com/users/palantir/following{/other_user}","gists_url":"https://api.github.com/users/palantir/gists{/gist_id}","starred_url":"https://api.github.com/users/palantir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/palantir/subscriptions","organizations_url":"https://api.github.com/users/palantir/orgs","repos_url":"https://api.github.com/users/palantir/repos","events_url":"https://api.github.com/users/palantir/events{/privacy}","received_events_url":"https://api.github.com/users/palantir/received_events","type":"Organization","site_admin":false},"repo":{"id":47428395,"name":"encrypted-config-value","full_name":"palantir/encrypted-config-value","owner":{"login":"palantir","id":303157,"avatar_url":"https://avatars.githubusercontent.com/u/303157?v=3","gravatar_id":"","url":"https://api.github.com/users/palantir","html_url":"https://github.com/palantir","followers_url":"https://api.github.com/users/palantir/followers","following_url":"https://api.github.com/users/palantir/following{/other_user}","gists_url":"https://api.github.com/users/palantir/gists{/gist_id}","starred_url":"https://api.github.com/users/palantir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/palantir/subscriptions","organizations_url":"https://api.github.com/users/palantir/orgs","repos_url":"https://api.github.com/users/palantir/repos","events_url":"https://api.github.com/users/palantir/events{/privacy}","received_events_url":"https://api.github.com/users/palantir/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/palantir/encrypted-config-value","description":"Tooling for encrypting certain configuration parameter values in dropwizard apps","fork":false,"url":"https://api.github.com/repos/palantir/encrypted-config-value","forks_url":"https://api.github.com/repos/palantir/encrypted-config-value/forks","keys_url":"https://api.github.com/repos/palantir/encrypted-config-value/keys{/key_id}","collaborators_url":"https://api.github.com/repos/palantir/encrypted-config-value/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/palantir/encrypted-config-value/teams","hooks_url":"https://api.github.com/repos/palantir/encrypted-config-value/hooks","issue_events_url":"https://api.github.com/repos/palantir/encrypted-config-value/issues/events{/number}","events_url":"https://api.github.com/repos/palantir/encrypted-config-value/events","assignees_url":"https://api.github.com/repos/palantir/encrypted-config-value/assignees{/user}","branches_url":"https://api.github.com/repos/palantir/encrypted-config-value/branches{/branch}","tags_url":"https://api.github.com/repos/palantir/encrypted-config-value/tags","blobs_url":"https://api.github.com/repos/palantir/encrypted-config-value/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/palantir/encrypted-config-value/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/palantir/encrypted-config-value/git/refs{/sha}","trees_url":"https://api.github.com/repos/palantir/encrypted-config-value/git/trees{/sha}","statuses_url":"https://api.github.com/repos/palantir/encrypted-config-value/statuses/{sha}","languages_url":"https://api.github.com/repos/palantir/encrypted-config-value/languages","stargazers_url":"https://api.github.com/repos/palantir/encrypted-config-value/stargazers","contributors_url":"https://api.github.com/repos/palantir/encrypted-config-value/contributors","subscribers_url":"https://api.github.com/repos/palantir/encrypted-config-value/subscribers","subscription_url":"https://api.github.com/repos/palantir/encrypted-config-value/subscription","commits_url":"https://api.github.com/repos/palantir/encrypted-config-value/commits{/sha}","git_commits_url":"https://api.github.com/repos/palantir/encrypted-config-value/git/commits{/sha}","comments_url":"https://api.github.com/repos/palantir/encrypted-config-value/comments{/number}","issue_comment_url":"https://api.github.com/repos/palantir/encrypted-config-value/issues/comments{/number}","contents_url":"https://api.github.com/repos/palantir/encrypted-config-value/contents/{+path}","compare_url":"https://api.github.com/repos/palantir/encrypted-config-value/compare/{base}...{head}","merges_url":"https://api.github.com/repos/palantir/encrypted-config-value/merges","archive_url":"https://api.github.com/repos/palantir/encrypted-config-value/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/palantir/encrypted-config-value/downloads","issues_url":"https://api.github.com/repos/palantir/encrypted-config-value/issues{/number}","pulls_url":"https://api.github.com/repos/palantir/encrypted-config-value/pulls{/number}","milestones_url":"https://api.github.com/repos/palantir/encrypted-config-value/milestones{/number}","notifications_url":"https://api.github.com/repos/palantir/encrypted-config-value/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/palantir/encrypted-config-value/labels{/name}","releases_url":"https://api.github.com/repos/palantir/encrypted-config-value/releases{/id}","created_at":"2015-12-04T20:56:14Z","updated_at":"2015-12-16T22:41:32Z","pushed_at":"2015-12-17T03:30:15Z","git_url":"git://github.com/palantir/encrypted-config-value.git","ssh_url":"git@github.com:palantir/encrypted-config-value.git","clone_url":"https://github.com/palantir/encrypted-config-value.git","svn_url":"https://github.com/palantir/encrypted-config-value","homepage":"","size":128,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"develop"}},"base":{"label":"palantir:develop","ref":"develop","sha":"98e40b085915892a67ec4ac922df04a53a45285b","user":{"login":"palantir","id":303157,"avatar_url":"https://avatars.githubusercontent.com/u/303157?v=3","gravatar_id":"","url":"https://api.github.com/users/palantir","html_url":"https://github.com/palantir","followers_url":"https://api.github.com/users/palantir/followers","following_url":"https://api.github.com/users/palantir/following{/other_user}","gists_url":"https://api.github.com/users/palantir/gists{/gist_id}","starred_url":"https://api.github.com/users/palantir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/palantir/subscriptions","organizations_url":"https://api.github.com/users/palantir/orgs","repos_url":"https://api.github.com/users/palantir/repos","events_url":"https://api.github.com/users/palantir/events{/privacy}","received_events_url":"https://api.github.com/users/palantir/received_events","type":"Organization","site_admin":false},"repo":{"id":47428395,"name":"encrypted-config-value","full_name":"palantir/encrypted-config-value","owner":{"login":"palantir","id":303157,"avatar_url":"https://avatars.githubusercontent.com/u/303157?v=3","gravatar_id":"","url":"https://api.github.com/users/palantir","html_url":"https://github.com/palantir","followers_url":"https://api.github.com/users/palantir/followers","following_url":"https://api.github.com/users/palantir/following{/other_user}","gists_url":"https://api.github.com/users/palantir/gists{/gist_id}","starred_url":"https://api.github.com/users/palantir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/palantir/subscriptions","organizations_url":"https://api.github.com/users/palantir/orgs","repos_url":"https://api.github.com/users/palantir/repos","events_url":"https://api.github.com/users/palantir/events{/privacy}","received_events_url":"https://api.github.com/users/palantir/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/palantir/encrypted-config-value","description":"Tooling for encrypting certain configuration parameter values in dropwizard apps","fork":false,"url":"https://api.github.com/repos/palantir/encrypted-config-value","forks_url":"https://api.github.com/repos/palantir/encrypted-config-value/forks","keys_url":"https://api.github.com/repos/palantir/encrypted-config-value/keys{/key_id}","collaborators_url":"https://api.github.com/repos/palantir/encrypted-config-value/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/palantir/encrypted-config-value/teams","hooks_url":"https://api.github.com/repos/palantir/encrypted-config-value/hooks","issue_events_url":"https://api.github.com/repos/palantir/encrypted-config-value/issues/events{/number}","events_url":"https://api.github.com/repos/palantir/encrypted-config-value/events","assignees_url":"https://api.github.com/repos/palantir/encrypted-config-value/assignees{/user}","branches_url":"https://api.github.com/repos/palantir/encrypted-config-value/branches{/branch}","tags_url":"https://api.github.com/repos/palantir/encrypted-config-value/tags","blobs_url":"https://api.github.com/repos/palantir/encrypted-config-value/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/palantir/encrypted-config-value/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/palantir/encrypted-config-value/git/refs{/sha}","trees_url":"https://api.github.com/repos/palantir/encrypted-config-value/git/trees{/sha}","statuses_url":"https://api.github.com/repos/palantir/encrypted-config-value/statuses/{sha}","languages_url":"https://api.github.com/repos/palantir/encrypted-config-value/languages","stargazers_url":"https://api.github.com/repos/palantir/encrypted-config-value/stargazers","contributors_url":"https://api.github.com/repos/palantir/encrypted-config-value/contributors","subscribers_url":"https://api.github.com/repos/palantir/encrypted-config-value/subscribers","subscription_url":"https://api.github.com/repos/palantir/encrypted-config-value/subscription","commits_url":"https://api.github.com/repos/palantir/encrypted-config-value/commits{/sha}","git_commits_url":"https://api.github.com/repos/palantir/encrypted-config-value/git/commits{/sha}","comments_url":"https://api.github.com/repos/palantir/encrypted-config-value/comments{/number}","issue_comment_url":"https://api.github.com/repos/palantir/encrypted-config-value/issues/comments{/number}","contents_url":"https://api.github.com/repos/palantir/encrypted-config-value/contents/{+path}","compare_url":"https://api.github.com/repos/palantir/encrypted-config-value/compare/{base}...{head}","merges_url":"https://api.github.com/repos/palantir/encrypted-config-value/merges","archive_url":"https://api.github.com/repos/palantir/encrypted-config-value/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/palantir/encrypted-config-value/downloads","issues_url":"https://api.github.com/repos/palantir/encrypted-config-value/issues{/number}","pulls_url":"https://api.github.com/repos/palantir/encrypted-config-value/pulls{/number}","milestones_url":"https://api.github.com/repos/palantir/encrypted-config-value/milestones{/number}","notifications_url":"https://api.github.com/repos/palantir/encrypted-config-value/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/palantir/encrypted-config-value/labels{/name}","releases_url":"https://api.github.com/repos/palantir/encrypted-config-value/releases{/id}","created_at":"2015-12-04T20:56:14Z","updated_at":"2015-12-16T22:41:32Z","pushed_at":"2015-12-17T03:30:15Z","git_url":"git://github.com/palantir/encrypted-config-value.git","ssh_url":"git@github.com:palantir/encrypted-config-value.git","clone_url":"https://github.com/palantir/encrypted-config-value.git","svn_url":"https://github.com/palantir/encrypted-config-value","homepage":"","size":128,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/palantir/encrypted-config-value/pulls/5"},"html":{"href":"https://github.com/palantir/encrypted-config-value/pull/5"},"issue":{"href":"https://api.github.com/repos/palantir/encrypted-config-value/issues/5"},"comments":{"href":"https://api.github.com/repos/palantir/encrypted-config-value/issues/5/comments"},"review_comments":{"href":"https://api.github.com/repos/palantir/encrypted-config-value/pulls/5/comments"},"review_comment":{"href":"https://api.github.com/repos/palantir/encrypted-config-value/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/palantir/encrypted-config-value/pulls/5/commits"},"statuses":{"href":"https://api.github.com/repos/palantir/encrypted-config-value/statuses/afc706afa75472587f74b01ab1345c340d17dae0"}}}}