{"action":"created","comment":{"url":"https://api.github.com/repos/defuse/php-encryption/pulls/comments/32890729","id":32890729,"diff_hunk":"@@ -0,0 +1,397 @@\n+<?php\n+namespace Defuse\\Crypto;\n+\n+use \\Defuse\\Crypto\\Exception as Ex;\n+\n+final class File extends Core implements StreamInterface\n+{\n+    const BUFFER = 8192;\n+    const CIPHER_MODE = 'ctr';\n+    \n+    /**\n+     * Encrypt the contents at $inputFilename, storing the result in $outputFilename\n+     * using HKDF of $key to perform authenticated encryption\n+     * \n+     * @param string $inputFilename\n+     * @param string $outputFilename\n+     * @param string $key\n+     * @return boolean\n+     */\n+    public static function encryptFile($inputFilename, $outputFilename, $key)\n+    {\n+        if (!is_string($inputFilename)) {\n+            throw new Ex\\InvalidInput(\"Input filename must be a string!\");\n+        }\n+        if (!is_string($outputFilename)) {\n+            throw new Ex\\InvalidInput(\"Output filename must be a string!\");\n+        }\n+        \n+        /**\n+         * Open the file handles\n+         */\n+        // Input file handle\n+        $if = \\fopen($inputFilename, 'rb');\n+        \\stream_set_read_buffer($if, self::BUFFER);\n+        \n+        // Output file handle\n+        $of = \\fopen($outputFilename, 'wb');\n+        \\stream_set_write_buffer($of, 0);\n+        \n+        // Use encryptResource() to actually write the encrypted data to $of\n+        $encrypted = self::encryptResource($if, $of, $key);\n+        \n+        // Close handlers\n+        \\fclose($if);\n+        \\fclose($of);\n+        \n+        // Return the result (which should be true)\n+        return $encrypted;\n+    }\n+    \n+    /**\n+     * Decrypt the contents at $inputFilename, storing the result in $outputFilename\n+     * using HKDF of $key to decrypt then verify\n+     * \n+     * @param string $inputFilename\n+     * @param string $outputFilename\n+     * @param string $key\n+     * @return boolean\n+     */\n+    public static function decryptFile($inputFilename, $outputFilename, $key)\n+    {\n+        /**\n+         * Open the file handles\n+         */\n+        // Input file handle\n+        $if = \\fopen($inputFilename, 'rb');\n+        \\stream_set_read_buffer($if, self::BUFFER);\n+        \n+        // Output file handle\n+        $of = \\fopen($outputFilename, 'wb');\n+        \\stream_set_write_buffer($of, 0);\n+        \n+        // Use decryptResource() to actually write the decrypted data to $of\n+        $decrypted = self::decryptResource($if, $of, $key);\n+        \n+        // Close handlers\n+        \\fclose($if);\n+        \\fclose($of);\n+        \n+        // Return the result (which should be true)\n+        return $decrypted;\n+    }\n+    \n+    /**\n+     * Encrypt the contents of a file handle $inputHandle and store the results\n+     * in $outputHandle using HKDF of $key to perform authenticated encryption\n+     * \n+     * @param resource $inputHandle\n+     * @param resource $outputHandle\n+     * @param string $key\n+     * @return boolean\n+     */\n+    public static function encryptResource($inputHandle, $outputHandle, $key)\n+    {\n+        // Because we don't have strict typing in PHP 5\n+        if (!is_resource($inputHandle)) {\n+            throw new Ex\\InvalidInput(\"Input handle must be a resource!\");\n+        }\n+        if (!is_resource($outputHandle)) {\n+            throw new Ex\\InvalidInput(\"Output handle must be a resource!\");\n+        }\n+        \n+        // Sanity check; key must be the appropriate length!\n+        if (self::ourStrlen($key) !== self::KEY_BYTE_SIZE) {\n+            throw new Ex\\InvalidInput(\"Bad key.\");\n+        }\n+        \n+        // 'aes-128-ctr' unless someone mucked with the config in Core.php\n+        $method = self::CIPHER.'-'.self::CIPHER_MODE;\n+        \n+        /**\n+         *  Let's split our keys\n+         */\n+        \n+        // $ekey -- Encryption Key -- used for AES\n+        $ekey = self::HKDF(\n+            self::HASH_FUNCTION,\n+            $key,\n+            self::KEY_BYTE_SIZE,\n+            self::ENCRYPTION_INFO\n+        );\n+        \n+        // $akey -- Authentication Key -- used for HMAC\n+        $akey = self::HKDF(\n+            self::HASH_FUNCTION,\n+            $key,\n+            self::KEY_BYTE_SIZE,\n+            self::AUTHENTICATION_INFO\n+        );\n+        \n+        /**\n+         *  Generate a random initialization vector.\n+         */\n+        self::ensureFunctionExists(\"openssl_cipher_iv_length\");\n+        $ivsize = openssl_cipher_iv_length($method);\n+        if ($ivsize === FALSE || $ivsize <= 0) {\n+            throw new Ex\\CannotPerformOperation('Improper IV size');\n+        }\n+        $iv = self::secureRandom($ivsize);\n+        \n+        /**\n+         * First let's write our IV to the first N blocks of the output file\n+         */\n+        \\fwrite($outputHandle, $iv, $ivsize);\n+        \n+        /**\n+         * We're going to initialize a HMAC-SHA256 with the given $akey\n+         * and update it with each ciphertext chunk\n+         */\n+        $hmac = \\hash_init(self::HASH_FUNCTION, HASH_HMAC, $akey);","path":"src/File.php","position":231,"original_position":150,"commit_id":"115b42f091ca8f781f7af85dc7b815ae1ce4cc22","original_commit_id":"11c7aa95dc37fe06dc2a2ea6b26fbe583e2a5829","user":{"login":"paragonie-scott","id":11591518,"avatar_url":"https://avatars.githubusercontent.com/u/11591518?v=3","gravatar_id":"","url":"https://api.github.com/users/paragonie-scott","html_url":"https://github.com/paragonie-scott","followers_url":"https://api.github.com/users/paragonie-scott/followers","following_url":"https://api.github.com/users/paragonie-scott/following{/other_user}","gists_url":"https://api.github.com/users/paragonie-scott/gists{/gist_id}","starred_url":"https://api.github.com/users/paragonie-scott/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paragonie-scott/subscriptions","organizations_url":"https://api.github.com/users/paragonie-scott/orgs","repos_url":"https://api.github.com/users/paragonie-scott/repos","events_url":"https://api.github.com/users/paragonie-scott/events{/privacy}","received_events_url":"https://api.github.com/users/paragonie-scott/received_events","type":"User","site_admin":false},"body":"(For people scanning the remarks, the commits since @defuse posted this one have added a check here on the following lines, but github doesn't mark this remark as outdated.)","created_at":"2015-06-21T05:54:07Z","updated_at":"2015-06-21T05:54:07Z","html_url":"https://github.com/defuse/php-encryption/pull/63#discussion_r32890729","pull_request_url":"https://api.github.com/repos/defuse/php-encryption/pulls/63","_links":{"self":{"href":"https://api.github.com/repos/defuse/php-encryption/pulls/comments/32890729"},"html":{"href":"https://github.com/defuse/php-encryption/pull/63#discussion_r32890729"},"pull_request":{"href":"https://api.github.com/repos/defuse/php-encryption/pulls/63"}}},"pull_request":{"url":"https://api.github.com/repos/defuse/php-encryption/pulls/63","id":37496552,"html_url":"https://github.com/defuse/php-encryption/pull/63","diff_url":"https://github.com/defuse/php-encryption/pull/63.diff","patch_url":"https://github.com/defuse/php-encryption/pull/63.patch","issue_url":"https://api.github.com/repos/defuse/php-encryption/issues/63","number":63,"state":"open","locked":false,"title":"Large file encryption/decryption","user":{"login":"paragonie-scott","id":11591518,"avatar_url":"https://avatars.githubusercontent.com/u/11591518?v=3","gravatar_id":"","url":"https://api.github.com/users/paragonie-scott","html_url":"https://github.com/paragonie-scott","followers_url":"https://api.github.com/users/paragonie-scott/followers","following_url":"https://api.github.com/users/paragonie-scott/following{/other_user}","gists_url":"https://api.github.com/users/paragonie-scott/gists{/gist_id}","starred_url":"https://api.github.com/users/paragonie-scott/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paragonie-scott/subscriptions","organizations_url":"https://api.github.com/users/paragonie-scott/orgs","repos_url":"https://api.github.com/users/paragonie-scott/repos","events_url":"https://api.github.com/users/paragonie-scott/events{/privacy}","received_events_url":"https://api.github.com/users/paragonie-scott/received_events","type":"User","site_admin":false},"body":"Differences from `Crypto`:\r\n* AES-CTR instead of AES-CBC\r\n* Operates on resources rather than strings\r\n* Decryption is slightly slow; it recalculates the MAC over the entire file and doesn't decrypt if it fails to validate.\r\n\r\nChanges to `Crypto`:\r\n* All common denominators between the two classes have been moved to a separate class called `Core`.\r\n\r\nThings we would like to test for before merging:\r\n* Is nonce reuse sufficiently mitigated?\r\n* Can we make the buffer size irrelevant to the decryption process?\r\n* Should we implement versioning in the regular library **before** adding this feature, and then make the ciphertext here versioned too?\r\n\r\nThis should close #39 (and #38?) when it's merged.","created_at":"2015-06-11T18:55:39Z","updated_at":"2015-06-21T05:54:07Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d75495c0e08483ad9123b1d55639ca2d790f316f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/defuse/php-encryption/pulls/63/commits","review_comments_url":"https://api.github.com/repos/defuse/php-encryption/pulls/63/comments","review_comment_url":"https://api.github.com/repos/defuse/php-encryption/pulls/comments{/number}","comments_url":"https://api.github.com/repos/defuse/php-encryption/issues/63/comments","statuses_url":"https://api.github.com/repos/defuse/php-encryption/statuses/115b42f091ca8f781f7af85dc7b815ae1ce4cc22","head":{"label":"paragonie:file_encryption","ref":"file_encryption","sha":"115b42f091ca8f781f7af85dc7b815ae1ce4cc22","user":{"login":"paragonie","id":11591525,"avatar_url":"https://avatars.githubusercontent.com/u/11591525?v=3","gravatar_id":"","url":"https://api.github.com/users/paragonie","html_url":"https://github.com/paragonie","followers_url":"https://api.github.com/users/paragonie/followers","following_url":"https://api.github.com/users/paragonie/following{/other_user}","gists_url":"https://api.github.com/users/paragonie/gists{/gist_id}","starred_url":"https://api.github.com/users/paragonie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paragonie/subscriptions","organizations_url":"https://api.github.com/users/paragonie/orgs","repos_url":"https://api.github.com/users/paragonie/repos","events_url":"https://api.github.com/users/paragonie/events{/privacy}","received_events_url":"https://api.github.com/users/paragonie/received_events","type":"Organization","site_admin":false},"repo":{"id":37230871,"name":"php-encryption","full_name":"paragonie/php-encryption","owner":{"login":"paragonie","id":11591525,"avatar_url":"https://avatars.githubusercontent.com/u/11591525?v=3","gravatar_id":"","url":"https://api.github.com/users/paragonie","html_url":"https://github.com/paragonie","followers_url":"https://api.github.com/users/paragonie/followers","following_url":"https://api.github.com/users/paragonie/following{/other_user}","gists_url":"https://api.github.com/users/paragonie/gists{/gist_id}","starred_url":"https://api.github.com/users/paragonie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paragonie/subscriptions","organizations_url":"https://api.github.com/users/paragonie/orgs","repos_url":"https://api.github.com/users/paragonie/repos","events_url":"https://api.github.com/users/paragonie/events{/privacy}","received_events_url":"https://api.github.com/users/paragonie/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/paragonie/php-encryption","description":"Simple Encryption in PHP.","fork":true,"url":"https://api.github.com/repos/paragonie/php-encryption","forks_url":"https://api.github.com/repos/paragonie/php-encryption/forks","keys_url":"https://api.github.com/repos/paragonie/php-encryption/keys{/key_id}","collaborators_url":"https://api.github.com/repos/paragonie/php-encryption/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/paragonie/php-encryption/teams","hooks_url":"https://api.github.com/repos/paragonie/php-encryption/hooks","issue_events_url":"https://api.github.com/repos/paragonie/php-encryption/issues/events{/number}","events_url":"https://api.github.com/repos/paragonie/php-encryption/events","assignees_url":"https://api.github.com/repos/paragonie/php-encryption/assignees{/user}","branches_url":"https://api.github.com/repos/paragonie/php-encryption/branches{/branch}","tags_url":"https://api.github.com/repos/paragonie/php-encryption/tags","blobs_url":"https://api.github.com/repos/paragonie/php-encryption/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/paragonie/php-encryption/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/paragonie/php-encryption/git/refs{/sha}","trees_url":"https://api.github.com/repos/paragonie/php-encryption/git/trees{/sha}","statuses_url":"https://api.github.com/repos/paragonie/php-encryption/statuses/{sha}","languages_url":"https://api.github.com/repos/paragonie/php-encryption/languages","stargazers_url":"https://api.github.com/repos/paragonie/php-encryption/stargazers","contributors_url":"https://api.github.com/repos/paragonie/php-encryption/contributors","subscribers_url":"https://api.github.com/repos/paragonie/php-encryption/subscribers","subscription_url":"https://api.github.com/repos/paragonie/php-encryption/subscription","commits_url":"https://api.github.com/repos/paragonie/php-encryption/commits{/sha}","git_commits_url":"https://api.github.com/repos/paragonie/php-encryption/git/commits{/sha}","comments_url":"https://api.github.com/repos/paragonie/php-encryption/comments{/number}","issue_comment_url":"https://api.github.com/repos/paragonie/php-encryption/issues/comments{/number}","contents_url":"https://api.github.com/repos/paragonie/php-encryption/contents/{+path}","compare_url":"https://api.github.com/repos/paragonie/php-encryption/compare/{base}...{head}","merges_url":"https://api.github.com/repos/paragonie/php-encryption/merges","archive_url":"https://api.github.com/repos/paragonie/php-encryption/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/paragonie/php-encryption/downloads","issues_url":"https://api.github.com/repos/paragonie/php-encryption/issues{/number}","pulls_url":"https://api.github.com/repos/paragonie/php-encryption/pulls{/number}","milestones_url":"https://api.github.com/repos/paragonie/php-encryption/milestones{/number}","notifications_url":"https://api.github.com/repos/paragonie/php-encryption/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/paragonie/php-encryption/labels{/name}","releases_url":"https://api.github.com/repos/paragonie/php-encryption/releases{/id}","created_at":"2015-06-11T00:52:16Z","updated_at":"2015-06-11T00:52:18Z","pushed_at":"2015-06-21T05:47:36Z","git_url":"git://github.com/paragonie/php-encryption.git","ssh_url":"git@github.com:paragonie/php-encryption.git","clone_url":"https://github.com/paragonie/php-encryption.git","svn_url":"https://github.com/paragonie/php-encryption","homepage":null,"size":12717,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"defuse:master","ref":"master","sha":"90bb7de6ba04581c5bbc704adfa6d8e6309d6583","user":{"login":"defuse","id":4211072,"avatar_url":"https://avatars.githubusercontent.com/u/4211072?v=3","gravatar_id":"","url":"https://api.github.com/users/defuse","html_url":"https://github.com/defuse","followers_url":"https://api.github.com/users/defuse/followers","following_url":"https://api.github.com/users/defuse/following{/other_user}","gists_url":"https://api.github.com/users/defuse/gists{/gist_id}","starred_url":"https://api.github.com/users/defuse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/defuse/subscriptions","organizations_url":"https://api.github.com/users/defuse/orgs","repos_url":"https://api.github.com/users/defuse/repos","events_url":"https://api.github.com/users/defuse/events{/privacy}","received_events_url":"https://api.github.com/users/defuse/received_events","type":"User","site_admin":false},"repo":{"id":16571051,"name":"php-encryption","full_name":"defuse/php-encryption","owner":{"login":"defuse","id":4211072,"avatar_url":"https://avatars.githubusercontent.com/u/4211072?v=3","gravatar_id":"","url":"https://api.github.com/users/defuse","html_url":"https://github.com/defuse","followers_url":"https://api.github.com/users/defuse/followers","following_url":"https://api.github.com/users/defuse/following{/other_user}","gists_url":"https://api.github.com/users/defuse/gists{/gist_id}","starred_url":"https://api.github.com/users/defuse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/defuse/subscriptions","organizations_url":"https://api.github.com/users/defuse/orgs","repos_url":"https://api.github.com/users/defuse/repos","events_url":"https://api.github.com/users/defuse/events{/privacy}","received_events_url":"https://api.github.com/users/defuse/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/defuse/php-encryption","description":"Simple Encryption in PHP.","fork":false,"url":"https://api.github.com/repos/defuse/php-encryption","forks_url":"https://api.github.com/repos/defuse/php-encryption/forks","keys_url":"https://api.github.com/repos/defuse/php-encryption/keys{/key_id}","collaborators_url":"https://api.github.com/repos/defuse/php-encryption/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/defuse/php-encryption/teams","hooks_url":"https://api.github.com/repos/defuse/php-encryption/hooks","issue_events_url":"https://api.github.com/repos/defuse/php-encryption/issues/events{/number}","events_url":"https://api.github.com/repos/defuse/php-encryption/events","assignees_url":"https://api.github.com/repos/defuse/php-encryption/assignees{/user}","branches_url":"https://api.github.com/repos/defuse/php-encryption/branches{/branch}","tags_url":"https://api.github.com/repos/defuse/php-encryption/tags","blobs_url":"https://api.github.com/repos/defuse/php-encryption/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/defuse/php-encryption/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/defuse/php-encryption/git/refs{/sha}","trees_url":"https://api.github.com/repos/defuse/php-encryption/git/trees{/sha}","statuses_url":"https://api.github.com/repos/defuse/php-encryption/statuses/{sha}","languages_url":"https://api.github.com/repos/defuse/php-encryption/languages","stargazers_url":"https://api.github.com/repos/defuse/php-encryption/stargazers","contributors_url":"https://api.github.com/repos/defuse/php-encryption/contributors","subscribers_url":"https://api.github.com/repos/defuse/php-encryption/subscribers","subscription_url":"https://api.github.com/repos/defuse/php-encryption/subscription","commits_url":"https://api.github.com/repos/defuse/php-encryption/commits{/sha}","git_commits_url":"https://api.github.com/repos/defuse/php-encryption/git/commits{/sha}","comments_url":"https://api.github.com/repos/defuse/php-encryption/comments{/number}","issue_comment_url":"https://api.github.com/repos/defuse/php-encryption/issues/comments{/number}","contents_url":"https://api.github.com/repos/defuse/php-encryption/contents/{+path}","compare_url":"https://api.github.com/repos/defuse/php-encryption/compare/{base}...{head}","merges_url":"https://api.github.com/repos/defuse/php-encryption/merges","archive_url":"https://api.github.com/repos/defuse/php-encryption/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/defuse/php-encryption/downloads","issues_url":"https://api.github.com/repos/defuse/php-encryption/issues{/number}","pulls_url":"https://api.github.com/repos/defuse/php-encryption/pulls{/number}","milestones_url":"https://api.github.com/repos/defuse/php-encryption/milestones{/number}","notifications_url":"https://api.github.com/repos/defuse/php-encryption/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/defuse/php-encryption/labels{/name}","releases_url":"https://api.github.com/repos/defuse/php-encryption/releases{/id}","created_at":"2014-02-06T06:47:32Z","updated_at":"2015-06-21T05:18:11Z","pushed_at":"2015-06-21T05:47:37Z","git_url":"git://github.com/defuse/php-encryption.git","ssh_url":"git@github.com:defuse/php-encryption.git","clone_url":"https://github.com/defuse/php-encryption.git","svn_url":"https://github.com/defuse/php-encryption","homepage":null,"size":877,"stargazers_count":293,"watchers_count":293,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":31,"mirror_url":null,"open_issues_count":17,"forks":31,"open_issues":17,"watchers":293,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/defuse/php-encryption/pulls/63"},"html":{"href":"https://github.com/defuse/php-encryption/pull/63"},"issue":{"href":"https://api.github.com/repos/defuse/php-encryption/issues/63"},"comments":{"href":"https://api.github.com/repos/defuse/php-encryption/issues/63/comments"},"review_comments":{"href":"https://api.github.com/repos/defuse/php-encryption/pulls/63/comments"},"review_comment":{"href":"https://api.github.com/repos/defuse/php-encryption/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/defuse/php-encryption/pulls/63/commits"},"statuses":{"href":"https://api.github.com/repos/defuse/php-encryption/statuses/115b42f091ca8f781f7af85dc7b815ae1ce4cc22"}}}}