-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'bertm/simplify-sha256' into next
- Loading branch information
Showing
22 changed files
with
204 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,27 +35,14 @@ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.ref.SoftReference; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.Provider; | ||
import java.util.Queue; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
import org.tanukisoftware.wrapper.WrapperManager; | ||
|
||
import freenet.node.Node; | ||
import freenet.node.NodeInitException; | ||
import freenet.support.Logger; | ||
import freenet.support.io.Closer; | ||
|
||
/** | ||
* @author Jeroen C. van Gelderen ([email protected]) | ||
*/ | ||
public class SHA256 { | ||
/** Size (in bytes) of this hash */ | ||
private static final int HASH_SIZE = 32; | ||
private static final Queue<SoftReference<MessageDigest>> digests = new ConcurrentLinkedQueue<>(); | ||
|
||
/** | ||
* It won't reset the Message Digest for you! | ||
|
@@ -78,57 +65,27 @@ public static void hash(InputStream is, MessageDigest md) throws IOException { | |
} | ||
} | ||
|
||
private static final Provider mdProvider = Util.mdProviders.get("SHA-256"); | ||
|
||
/** | ||
* Create a new SHA-256 MessageDigest | ||
* Either succeed or stop the node. | ||
*/ | ||
public static MessageDigest getMessageDigest() { | ||
try { | ||
SoftReference<MessageDigest> item = null; | ||
while (((item = digests.poll()) != null)) { | ||
MessageDigest md = item.get(); | ||
if (md != null) { | ||
return md; | ||
} | ||
} | ||
return MessageDigest.getInstance("SHA-256", mdProvider); | ||
} catch(NoSuchAlgorithmException e2) { | ||
//TODO: maybe we should point to a HOWTO for freejvms | ||
Logger.error(Node.class, "Check your JVM settings especially the JCE!" + e2); | ||
System.err.println("Check your JVM settings especially the JCE!" + e2); | ||
e2.printStackTrace(); | ||
} | ||
WrapperManager.stop(NodeInitException.EXIT_CRAPPY_JVM); | ||
throw new RuntimeException(); | ||
return HashType.SHA256.get(); | ||
} | ||
|
||
/** | ||
* Return a MessageDigest to the pool. | ||
* Must be SHA-256 ! | ||
* No-op function retained for backwards compatibility. | ||
* | ||
* @deprecated message digests are no longer pooled, there is no need to return them | ||
*/ | ||
@Deprecated | ||
public static void returnMessageDigest(MessageDigest md256) { | ||
if(md256 == null) | ||
return; | ||
String algo = md256.getAlgorithm(); | ||
if(!(algo.equals("SHA-256") || algo.equals("SHA256"))) | ||
throw new IllegalArgumentException("Should be SHA-256 but is " + algo); | ||
md256.reset(); | ||
digests.add(new SoftReference<>(md256)); | ||
} | ||
|
||
public static byte[] digest(byte[] data) { | ||
MessageDigest md = null; | ||
try { | ||
md = getMessageDigest(); | ||
return md.digest(data); | ||
} finally { | ||
returnMessageDigest(md); | ||
} | ||
return getMessageDigest().digest(data); | ||
} | ||
|
||
public static int getDigestLength() { | ||
return HASH_SIZE; | ||
return HashType.SHA256.hashLength; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.