Converted to kotlin
This commit is contained in:
parent
c8e9fbfa12
commit
d619ee2d53
@ -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() {
|
|
||||||
}
|
|
||||||
}
|
|
63
src/dorkbox/util/entropy/Entropy.kt
Normal file
63
src/dorkbox/util/entropy/Entropy.kt
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -13,10 +13,10 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package dorkbox.util.entropy;
|
package dorkbox.util.entropy
|
||||||
|
|
||||||
public
|
|
||||||
interface EntropyProvider {
|
interface EntropyProvider {
|
||||||
|
|
||||||
byte[] get(String messageForUser) throws Exception;
|
@Throws(Exception::class)
|
||||||
|
fun get(messageForUser: String): ByteArray
|
||||||
}
|
}
|
@ -13,24 +13,18 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package dorkbox.util.entropy;
|
package dorkbox.util.entropy
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom
|
||||||
|
|
||||||
public
|
class SimpleEntropy : EntropyProvider {
|
||||||
class SimpleEntropy implements EntropyProvider {
|
private val secureRandom = SecureRandom()
|
||||||
|
|
||||||
public static
|
override operator fun get(messageForUser: String): ByteArray {
|
||||||
Object create() {
|
val rand = ByteArray(256)
|
||||||
return new SimpleEntropy();
|
synchronized(secureRandom) {
|
||||||
|
secureRandom.nextBytes(rand)
|
||||||
}
|
}
|
||||||
|
return rand
|
||||||
@Override
|
|
||||||
public
|
|
||||||
byte[] get(String ignored) throws Exception {
|
|
||||||
SecureRandom secureRandom = new SecureRandom();
|
|
||||||
byte[] rand = new byte[256];
|
|
||||||
secureRandom.nextBytes(rand);
|
|
||||||
return rand;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -13,30 +13,11 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package dorkbox.util.exceptions;
|
package dorkbox.util.exceptions
|
||||||
|
|
||||||
public
|
class InitializationException : Exception {
|
||||||
class InitializationException extends Exception {
|
constructor() : super()
|
||||||
|
constructor(message: String, cause: Throwable) : super(message, cause)
|
||||||
private static final long serialVersionUID = 3331031046821855954L;
|
constructor(message: String) : super(message)
|
||||||
|
constructor(cause: Throwable) : super(cause)
|
||||||
public
|
|
||||||
InitializationException() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public
|
|
||||||
InitializationException(String message, Throwable cause) {
|
|
||||||
super(message, cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
public
|
|
||||||
InitializationException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public
|
|
||||||
InitializationException(Throwable cause) {
|
|
||||||
super(cause);
|
|
||||||
}
|
|
||||||
}
|
}
|
23
src/dorkbox/util/exceptions/SecurityException.kt
Normal file
23
src/dorkbox/util/exceptions/SecurityException.kt
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user