diff --git a/src/dorkbox/util/entropy/Entropy.java b/src/dorkbox/util/entropy/Entropy.java deleted file mode 100644 index 8063613..0000000 --- a/src/dorkbox/util/entropy/Entropy.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2010 dorkbox, llc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package dorkbox.util.entropy; - -import dorkbox.util.exceptions.InitializationException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Method; - -public final -class Entropy { - - @SuppressWarnings("StaticNonFinalField") - private static EntropyProvider provider = null; - - /** - * Starts the process, and gets, the next amount of entropy bytes - */ - public static - byte[] get(String messageForUser) throws InitializationException { - synchronized (Entropy.class) { - try { - if (provider == null) { - Entropy.init(SimpleEntropy.class); - } - - return provider.get(messageForUser); - } catch (Exception e) { - Logger logger = LoggerFactory.getLogger(Entropy.class); - String error = "Unable to get entropy bytes for " + provider.getClass(); - logger.error(error, e); - throw new InitializationException(error); - } - } - } - - /** - * Will only set the Entropy provider if it has not ALREADY been set! - */ - public static - void init(Class providerClass, Object... args) throws InitializationException { - synchronized (Entropy.class) { - if (provider == null) { - Exception exception = null; - - // use reflection to create the provider. - try { - Method createMethod = null; - Method[] declaredMethods = providerClass.getDeclaredMethods(); - for (Method m : declaredMethods) { - if (m.getName() - .equals("create")) { - createMethod = m; - break; - } - } - - if (createMethod != null) { - createMethod.setAccessible(true); - - if (args.length == 0) { - provider = (EntropyProvider) createMethod.invoke(null); - } - else { - provider = (EntropyProvider) createMethod.invoke(null, args); - } - return; - } - } catch (Exception e) { - exception = e; - } - - Logger logger = LoggerFactory.getLogger(Entropy.class); - String error = "Unable to create entropy provider for " + providerClass + " with " + args.length + " args"; - if (exception != null) { - logger.error(error, exception); - } - else { - logger.error(error); - } - - throw new InitializationException(error); - } - } - } - - private - Entropy() { - } -} diff --git a/src/dorkbox/util/entropy/Entropy.kt b/src/dorkbox/util/entropy/Entropy.kt new file mode 100644 index 0000000..483afd9 --- /dev/null +++ b/src/dorkbox/util/entropy/Entropy.kt @@ -0,0 +1,63 @@ +/* + * Copyright 2010 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dorkbox.util.entropy + +import dorkbox.util.exceptions.InitializationException +import org.slf4j.LoggerFactory + +object Entropy { + private var provider: EntropyProvider? = null + + /** + * Starts the process, and gets, the next amount of entropy bytes + */ + @Throws(InitializationException::class) + operator fun get(messageForUser: String): ByteArray { + init(SimpleEntropy::class.java) + + return try { + provider!!.get(messageForUser) + } catch (e: Exception) { + val logger = LoggerFactory.getLogger(Entropy::class.java) + val error = "Unable to get entropy bytes for " + provider!!.javaClass + logger.error(error, e) + throw InitializationException(error) + } + } + + /** + * Will only set the Entropy provider if it has not ALREADY been set! + */ + @Throws(InitializationException::class) + fun init(providerClass: Class, vararg args: Any) { + synchronized(Entropy::class.java) { + if (provider == null) { + try { + provider = if (args.isEmpty()) { + providerClass.getDeclaredConstructor().newInstance() + } else { + providerClass.getDeclaredConstructor().newInstance(args) + } + } catch (e: Exception) { + val logger = LoggerFactory.getLogger(Entropy::class.java) + val error = "Unable to create entropy provider for " + providerClass + " with " + args.size + " args" + logger.error(error, e) + throw InitializationException(error) + } + } + } + } +} diff --git a/src/dorkbox/util/entropy/EntropyProvider.java b/src/dorkbox/util/entropy/EntropyProvider.kt similarity index 85% rename from src/dorkbox/util/entropy/EntropyProvider.java rename to src/dorkbox/util/entropy/EntropyProvider.kt index a3108db..48a7f28 100644 --- a/src/dorkbox/util/entropy/EntropyProvider.java +++ b/src/dorkbox/util/entropy/EntropyProvider.kt @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package dorkbox.util.entropy; +package dorkbox.util.entropy -public interface EntropyProvider { - byte[] get(String messageForUser) throws Exception; + @Throws(Exception::class) + fun get(messageForUser: String): ByteArray } diff --git a/src/dorkbox/util/entropy/SimpleEntropy.java b/src/dorkbox/util/entropy/SimpleEntropy.kt similarity index 58% rename from src/dorkbox/util/entropy/SimpleEntropy.java rename to src/dorkbox/util/entropy/SimpleEntropy.kt index 573f919..ebadd2b 100644 --- a/src/dorkbox/util/entropy/SimpleEntropy.java +++ b/src/dorkbox/util/entropy/SimpleEntropy.kt @@ -13,24 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package dorkbox.util.entropy; +package dorkbox.util.entropy -import java.security.SecureRandom; +import java.security.SecureRandom -public -class SimpleEntropy implements EntropyProvider { +class SimpleEntropy : EntropyProvider { + private val secureRandom = SecureRandom() - public static - Object create() { - return new SimpleEntropy(); - } - - @Override - public - byte[] get(String ignored) throws Exception { - SecureRandom secureRandom = new SecureRandom(); - byte[] rand = new byte[256]; - secureRandom.nextBytes(rand); - return rand; + override operator fun get(messageForUser: String): ByteArray { + val rand = ByteArray(256) + synchronized(secureRandom) { + secureRandom.nextBytes(rand) + } + return rand } } diff --git a/src/dorkbox/util/exceptions/InitializationException.java b/src/dorkbox/util/exceptions/InitializationException.kt similarity index 53% rename from src/dorkbox/util/exceptions/InitializationException.java rename to src/dorkbox/util/exceptions/InitializationException.kt index a09cf63..d7d329e 100644 --- a/src/dorkbox/util/exceptions/InitializationException.java +++ b/src/dorkbox/util/exceptions/InitializationException.kt @@ -13,30 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package dorkbox.util.exceptions; +package dorkbox.util.exceptions -public -class InitializationException extends Exception { - - private static final long serialVersionUID = 3331031046821855954L; - - public - InitializationException() { - super(); - } - - public - InitializationException(String message, Throwable cause) { - super(message, cause); - } - - public - InitializationException(String message) { - super(message); - } - - public - InitializationException(Throwable cause) { - super(cause); - } +class InitializationException : Exception { + constructor() : super() + constructor(message: String, cause: Throwable) : super(message, cause) + constructor(message: String) : super(message) + constructor(cause: Throwable) : super(cause) } diff --git a/src/dorkbox/util/exceptions/SecurityException.kt b/src/dorkbox/util/exceptions/SecurityException.kt new file mode 100644 index 0000000..66068d7 --- /dev/null +++ b/src/dorkbox/util/exceptions/SecurityException.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2010 dorkbox, llc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dorkbox.util.exceptions + +class SecurityException : Exception { + constructor() : super() + constructor(message: String, cause: Throwable) : super(message, cause) + constructor(message: String) : super(message) + constructor(cause: Throwable) : super(cause) +}