{"action":"created","issue":{"url":"https://api.github.com/repos/wg/scrypt/issues/15","labels_url":"https://api.github.com/repos/wg/scrypt/issues/15/labels{/name}","comments_url":"https://api.github.com/repos/wg/scrypt/issues/15/comments","events_url":"https://api.github.com/repos/wg/scrypt/issues/15/events","html_url":"https://github.com/wg/scrypt/issues/15","id":25232279,"number":15,"title":"Simply \"clone\" the Mac for SHA256 Hashing","user":{"login":"pfumagalli","id":366777,"avatar_url":"https://avatars.githubusercontent.com/u/366777?v=3","gravatar_id":"","url":"https://api.github.com/users/pfumagalli","html_url":"https://github.com/pfumagalli","followers_url":"https://api.github.com/users/pfumagalli/followers","following_url":"https://api.github.com/users/pfumagalli/following{/other_user}","gists_url":"https://api.github.com/users/pfumagalli/gists{/gist_id}","starred_url":"https://api.github.com/users/pfumagalli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pfumagalli/subscriptions","organizations_url":"https://api.github.com/users/pfumagalli/orgs","repos_url":"https://api.github.com/users/pfumagalli/repos","events_url":"https://api.github.com/users/pfumagalli/events{/privacy}","received_events_url":"https://api.github.com/users/pfumagalli/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2014-01-08T10:57:15Z","updated_at":"2015-09-14T18:08:05Z","closed_at":null,"body":"Hey Will,\r\n\r\nOn \"Scrypt.java\" you're calling every time:\r\n\r\n```\r\npublic static byte[] scryptJ(byte[] passwd, byte[] salt, ...) {\r\n    ....\r\n    Mac mac = Mac.getInstance(\"HmacSHA256\");\r\n    ...\r\n}\r\n```\r\n\r\nFor whatever reason (bad karma on JSSE?) that call roughly takes 50% of the entire SCrypt calculation. Initialise the Mac statically and then call `.clone()` on it, you get double the performance (according to JProfiler).\r\n\r\n```\r\nprivate static final Mac SHARED_MAC = Mac.getInstance(\"HmacSHA256\");\r\npublic static byte[] scryptJ(byte[] passwd, byte[] salt, ...) {\r\n    ....\r\n    Mac mac = (Mac) SHARED_MAC.clone();\r\n    ...\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/wg/scrypt/issues/comments/140162791","html_url":"https://github.com/wg/scrypt/issues/15#issuecomment-140162791","issue_url":"https://api.github.com/repos/wg/scrypt/issues/15","id":140162791,"user":{"login":"rlubbat","id":1848408,"avatar_url":"https://avatars.githubusercontent.com/u/1848408?v=3","gravatar_id":"","url":"https://api.github.com/users/rlubbat","html_url":"https://github.com/rlubbat","followers_url":"https://api.github.com/users/rlubbat/followers","following_url":"https://api.github.com/users/rlubbat/following{/other_user}","gists_url":"https://api.github.com/users/rlubbat/gists{/gist_id}","starred_url":"https://api.github.com/users/rlubbat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rlubbat/subscriptions","organizations_url":"https://api.github.com/users/rlubbat/orgs","repos_url":"https://api.github.com/users/rlubbat/repos","events_url":"https://api.github.com/users/rlubbat/events{/privacy}","received_events_url":"https://api.github.com/users/rlubbat/received_events","type":"User","site_admin":false},"created_at":"2015-09-14T18:08:05Z","updated_at":"2015-09-14T18:08:05Z","body":"Cloning may not work. It depends on the underlying JCE implementation. For example, we ran into this in a project that was using BouncyCastle. We had Mac.clone() and it blew up. If you want to clone the Mac for performance reasons, make sure you don't do it blindly. You will need to check if it is possible and proceed as appropriate. We ended up attempting the clone in the class constructor and used a boolean to indicate if cloning is possible. We check the boolean to decide to clone or get an instance in the code."}}