diff options
author | Simon Rettberg | 2014-04-19 16:27:36 +0200 |
---|---|---|
committer | Simon Rettberg | 2014-04-19 16:27:36 +0200 |
commit | 2bfbda7280809fff920f5692b4faf84e4873da7f (patch) | |
tree | d71e9f96eae09ab35f1e6d936f698a218ef8bf3c | |
parent | [LdapUser] Added doc-comments, some TODOs, restructured connection error chec... (diff) | |
download | masterserver-2bfbda7280809fff920f5692b4faf84e4873da7f.tar.gz masterserver-2bfbda7280809fff920f5692b4faf84e4873da7f.tar.xz masterserver-2bfbda7280809fff920f5692b4faf84e4873da7f.zip |
Speed up sha512_crypt by caching sha512 digesters (thread local)
-rw-r--r-- | src/main/java/org/openslx/imagemaster/util/Sha512Crypt.java | 47 | ||||
-rw-r--r-- | src/main/java/org/openslx/imagemaster/util/Util.java | 40 |
2 files changed, 69 insertions, 18 deletions
diff --git a/src/main/java/org/openslx/imagemaster/util/Sha512Crypt.java b/src/main/java/org/openslx/imagemaster/util/Sha512Crypt.java index 472ead9..15cf9ac 100644 --- a/src/main/java/org/openslx/imagemaster/util/Sha512Crypt.java +++ b/src/main/java/org/openslx/imagemaster/util/Sha512Crypt.java @@ -44,6 +44,7 @@ package org.openslx.imagemaster.util; import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; /*------------------------------------------------------------------------------ class @@ -87,18 +88,33 @@ public final class Sha512Crypt static private final int ROUNDS_MAX = 999999999; static private final String SALTCHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; static private final String itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - - static private MessageDigest getSHA512() - { - try - { - return MessageDigest.getInstance("SHA-512"); - } - catch (java.security.NoSuchAlgorithmException ex) - { - throw new RuntimeException(ex); - } - } + + /** + * Cache of sha512 digesters + */ + private static final ThreadLocal<MessageDigest> + sha1 = newThreadLocalSha512(), + sha2 = newThreadLocalSha512(); + + /** + * For instantiating TL sha512, so we don't have to copy this stuff twice + * @return thread local sha512 digester + */ + private static ThreadLocal<MessageDigest> newThreadLocalSha512() { + return new ThreadLocal<MessageDigest>() { + @Override + public MessageDigest initialValue() + { + try { + return MessageDigest.getInstance( "SHA-512" ); + } catch ( NoSuchAlgorithmException e ) { + e.printStackTrace(); + System.exit(1); + return null; + } + } + }; + } /** * <p>This method actually generates an Sha512 crypted password hash @@ -120,8 +136,8 @@ public final class Sha512Crypt public static final String Sha512_crypt(String keyStr, String saltStr, int roundsCount) { - MessageDigest ctx = getSHA512(); - MessageDigest alt_ctx = getSHA512(); + MessageDigest ctx = sha1.get(); + MessageDigest alt_ctx = sha2.get(); byte[] alt_result; byte[] temp_result; @@ -172,12 +188,11 @@ public final class Sha512Crypt } else { - java.util.Random randgen = new java.util.Random(); StringBuilder saltBuf = new StringBuilder(); while (saltBuf.length() < 16) { - int index = (int) (randgen.nextFloat() * SALTCHARS.length()); + int index = Util.randomInt(SALTCHARS.length()); saltBuf.append(SALTCHARS.substring(index, index+1)); } diff --git a/src/main/java/org/openslx/imagemaster/util/Util.java b/src/main/java/org/openslx/imagemaster/util/Util.java index ff0b8c1..4459a3a 100644 --- a/src/main/java/org/openslx/imagemaster/util/Util.java +++ b/src/main/java/org/openslx/imagemaster/util/Util.java @@ -1,16 +1,52 @@ package org.openslx.imagemaster.util; +import java.util.Random; + +import org.apache.log4j.Logger; + public class Util { + private static Logger log = Logger.getLogger( Util.class ); + /** + * Check if the given object is null, abort program if true. + * An optional message to be printed can be passed. A stack trace + * will be printed, too. Finally the application terminates with + * exit code 2. + * + * This comes in handy if something must not be null, and you want + * user friendsly output. A perfect example would be reading settings + * from a config file. You can use this on mandatory fields. + * + * @param something the object to compare to null + * @param message the message to be printed if something is null + */ public static void notNullFatal( Object something, String message ) { if ( something == null ) { if ( message != null ) - System.out.println( "[NOTNULL] " + message ); - System.out.println( Thread.currentThread().getStackTrace().toString() ); + log.fatal( "[NOTNULL] " + message ); + log.warn( Thread.currentThread().getStackTrace().toString() ); System.exit( 2 ); } } + + /** + * Static {@link Random} instance. + */ + private static final Random random = new Random(); + + /** + * Return a random integer in the range of 0 (inclusive) and + * n (exclusive). Uses the internal static instance of {@link Random}, + * so you don't have to deal with instances everywhere. + * + * @param n the upper bound (exclusive) + * @return number between 0 (inclusive) and n (exclusive) + */ + public static int randomInt( int n ) + { + return random.nextInt( n ); + } } |