summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/util')
-rw-r--r--src/main/java/org/openslx/imagemaster/util/Hash.java26
-rw-r--r--src/main/java/org/openslx/imagemaster/util/RandomString.java22
-rw-r--r--src/main/java/org/openslx/imagemaster/util/Util.java33
3 files changed, 44 insertions, 37 deletions
diff --git a/src/main/java/org/openslx/imagemaster/util/Hash.java b/src/main/java/org/openslx/imagemaster/util/Hash.java
index 24eb595..8db6a5f 100644
--- a/src/main/java/org/openslx/imagemaster/util/Hash.java
+++ b/src/main/java/org/openslx/imagemaster/util/Hash.java
@@ -10,6 +10,7 @@ public class Hash
* Cache of md5 digesters
*/
private static final ThreadLocal<MessageDigest> md5hash = new ThreadLocal<MessageDigest>() {
+
@Override
public MessageDigest initialValue()
{
@@ -17,7 +18,7 @@ public class Hash
return MessageDigest.getInstance( "MD5" );
} catch ( NoSuchAlgorithmException e ) {
e.printStackTrace();
- System.exit(1);
+ System.exit( 1 );
return null;
}
}
@@ -26,6 +27,7 @@ public class Hash
* Cache of sha256 digesters
*/
private static final ThreadLocal<MessageDigest> sha256hash = new ThreadLocal<MessageDigest>() {
+
@Override
public MessageDigest initialValue()
{
@@ -33,7 +35,7 @@ public class Hash
return MessageDigest.getInstance( "SHA-256" );
} catch ( NoSuchAlgorithmException e ) {
e.printStackTrace();
- System.exit(1);
+ System.exit( 1 );
return null;
}
}
@@ -48,7 +50,7 @@ public class Hash
private static final Charset UTF8 = Charset.forName( "UTF-8" );
// MD5
-
+
/**
* Compute md5 hash of given binary data.
*
@@ -59,7 +61,7 @@ public class Hash
{
return toHexString( md5hash.get().digest( bytes ) );
}
-
+
/**
* Compute md5 hash of the given string.
* The string will be converted to utf-8 before computation.
@@ -69,11 +71,11 @@ public class Hash
*/
public static String md5( final String text )
{
- return md5( text.getBytes( UTF8 ));
+ return md5( text.getBytes( UTF8 ) );
}
-
+
// SHA-256
-
+
/**
* Compute sha256 hash of given binary data.
*
@@ -94,20 +96,20 @@ public class Hash
*/
public static String sha256( final String text )
{
- return sha256( text.getBytes( UTF8 ));
+ return sha256( text.getBytes( UTF8 ) );
}
-
+
// Helper
-
+
/**
* Convert given binary data to hex.
*
* @param bytes binary data in a byte array
- * @return upper case hex representation of bytes
+ * @return upper case hex representation of bytes
*/
private static String toHexString( final byte[] bytes )
{
- final char[] hexChars = new char[bytes.length * 2];
+ final char[] hexChars = new char[ bytes.length * 2 ];
for ( int j = 0; j < bytes.length; ++j ) {
final int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_CHARS[v >>> 4];
diff --git a/src/main/java/org/openslx/imagemaster/util/RandomString.java b/src/main/java/org/openslx/imagemaster/util/RandomString.java
index a0e9419..fc4f9d3 100644
--- a/src/main/java/org/openslx/imagemaster/util/RandomString.java
+++ b/src/main/java/org/openslx/imagemaster/util/RandomString.java
@@ -4,25 +4,27 @@ import java.security.SecureRandom;
/**
* Generate secure random strings
- * @author nils
- *
+ *
*/
-public class RandomString {
- private static final String lettersSpecial="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-$%&/()=?@";
- private static final String letters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
+public class RandomString
+{
+ private static final String lettersSpecial = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890+-$%&/()=?@";
+ private static final String letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
private static final SecureRandom random = new SecureRandom();
-
+
/**
* Generate a random string.
+ *
* @param length the length of the string
* @param specialChars whether to use special charachters or not
* @return the generated string
*/
- public static String generate(int length, boolean specialChars) {
- String used = (specialChars)? lettersSpecial : letters;
+ public static String generate( int length, boolean specialChars )
+ {
+ String used = ( specialChars ) ? lettersSpecial : letters;
String result = "";
- for (int i = 0; i < length; i++) {
- int index = (int)(random.nextDouble()*used.length());
+ for ( int i = 0; i < length; i++ ) {
+ int index = (int) ( random.nextDouble() * used.length() );
result += used.substring( index, index + 1 );
}
return result;
diff --git a/src/main/java/org/openslx/imagemaster/util/Util.java b/src/main/java/org/openslx/imagemaster/util/Util.java
index 9e0f708..0df4212 100644
--- a/src/main/java/org/openslx/imagemaster/util/Util.java
+++ b/src/main/java/org/openslx/imagemaster/util/Util.java
@@ -7,6 +7,7 @@ import org.apache.log4j.Logger;
public class Util
{
+
private static Logger log = Logger.getLogger( Util.class );
/**
@@ -31,12 +32,12 @@ public class Util
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},
@@ -49,23 +50,25 @@ public class Util
{
return random.nextInt( n );
}
-
+
/**
* Remove a folder and all contents
+ *
* @param folder
*/
- public static void deleteFolder(File folder) {
- File[] files = folder.listFiles();
- if (files!=null) {
- for (File f: files) {
- if (f.isDirectory()) {
- deleteFolder(f);
- } else {
- f.delete();
- }
- }
- }
- folder.delete();
+ public static void deleteFolder( File folder )
+ {
+ File[] files = folder.listFiles();
+ if ( files != null ) {
+ for ( File f : files ) {
+ if ( f.isDirectory() ) {
+ deleteFolder( f );
+ } else {
+ f.delete();
+ }
+ }
+ }
+ folder.delete();
}
}