Converted to kotlin

This commit is contained in:
Robinson 2021-04-26 14:43:33 +02:00
parent c8e9fbfa12
commit d619ee2d53
6 changed files with 105 additions and 148 deletions

View File

@ -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<? extends EntropyProvider> 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() {
}
}

View File

@ -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<out EntropyProvider>, 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)
}
}
}
}
}

View File

@ -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
}

View File

@ -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 operator fun get(messageForUser: String): ByteArray {
val rand = ByteArray(256)
synchronized(secureRandom) {
secureRandom.nextBytes(rand)
}
@Override
public
byte[] get(String ignored) throws Exception {
SecureRandom secureRandom = new SecureRandom();
byte[] rand = new byte[256];
secureRandom.nextBytes(rand);
return rand;
return rand
}
}

View File

@ -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)
}

View File

@ -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)
}