{"action":"created","comment":{"url":"https://api.github.com/repos/freenet/fred/pulls/comments/27094354","id":27094354,"diff_hunk":"@@ -0,0 +1,205 @@\n+/* This code is part of Freenet. It is distributed under the GNU General\n+ * Public License, version 2 (or at your option any later version). See\n+ * http://www.gnu.org/ for further details of the GPL. */\n+package freenet.crypt;\n+\n+import java.nio.ByteBuffer;\n+import java.security.MessageDigest;\n+\n+import net.i2p.util.NativeBigInteger;\n+import freenet.support.HexUtil;\n+\n+/**\n+ * The Hash class will generate the hash value of a given set of bytes and also verify that\n+ * a hash matches a given set of bytes. The addBytes methods can be used to pass data into \n+ * a buffer that will be used to generate a hash. Once a hash is generated, the buffer is \n+ * cleared or reset. \n+ * @author unixninja92\n+ *\n+ */\n+public final class Hash{\n+\tpublic static final HashType preferredHashAlg = HashType.SHA256;\n+\tprivate final HashType type;\n+\tprivate MessageDigest digest;\n+\t\n+\t/**\n+\t * Creates an instance of Hash using the specified hashing algorithm.\n+\t * @param type The hashing algorithm to use. \n+\t */\n+\tpublic Hash(HashType type){\n+\t\tthis.type = type;\n+\t\tdigest = type.get();\n+\t}\n+\t\n+\t/**\n+\t * Generates the hash of all the bytes in the buffer added with the\n+\t * addBytes methods. The buffer is then cleared after the hash has been\n+\t * generated.\n+\t * @return The generated hash of all the bytes added since last reset.\n+\t */\n+\tpublic final byte[] genHash(){\n+\t\tbyte[] result = digest.digest();\n+\t\tif(type == HashType.ED2K){\n+\t\t\t//ED2K does not reset after generating a digest. Work around this issue\n+\t\t\tdigest.reset();\n+\t\t}else if(type == HashType.TTH){\n+\t\t\t//TTH's .reset method is broken or isn't implemented. Work around this bug\n+\t\t\tdigest = type.get();\n+\t\t}\n+\t\treturn result;\n+\t}\n+\t\n+\t/**\n+\t * Generates the hash of only the specified bytes. The buffer is cleared before \n+\t * processing the input to ensure that no extra data is included. Once the hash\n+\t * has been generated, the buffer is cleared again. \n+\t * @param input The bytes to hash\n+\t * @return The generated hash of the data\n+\t */\n+\tpublic final byte[] genHash(byte[]... input) {\n+\t\tdigest.reset();\n+\t\taddBytes(input);\n+\t\treturn genHash();\n+\t}\n+\t\n+\t/**\n+\t * Generates the HashResult of all the bytes in the buffer added with the\n+\t * addBytes methods. The buffer is then cleared after the hash has been\n+\t * generated.\n+\t * @return The generated hash as a HashResult of all the bytes added \n+\t * since last reset.\n+\t */\n+\tpublic final HashResult genHashResult() {\n+\t\treturn new HashResult(type, genHash());\n+\t}\n+\t\n+\t/**\n+\t * Generates the hash as a HashResult string of only the specified bytes. \n+\t * The buffer is cleared before processing the input to ensure that no \n+\t * extra data is included. Once the hash has been generated, the buffer \n+\t * is cleared again. \n+\t * @param input The bytes to hash\n+\t * @return The generated hash as a HashResult of the data\n+\t */\n+\tpublic final HashResult genHashResult(byte[]... input){\n+\t\tdigest.reset();\n+\t\taddBytes(input);\n+\t\treturn genHashResult();\n+\t}\n+\t\n+\t/**\n+\t * Generates the hash as a hex string of all the bytes in the buffer added \n+\t * with the addBytes methods. The buffer is then cleared after the hash \n+\t * has been generated.\n+\t * @return The generated hash as a hex string of all the bytes added since \n+\t * last reset.\n+\t */\n+\tpublic final String genHexHash() {\n+\t\treturn HexUtil.bytesToHex(genHash());\n+\t}\n+\t\n+\t/**\n+\t * Generates the hash as a NativeBigInteger of all the bytes in the buffer \n+\t * added with the addBytes methods. The buffer is then cleared after the hash \n+\t * has been generated.\n+\t * @return The generated hash as a NativeBigInteger of all the bytes added \n+\t * since last reset.\n+\t */\n+\tpublic final NativeBigInteger genNativeBigIntegerHash(){\n+\t\treturn new NativeBigInteger(1, genHash());\n+\t}\n+\t\n+\t/**\n+\t * Generates the hash as a NativeBigInteger string of only the specified \n+\t * bytes. The buffer is cleared before processing the input to ensure that \n+\t * no extra data is included. Once the hash has been generated, the buffer \n+\t * is cleared again. \n+\t * @param input The bytes to hash\n+\t * @return The generated hash as a NativeBigInteger of the data\n+\t */\n+\tpublic final NativeBigInteger genNativeBigIntegerHash(byte[]... data){\n+\t\tdigest.reset();\n+\t\taddBytes(data);\n+\t\treturn genNativeBigIntegerHash();\n+\t}\n+\t\n+\t/**\n+\t * Adds the specified byte to the buffer of bytes to be hashed. \n+\t * @param input Byte to be added to hash\n+\t */\n+\tpublic final void addByte(byte input){\n+\t\tdigest.update(input);\n+\t}\n+\n+\t/**\n+\t * Adds the specified byte arrays to the buffer of bytes to be hashed. \n+\t * @param input The byte[]s to add\n+\t */\n+\tpublic final void addBytes(byte[]... input){\n+\t\tfor(byte[] b: input){\n+\t\t\tdigest.update(b);\n+\t\t}\n+\t}\n+\n+\t/**\n+\t * Adds the remaining bytes from a  ByteBuffer to the buffer of bytes \n+\t * to be hashed. The bytes read from the ByteBuffer will be from \n+\t * input.position() to input.remaining(). Upon return, the ByteBuffer's\n+\t * .position() will be equal to .remaining() and .remaining() will \n+\t * stay unchanged. \n+\t * @param input The ByteBuffer to be hashed\n+\t */\n+\tpublic final void addBytes(ByteBuffer input){\n+\t\tdigest.update(input);\n+\t}\n+\t\n+\t/**\n+\t * Adds the specified portion of the byte[] passed in to the buffer \n+\t * of bytes to be hashed.\n+\t * @param input The array containing bytes to be hashed\n+\t * @param offset Where the first byte to hash is\n+\t * @param len How many bytes after the offset to add to hash.\n+\t */\n+\tpublic final void addBytes(byte[] input, int offset, int len){\n+\t\tdigest.update(input, offset, len);\n+\t}\n+\t\n+\t/**\n+\t * Generates the hash of the byte arrays provided and checks to see if that hash\n+\t * is the same as the one passed in. The buffer is cleared before processing the \n+\t * input to ensure that no extra data is included. Once the hash has been \n+\t * generated, the buffer is cleared again. \n+\t * @param hash The hash to be verified\n+\t * @param data The data to be hashed\n+\t * @return Returns true if the generated hash matches the passed in hash. \n+\t * Otherwise returns false.\n+\t */\n+\tpublic final boolean verify(byte[] hash, byte[]... data){\n+\t\treturn MessageDigest.isEqual(hash, genHash(data));\n+\t}\n+\t\n+\t/**\n+\t * Checks to see if the HashResults passed in are equivalent. Does a simple byte","path":"src/freenet/crypt/Hash.java","position":181,"original_position":182,"commit_id":"d7031ea233eaadfd2dd95180e8af66bef08e025b","original_commit_id":"0857e879a77f19a3e87e2ef2bcd5e03a93a38761","user":{"login":"Thynix","id":199917,"avatar_url":"https://avatars.githubusercontent.com/u/199917?v=3","gravatar_id":"","url":"https://api.github.com/users/Thynix","html_url":"https://github.com/Thynix","followers_url":"https://api.github.com/users/Thynix/followers","following_url":"https://api.github.com/users/Thynix/following{/other_user}","gists_url":"https://api.github.com/users/Thynix/gists{/gist_id}","starred_url":"https://api.github.com/users/Thynix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Thynix/subscriptions","organizations_url":"https://api.github.com/users/Thynix/orgs","repos_url":"https://api.github.com/users/Thynix/repos","events_url":"https://api.github.com/users/Thynix/events{/privacy}","received_events_url":"https://api.github.com/users/Thynix/received_events","type":"User","site_admin":false},"body":"Oh! Okay.","created_at":"2015-03-25T04:25:50Z","updated_at":"2015-03-25T04:25:50Z","html_url":"https://github.com/freenet/fred/pull/258#discussion_r27094354","pull_request_url":"https://api.github.com/repos/freenet/fred/pulls/258","_links":{"self":{"href":"https://api.github.com/repos/freenet/fred/pulls/comments/27094354"},"html":{"href":"https://github.com/freenet/fred/pull/258#discussion_r27094354"},"pull_request":{"href":"https://api.github.com/repos/freenet/fred/pulls/258"}}},"pull_request":{"url":"https://api.github.com/repos/freenet/fred/pulls/258","id":18120198,"html_url":"https://github.com/freenet/fred/pull/258","diff_url":"https://github.com/freenet/fred/pull/258.diff","patch_url":"https://github.com/freenet/fred/pull/258.patch","issue_url":"https://api.github.com/repos/freenet/fred/issues/258","number":258,"state":"open","locked":false,"title":"Hashing API","user":{"login":"unixninja92","id":3614212,"avatar_url":"https://avatars.githubusercontent.com/u/3614212?v=3","gravatar_id":"","url":"https://api.github.com/users/unixninja92","html_url":"https://github.com/unixninja92","followers_url":"https://api.github.com/users/unixninja92/followers","following_url":"https://api.github.com/users/unixninja92/following{/other_user}","gists_url":"https://api.github.com/users/unixninja92/gists{/gist_id}","starred_url":"https://api.github.com/users/unixninja92/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/unixninja92/subscriptions","organizations_url":"https://api.github.com/users/unixninja92/orgs","repos_url":"https://api.github.com/users/unixninja92/repos","events_url":"https://api.github.com/users/unixninja92/events{/privacy}","received_events_url":"https://api.github.com/users/unixninja92/received_events","type":"User","site_admin":false},"body":"Here is the Hashing API I have written as part of my GSoC project. Also added a  check of policy files for JCE in JCELoader. ","created_at":"2014-07-08T18:35:36Z","updated_at":"2015-03-25T04:25:50Z","closed_at":null,"merged_at":null,"merge_commit_sha":"4d9ddbe56f07d6ad7d0635c9b83fa29ffa891402","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/freenet/fred/pulls/258/commits","review_comments_url":"https://api.github.com/repos/freenet/fred/pulls/258/comments","review_comment_url":"https://api.github.com/repos/freenet/fred/pulls/comments{/number}","comments_url":"https://api.github.com/repos/freenet/fred/issues/258/comments","statuses_url":"https://api.github.com/repos/freenet/fred/statuses/d7031ea233eaadfd2dd95180e8af66bef08e025b","head":{"label":"unixninja92:HashingAPI","ref":"HashingAPI","sha":"d7031ea233eaadfd2dd95180e8af66bef08e025b","user":{"login":"unixninja92","id":3614212,"avatar_url":"https://avatars.githubusercontent.com/u/3614212?v=3","gravatar_id":"","url":"https://api.github.com/users/unixninja92","html_url":"https://github.com/unixninja92","followers_url":"https://api.github.com/users/unixninja92/followers","following_url":"https://api.github.com/users/unixninja92/following{/other_user}","gists_url":"https://api.github.com/users/unixninja92/gists{/gist_id}","starred_url":"https://api.github.com/users/unixninja92/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/unixninja92/subscriptions","organizations_url":"https://api.github.com/users/unixninja92/orgs","repos_url":"https://api.github.com/users/unixninja92/repos","events_url":"https://api.github.com/users/unixninja92/events{/privacy}","received_events_url":"https://api.github.com/users/unixninja92/received_events","type":"User","site_admin":false},"repo":{"id":20228585,"name":"fred-staging","full_name":"unixninja92/fred-staging","owner":{"login":"unixninja92","id":3614212,"avatar_url":"https://avatars.githubusercontent.com/u/3614212?v=3","gravatar_id":"","url":"https://api.github.com/users/unixninja92","html_url":"https://github.com/unixninja92","followers_url":"https://api.github.com/users/unixninja92/followers","following_url":"https://api.github.com/users/unixninja92/following{/other_user}","gists_url":"https://api.github.com/users/unixninja92/gists{/gist_id}","starred_url":"https://api.github.com/users/unixninja92/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/unixninja92/subscriptions","organizations_url":"https://api.github.com/users/unixninja92/orgs","repos_url":"https://api.github.com/users/unixninja92/repos","events_url":"https://api.github.com/users/unixninja92/events{/privacy}","received_events_url":"https://api.github.com/users/unixninja92/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/unixninja92/fred-staging","description":"Freenet REference Daemon (trunk, writable by all devs)","fork":true,"url":"https://api.github.com/repos/unixninja92/fred-staging","forks_url":"https://api.github.com/repos/unixninja92/fred-staging/forks","keys_url":"https://api.github.com/repos/unixninja92/fred-staging/keys{/key_id}","collaborators_url":"https://api.github.com/repos/unixninja92/fred-staging/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/unixninja92/fred-staging/teams","hooks_url":"https://api.github.com/repos/unixninja92/fred-staging/hooks","issue_events_url":"https://api.github.com/repos/unixninja92/fred-staging/issues/events{/number}","events_url":"https://api.github.com/repos/unixninja92/fred-staging/events","assignees_url":"https://api.github.com/repos/unixninja92/fred-staging/assignees{/user}","branches_url":"https://api.github.com/repos/unixninja92/fred-staging/branches{/branch}","tags_url":"https://api.github.com/repos/unixninja92/fred-staging/tags","blobs_url":"https://api.github.com/repos/unixninja92/fred-staging/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/unixninja92/fred-staging/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/unixninja92/fred-staging/git/refs{/sha}","trees_url":"https://api.github.com/repos/unixninja92/fred-staging/git/trees{/sha}","statuses_url":"https://api.github.com/repos/unixninja92/fred-staging/statuses/{sha}","languages_url":"https://api.github.com/repos/unixninja92/fred-staging/languages","stargazers_url":"https://api.github.com/repos/unixninja92/fred-staging/stargazers","contributors_url":"https://api.github.com/repos/unixninja92/fred-staging/contributors","subscribers_url":"https://api.github.com/repos/unixninja92/fred-staging/subscribers","subscription_url":"https://api.github.com/repos/unixninja92/fred-staging/subscription","commits_url":"https://api.github.com/repos/unixninja92/fred-staging/commits{/sha}","git_commits_url":"https://api.github.com/repos/unixninja92/fred-staging/git/commits{/sha}","comments_url":"https://api.github.com/repos/unixninja92/fred-staging/comments{/number}","issue_comment_url":"https://api.github.com/repos/unixninja92/fred-staging/issues/comments{/number}","contents_url":"https://api.github.com/repos/unixninja92/fred-staging/contents/{+path}","compare_url":"https://api.github.com/repos/unixninja92/fred-staging/compare/{base}...{head}","merges_url":"https://api.github.com/repos/unixninja92/fred-staging/merges","archive_url":"https://api.github.com/repos/unixninja92/fred-staging/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/unixninja92/fred-staging/downloads","issues_url":"https://api.github.com/repos/unixninja92/fred-staging/issues{/number}","pulls_url":"https://api.github.com/repos/unixninja92/fred-staging/pulls{/number}","milestones_url":"https://api.github.com/repos/unixninja92/fred-staging/milestones{/number}","notifications_url":"https://api.github.com/repos/unixninja92/fred-staging/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/unixninja92/fred-staging/labels{/name}","releases_url":"https://api.github.com/repos/unixninja92/fred-staging/releases{/id}","created_at":"2014-05-27T17:21:46Z","updated_at":"2014-07-20T15:29:46Z","pushed_at":"2015-03-22T23:06:51Z","git_url":"git://github.com/unixninja92/fred-staging.git","ssh_url":"git@github.com:unixninja92/fred-staging.git","clone_url":"https://github.com/unixninja92/fred-staging.git","svn_url":"https://github.com/unixninja92/fred-staging","homepage":"http://freenetproject.org/","size":47608,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"next"}},"base":{"label":"freenet:next","ref":"next","sha":"9e580dee4e1cf5815181086b3a399bbf59ec9553","user":{"login":"freenet","id":77513,"avatar_url":"https://avatars.githubusercontent.com/u/77513?v=3","gravatar_id":"","url":"https://api.github.com/users/freenet","html_url":"https://github.com/freenet","followers_url":"https://api.github.com/users/freenet/followers","following_url":"https://api.github.com/users/freenet/following{/other_user}","gists_url":"https://api.github.com/users/freenet/gists{/gist_id}","starred_url":"https://api.github.com/users/freenet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/freenet/subscriptions","organizations_url":"https://api.github.com/users/freenet/orgs","repos_url":"https://api.github.com/users/freenet/repos","events_url":"https://api.github.com/users/freenet/events{/privacy}","received_events_url":"https://api.github.com/users/freenet/received_events","type":"Organization","site_admin":false},"repo":{"id":185026,"name":"fred","full_name":"freenet/fred","owner":{"login":"freenet","id":77513,"avatar_url":"https://avatars.githubusercontent.com/u/77513?v=3","gravatar_id":"","url":"https://api.github.com/users/freenet","html_url":"https://github.com/freenet","followers_url":"https://api.github.com/users/freenet/followers","following_url":"https://api.github.com/users/freenet/following{/other_user}","gists_url":"https://api.github.com/users/freenet/gists{/gist_id}","starred_url":"https://api.github.com/users/freenet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/freenet/subscriptions","organizations_url":"https://api.github.com/users/freenet/orgs","repos_url":"https://api.github.com/users/freenet/repos","events_url":"https://api.github.com/users/freenet/events{/privacy}","received_events_url":"https://api.github.com/users/freenet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/freenet/fred","description":"Freenet REference Daemon","fork":false,"url":"https://api.github.com/repos/freenet/fred","forks_url":"https://api.github.com/repos/freenet/fred/forks","keys_url":"https://api.github.com/repos/freenet/fred/keys{/key_id}","collaborators_url":"https://api.github.com/repos/freenet/fred/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/freenet/fred/teams","hooks_url":"https://api.github.com/repos/freenet/fred/hooks","issue_events_url":"https://api.github.com/repos/freenet/fred/issues/events{/number}","events_url":"https://api.github.com/repos/freenet/fred/events","assignees_url":"https://api.github.com/repos/freenet/fred/assignees{/user}","branches_url":"https://api.github.com/repos/freenet/fred/branches{/branch}","tags_url":"https://api.github.com/repos/freenet/fred/tags","blobs_url":"https://api.github.com/repos/freenet/fred/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/freenet/fred/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/freenet/fred/git/refs{/sha}","trees_url":"https://api.github.com/repos/freenet/fred/git/trees{/sha}","statuses_url":"https://api.github.com/repos/freenet/fred/statuses/{sha}","languages_url":"https://api.github.com/repos/freenet/fred/languages","stargazers_url":"https://api.github.com/repos/freenet/fred/stargazers","contributors_url":"https://api.github.com/repos/freenet/fred/contributors","subscribers_url":"https://api.github.com/repos/freenet/fred/subscribers","subscription_url":"https://api.github.com/repos/freenet/fred/subscription","commits_url":"https://api.github.com/repos/freenet/fred/commits{/sha}","git_commits_url":"https://api.github.com/repos/freenet/fred/git/commits{/sha}","comments_url":"https://api.github.com/repos/freenet/fred/comments{/number}","issue_comment_url":"https://api.github.com/repos/freenet/fred/issues/comments{/number}","contents_url":"https://api.github.com/repos/freenet/fred/contents/{+path}","compare_url":"https://api.github.com/repos/freenet/fred/compare/{base}...{head}","merges_url":"https://api.github.com/repos/freenet/fred/merges","archive_url":"https://api.github.com/repos/freenet/fred/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/freenet/fred/downloads","issues_url":"https://api.github.com/repos/freenet/fred/issues{/number}","pulls_url":"https://api.github.com/repos/freenet/fred/pulls{/number}","milestones_url":"https://api.github.com/repos/freenet/fred/milestones{/number}","notifications_url":"https://api.github.com/repos/freenet/fred/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/freenet/fred/labels{/name}","releases_url":"https://api.github.com/repos/freenet/fred/releases{/id}","created_at":"2009-04-25T01:00:27Z","updated_at":"2015-03-25T00:26:16Z","pushed_at":"2015-03-22T22:54:44Z","git_url":"git://github.com/freenet/fred.git","ssh_url":"git@github.com:freenet/fred.git","clone_url":"https://github.com/freenet/fred.git","svn_url":"https://github.com/freenet/fred","homepage":"https://freenetproject.org/","size":151248,"stargazers_count":160,"watchers_count":160,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":80,"mirror_url":null,"open_issues_count":21,"forks":80,"open_issues":21,"watchers":160,"default_branch":"next"}},"_links":{"self":{"href":"https://api.github.com/repos/freenet/fred/pulls/258"},"html":{"href":"https://github.com/freenet/fred/pull/258"},"issue":{"href":"https://api.github.com/repos/freenet/fred/issues/258"},"comments":{"href":"https://api.github.com/repos/freenet/fred/issues/258/comments"},"review_comments":{"href":"https://api.github.com/repos/freenet/fred/pulls/258/comments"},"review_comment":{"href":"https://api.github.com/repos/freenet/fred/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/freenet/fred/pulls/258/commits"},"statuses":{"href":"https://api.github.com/repos/freenet/fred/statuses/d7031ea233eaadfd2dd95180e8af66bef08e025b"}}}}