From 5fbff0086bbfec254865cdf8833f7a4ad649c104 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 30 Dec 2016 03:29:45 +1300 Subject: Init crypto on statup (fix #2422) --- src/org/traccar/helper/Hashing.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/helper/Hashing.java b/src/org/traccar/helper/Hashing.java index 55086bac7..47d263fca 100644 --- a/src/org/traccar/helper/Hashing.java +++ b/src/org/traccar/helper/Hashing.java @@ -19,7 +19,9 @@ import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.xml.bind.DatatypeConverter; import java.security.GeneralSecurityException; +import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.security.spec.InvalidKeySpecException; public final class Hashing { @@ -27,6 +29,14 @@ public final class Hashing { public static final int SALT_SIZE = 24; public static final int HASH_SIZE = 24; + private static SecretKeyFactory factory; + static { + try { + factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); + } catch (NoSuchAlgorithmException e) { + } + } + public static class HashingResult { private final String hash; @@ -52,10 +62,9 @@ public final class Hashing { private static byte[] function(char[] password, byte[] salt) { try { PBEKeySpec spec = new PBEKeySpec(password, salt, ITERATIONS, HASH_SIZE * Byte.SIZE); - SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); return factory.generateSecret(spec).getEncoded(); - } catch (GeneralSecurityException error) { - throw new SecurityException(error); + } catch (InvalidKeySpecException e) { + throw new SecurityException(e); } } -- cgit v1.2.3