code polish/cleanup
This commit is contained in:
parent
5aad770f0d
commit
5b8cbadb84
@ -54,7 +54,6 @@
|
|||||||
<orderEntry type="library" name="kryo3" level="application" />
|
<orderEntry type="library" name="kryo3" level="application" />
|
||||||
<orderEntry type="library" name="jansi (1.11b)" level="application" />
|
<orderEntry type="library" name="jansi (1.11b)" level="application" />
|
||||||
<orderEntry type="library" name="netty-all (4.1.0)" level="application" />
|
<orderEntry type="library" name="netty-all (4.1.0)" level="application" />
|
||||||
<orderEntry type="library" name="JCTools (1.4 alpha MTAQ)" level="application" />
|
|
||||||
<orderEntry type="library" name="controlsfx" level="application" />
|
<orderEntry type="library" name="controlsfx" level="application" />
|
||||||
<orderEntry type="library" name="junit-4.12" level="application" />
|
<orderEntry type="library" name="junit-4.12" level="application" />
|
||||||
<orderEntry type="library" name="bouncyCastle bcpg-jdk15on (1.53)" level="application" />
|
<orderEntry type="library" name="bouncyCastle bcpg-jdk15on (1.53)" level="application" />
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package dorkbox.util.bytes;
|
package dorkbox.util.bytes;
|
||||||
|
|
||||||
import dorkbox.util.objectPool.PoolableObject;
|
import dorkbox.objectPool.PoolableObject;
|
||||||
|
|
||||||
public
|
public
|
||||||
class ByteBuffer2Poolable extends PoolableObject<ByteBuffer2> {
|
class ByteBuffer2Poolable extends PoolableObject<ByteBuffer2> {
|
||||||
|
@ -34,38 +34,34 @@
|
|||||||
*/
|
*/
|
||||||
package dorkbox.util.bytes;
|
package dorkbox.util.bytes;
|
||||||
|
|
||||||
public class OptimizeUtilsByteArray {
|
public
|
||||||
|
class OptimizeUtilsByteArray {
|
||||||
|
|
||||||
private static final OptimizeUtilsByteArray instance = new OptimizeUtilsByteArray();
|
/**
|
||||||
|
* FROM KRYO
|
||||||
public static OptimizeUtilsByteArray get() {
|
* <p>
|
||||||
return instance;
|
* Returns the number of bytes that would be written with {@link #writeInt(byte[], int, boolean)}.
|
||||||
}
|
*
|
||||||
|
* @param optimizePositive
|
||||||
private OptimizeUtilsByteArray() {
|
* true if you want to optimize the number of bytes needed to write the length value
|
||||||
|
*/
|
||||||
|
public static
|
||||||
|
int intLength(int value, boolean optimizePositive) {
|
||||||
|
return ByteBuffer2.intLength(value, optimizePositive);
|
||||||
}
|
}
|
||||||
|
|
||||||
// int
|
// int
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FROM KRYO
|
* FROM KRYO
|
||||||
*
|
* <p>
|
||||||
* Returns the number of bytes that would be written with {@link #writeInt(int, boolean)}.
|
|
||||||
*
|
|
||||||
* @param optimizePositive true if you want to optimize the number of bytes needed to write the length value
|
|
||||||
*/
|
|
||||||
public final int intLength (int value, boolean optimizePositive) {
|
|
||||||
return ByteBuffer2.intLength(value, optimizePositive);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* FROM KRYO
|
|
||||||
*
|
|
||||||
* look at buffer, and see if we can read the length of the int off of it. (from the reader index)
|
* look at buffer, and see if we can read the length of the int off of it. (from the reader index)
|
||||||
*
|
*
|
||||||
* @return 0 if we could not read anything, >0 for the number of bytes for the int on the buffer
|
* @return 0 if we could not read anything, >0 for the number of bytes for the int on the buffer
|
||||||
*/
|
*/
|
||||||
public boolean canReadInt(byte[] buffer) {
|
@SuppressWarnings("SimplifiableIfStatement")
|
||||||
|
public static
|
||||||
|
boolean canReadInt(byte[] buffer) {
|
||||||
int length = buffer.length;
|
int length = buffer.length;
|
||||||
|
|
||||||
if (length >= 5) {
|
if (length >= 5) {
|
||||||
@ -93,18 +89,17 @@ public class OptimizeUtilsByteArray {
|
|||||||
if ((buffer[p++] & 0x80) == 0) {
|
if ((buffer[p++] & 0x80) == 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (p == length) {
|
return p != length;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FROM KRYO
|
* FROM KRYO
|
||||||
*
|
* <p>
|
||||||
* Reads an int from the buffer that was optimized.
|
* Reads an int from the buffer that was optimized.
|
||||||
*/
|
*/
|
||||||
public int readInt (byte[] buffer, boolean optimizePositive) {
|
@SuppressWarnings("UnusedAssignment")
|
||||||
|
public static
|
||||||
|
int readInt(byte[] buffer, boolean optimizePositive) {
|
||||||
int position = 0;
|
int position = 0;
|
||||||
int b = buffer[position++];
|
int b = buffer[position++];
|
||||||
int result = b & 0x7F;
|
int result = b & 0x7F;
|
||||||
@ -129,56 +124,60 @@ public class OptimizeUtilsByteArray {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* FROM KRYO
|
* FROM KRYO
|
||||||
*
|
* <p>
|
||||||
* Writes the specified int to the buffer using 1 to 5 bytes, depending on the size of the number.
|
* Writes the specified int to the buffer using 1 to 5 bytes, depending on the size of the number.
|
||||||
*
|
*
|
||||||
* @param optimizePositive true if you want to optimize the number of bytes needed to write the length value
|
* @param optimizePositive
|
||||||
|
* true if you want to optimize the number of bytes needed to write the length value
|
||||||
|
*
|
||||||
* @return the number of bytes written.
|
* @return the number of bytes written.
|
||||||
*/
|
*/
|
||||||
public int writeInt (byte[] buffer, int value, boolean optimizePositive) {
|
@SuppressWarnings({"UnusedAssignment", "NumericCastThatLosesPrecision", "Duplicates"})
|
||||||
|
public static
|
||||||
|
int writeInt(byte[] buffer, int value, boolean optimizePositive) {
|
||||||
int position = 0;
|
int position = 0;
|
||||||
if (!optimizePositive) {
|
if (!optimizePositive) {
|
||||||
value = value << 1 ^ value >> 31;
|
value = value << 1 ^ value >> 31;
|
||||||
}
|
}
|
||||||
if (value >>> 7 == 0) {
|
if (value >>> 7 == 0) {
|
||||||
buffer[position++] = (byte)value;
|
buffer[position++] = (byte) value;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (value >>> 14 == 0) {
|
if (value >>> 14 == 0) {
|
||||||
buffer[position++] = (byte)(value & 0x7F | 0x80);
|
buffer[position++] = (byte) (value & 0x7F | 0x80);
|
||||||
buffer[position++] = (byte)(value >>> 7);
|
buffer[position++] = (byte) (value >>> 7);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
if (value >>> 21 == 0) {
|
if (value >>> 21 == 0) {
|
||||||
buffer[position++] = (byte)(value & 0x7F | 0x80);
|
buffer[position++] = (byte) (value & 0x7F | 0x80);
|
||||||
buffer[position++] = (byte)(value >>> 7 | 0x80);
|
buffer[position++] = (byte) (value >>> 7 | 0x80);
|
||||||
buffer[position++] = (byte)(value >>> 14);
|
buffer[position++] = (byte) (value >>> 14);
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
if (value >>> 28 == 0) {
|
if (value >>> 28 == 0) {
|
||||||
buffer[position++] = (byte)(value & 0x7F | 0x80);
|
buffer[position++] = (byte) (value & 0x7F | 0x80);
|
||||||
buffer[position++] = (byte)(value >>> 7 | 0x80);
|
buffer[position++] = (byte) (value >>> 7 | 0x80);
|
||||||
buffer[position++] = (byte)(value >>> 14 | 0x80);
|
buffer[position++] = (byte) (value >>> 14 | 0x80);
|
||||||
buffer[position++] = (byte)(value >>> 21);
|
buffer[position++] = (byte) (value >>> 21);
|
||||||
return 4;
|
return 4;
|
||||||
}
|
}
|
||||||
buffer[position++] = (byte)(value & 0x7F | 0x80);
|
buffer[position++] = (byte) (value & 0x7F | 0x80);
|
||||||
buffer[position++] = (byte)(value >>> 7 | 0x80);
|
buffer[position++] = (byte) (value >>> 7 | 0x80);
|
||||||
buffer[position++] = (byte)(value >>> 14 | 0x80);
|
buffer[position++] = (byte) (value >>> 14 | 0x80);
|
||||||
buffer[position++] = (byte)(value >>> 21 | 0x80);
|
buffer[position++] = (byte) (value >>> 21 | 0x80);
|
||||||
buffer[position++] = (byte)(value >>> 28);
|
buffer[position++] = (byte) (value >>> 28);
|
||||||
return 5;
|
return 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// long
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of bytes that would be written with {@link #writeLong(long, boolean)}.
|
* Returns the number of bytes that would be written with {@link #writeLong(byte[], long, boolean)}.
|
||||||
*
|
*
|
||||||
* @param optimizePositive true if you want to optimize the number of bytes needed to write the length value
|
* @param optimizePositive
|
||||||
|
* true if you want to optimize the number of bytes needed to write the length value
|
||||||
*/
|
*/
|
||||||
public final int longLength (long value, boolean optimizePositive) {
|
@SuppressWarnings("Duplicates")
|
||||||
|
public static
|
||||||
|
int longLength(long value, boolean optimizePositive) {
|
||||||
if (!optimizePositive) {
|
if (!optimizePositive) {
|
||||||
value = value << 1 ^ value >> 63;
|
value = value << 1 ^ value >> 63;
|
||||||
}
|
}
|
||||||
@ -209,14 +208,20 @@ public class OptimizeUtilsByteArray {
|
|||||||
return 9;
|
return 9;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// long
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FROM KRYO
|
* FROM KRYO
|
||||||
*
|
* <p>
|
||||||
* Reads a 1-9 byte long.
|
* Reads a 1-9 byte long.
|
||||||
*
|
*
|
||||||
* @param optimizePositive true if you want to optimize the number of bytes needed to write the length value
|
* @param optimizePositive
|
||||||
|
* true if you want to optimize the number of bytes needed to write the length value
|
||||||
*/
|
*/
|
||||||
public long readLong (byte[] buffer, boolean optimizePositive) {
|
@SuppressWarnings({"IntegerMultiplicationImplicitCastToLong", "UnusedAssignment"})
|
||||||
|
public static
|
||||||
|
long readLong(byte[] buffer, boolean optimizePositive) {
|
||||||
int position = 0;
|
int position = 0;
|
||||||
int b = buffer[position++];
|
int b = buffer[position++];
|
||||||
long result = b & 0x7F;
|
long result = b & 0x7F;
|
||||||
@ -231,19 +236,19 @@ public class OptimizeUtilsByteArray {
|
|||||||
result |= (b & 0x7F) << 21;
|
result |= (b & 0x7F) << 21;
|
||||||
if ((b & 0x80) != 0) {
|
if ((b & 0x80) != 0) {
|
||||||
b = buffer[position++];
|
b = buffer[position++];
|
||||||
result |= (long)(b & 0x7F) << 28;
|
result |= (long) (b & 0x7F) << 28;
|
||||||
if ((b & 0x80) != 0) {
|
if ((b & 0x80) != 0) {
|
||||||
b = buffer[position++];
|
b = buffer[position++];
|
||||||
result |= (long)(b & 0x7F) << 35;
|
result |= (long) (b & 0x7F) << 35;
|
||||||
if ((b & 0x80) != 0) {
|
if ((b & 0x80) != 0) {
|
||||||
b = buffer[position++];
|
b = buffer[position++];
|
||||||
result |= (long)(b & 0x7F) << 42;
|
result |= (long) (b & 0x7F) << 42;
|
||||||
if ((b & 0x80) != 0) {
|
if ((b & 0x80) != 0) {
|
||||||
b = buffer[position++];
|
b = buffer[position++];
|
||||||
result |= (long)(b & 0x7F) << 49;
|
result |= (long) (b & 0x7F) << 49;
|
||||||
if ((b & 0x80) != 0) {
|
if ((b & 0x80) != 0) {
|
||||||
b = buffer[position++];
|
b = buffer[position++];
|
||||||
result |= (long)b << 56;
|
result |= (long) b << 56;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -258,7 +263,98 @@ public class OptimizeUtilsByteArray {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean canReadLong (byte[] buffer) {
|
/**
|
||||||
|
* FROM KRYO
|
||||||
|
* <p>
|
||||||
|
* Writes a 1-9 byte long.
|
||||||
|
*
|
||||||
|
* @param optimizePositive
|
||||||
|
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
|
||||||
|
* bytes).
|
||||||
|
*
|
||||||
|
* @return the number of bytes written.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings({"Duplicates", "UnusedAssignment", "NumericCastThatLosesPrecision"})
|
||||||
|
public static
|
||||||
|
int writeLong(byte[] buffer, long value, boolean optimizePositive) {
|
||||||
|
if (!optimizePositive) {
|
||||||
|
value = value << 1 ^ value >> 63;
|
||||||
|
}
|
||||||
|
int position = 0;
|
||||||
|
if (value >>> 7 == 0) {
|
||||||
|
buffer[position++] = (byte) value;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (value >>> 14 == 0) {
|
||||||
|
buffer[position++] = (byte) (value & 0x7F | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 7);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
if (value >>> 21 == 0) {
|
||||||
|
buffer[position++] = (byte) (value & 0x7F | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 7 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 14);
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
if (value >>> 28 == 0) {
|
||||||
|
buffer[position++] = (byte) (value & 0x7F | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 7 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 14 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 21);
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
if (value >>> 35 == 0) {
|
||||||
|
buffer[position++] = (byte) (value & 0x7F | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 7 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 14 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 21 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 28);
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
if (value >>> 42 == 0) {
|
||||||
|
buffer[position++] = (byte) (value & 0x7F | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 7 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 14 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 21 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 28 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 35);
|
||||||
|
return 6;
|
||||||
|
}
|
||||||
|
if (value >>> 49 == 0) {
|
||||||
|
buffer[position++] = (byte) (value & 0x7F | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 7 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 14 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 21 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 28 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 35 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 42);
|
||||||
|
return 7;
|
||||||
|
}
|
||||||
|
if (value >>> 56 == 0) {
|
||||||
|
buffer[position++] = (byte) (value & 0x7F | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 7 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 14 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 21 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 28 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 35 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 42 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 49);
|
||||||
|
return 8;
|
||||||
|
}
|
||||||
|
buffer[position++] = (byte) (value & 0x7F | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 7 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 14 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 21 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 28 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 35 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 42 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 49 | 0x80);
|
||||||
|
buffer[position++] = (byte) (value >>> 56);
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static
|
||||||
|
boolean canReadLong(byte[] buffer) {
|
||||||
int limit = buffer.length;
|
int limit = buffer.length;
|
||||||
|
|
||||||
if (limit >= 9) {
|
if (limit >= 9) {
|
||||||
@ -307,12 +403,17 @@ public class OptimizeUtilsByteArray {
|
|||||||
if (p == limit) {
|
if (p == limit) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
//noinspection SimplifiableIfStatement
|
||||||
if ((buffer[p++] & 0x80) == 0) {
|
if ((buffer[p++] & 0x80) == 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (p == limit) {
|
|
||||||
return false;
|
return p != limit;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
|
||||||
|
|
||||||
|
private
|
||||||
|
OptimizeUtilsByteArray() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,25 +36,21 @@ package dorkbox.util.bytes;
|
|||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
|
|
||||||
public class OptimizeUtilsByteBuf {
|
public
|
||||||
|
class OptimizeUtilsByteBuf {
|
||||||
private static final OptimizeUtilsByteBuf instance = new OptimizeUtilsByteBuf();
|
|
||||||
|
|
||||||
public static OptimizeUtilsByteBuf get() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
// int
|
// int
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FROM KRYO
|
* FROM KRYO
|
||||||
*
|
* <p>
|
||||||
* Returns the number of bytes that would be written with {@link #writeInt(int, boolean)}.
|
* Returns the number of bytes that would be written with {@link #writeInt(ByteBuf, int, boolean)}.
|
||||||
*
|
*
|
||||||
* @param optimizePositive
|
* @param optimizePositive
|
||||||
* true if you want to optimize the number of bytes needed to write the length value
|
* true if you want to optimize the number of bytes needed to write the length value
|
||||||
*/
|
*/
|
||||||
public final int intLength(int value, boolean optimizePositive) {
|
public static
|
||||||
|
int intLength(int value, boolean optimizePositive) {
|
||||||
if (!optimizePositive) {
|
if (!optimizePositive) {
|
||||||
value = value << 1 ^ value >> 31;
|
value = value << 1 ^ value >> 31;
|
||||||
}
|
}
|
||||||
@ -75,12 +71,13 @@ public class OptimizeUtilsByteBuf {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* FROM KRYO
|
* FROM KRYO
|
||||||
*
|
* <p>
|
||||||
* look at buffer, and see if we can read the length of the int off of it. (from the reader index)
|
* look at buffer, and see if we can read the length of the int off of it. (from the reader index)
|
||||||
*
|
*
|
||||||
* @return 0 if we could not read anything, >0 for the number of bytes for the int on the buffer
|
* @return 0 if we could not read anything, >0 for the number of bytes for the int on the buffer
|
||||||
*/
|
*/
|
||||||
public final int canReadInt(ByteBuf buffer) {
|
public static
|
||||||
|
int canReadInt(ByteBuf buffer) {
|
||||||
int startIndex = buffer.readerIndex();
|
int startIndex = buffer.readerIndex();
|
||||||
try {
|
try {
|
||||||
int remaining = buffer.readableBytes();
|
int remaining = buffer.readableBytes();
|
||||||
@ -98,10 +95,11 @@ public class OptimizeUtilsByteBuf {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* FROM KRYO
|
* FROM KRYO
|
||||||
*
|
* <p>
|
||||||
* Reads an int from the buffer that was optimized.
|
* Reads an int from the buffer that was optimized.
|
||||||
*/
|
*/
|
||||||
public final int readInt(ByteBuf buffer, boolean optimizePositive) {
|
public static
|
||||||
|
int readInt(ByteBuf buffer, boolean optimizePositive) {
|
||||||
int b = buffer.readByte();
|
int b = buffer.readByte();
|
||||||
int result = b & 0x7F;
|
int result = b & 0x7F;
|
||||||
if ((b & 0x80) != 0) {
|
if ((b & 0x80) != 0) {
|
||||||
@ -125,14 +123,16 @@ public class OptimizeUtilsByteBuf {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* FROM KRYO
|
* FROM KRYO
|
||||||
*
|
* <p>
|
||||||
* Writes the specified int to the buffer using 1 to 5 bytes, depending on the size of the number.
|
* Writes the specified int to the buffer using 1 to 5 bytes, depending on the size of the number.
|
||||||
*
|
*
|
||||||
* @param optimizePositive
|
* @param optimizePositive
|
||||||
* true if you want to optimize the number of bytes needed to write the length value
|
* true if you want to optimize the number of bytes needed to write the length value
|
||||||
|
*
|
||||||
* @return the number of bytes written.
|
* @return the number of bytes written.
|
||||||
*/
|
*/
|
||||||
public final int writeInt(ByteBuf buffer, int value, boolean optimizePositive) {
|
public static
|
||||||
|
int writeInt(ByteBuf buffer, int value, boolean optimizePositive) {
|
||||||
if (!optimizePositive) {
|
if (!optimizePositive) {
|
||||||
value = value << 1 ^ value >> 31;
|
value = value << 1 ^ value >> 31;
|
||||||
}
|
}
|
||||||
@ -172,9 +172,10 @@ public class OptimizeUtilsByteBuf {
|
|||||||
* Returns the number of bytes that would be written with {@link #writeLong(long, boolean)}.
|
* Returns the number of bytes that would be written with {@link #writeLong(long, boolean)}.
|
||||||
*
|
*
|
||||||
* @param optimizePositive
|
* @param optimizePositive
|
||||||
* true if you want to optimize the number of bytes needed to write the length value
|
* true if you want to optimize the number of bytes needed to write the length value
|
||||||
*/
|
*/
|
||||||
public final int longLength(long value, boolean optimizePositive) {
|
public static
|
||||||
|
int longLength(long value, boolean optimizePositive) {
|
||||||
if (!optimizePositive) {
|
if (!optimizePositive) {
|
||||||
value = value << 1 ^ value >> 63;
|
value = value << 1 ^ value >> 63;
|
||||||
}
|
}
|
||||||
@ -207,13 +208,14 @@ public class OptimizeUtilsByteBuf {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* FROM KRYO
|
* FROM KRYO
|
||||||
*
|
* <p>
|
||||||
* Reads a 1-9 byte long.
|
* Reads a 1-9 byte long.
|
||||||
*
|
*
|
||||||
* @param optimizePositive
|
* @param optimizePositive
|
||||||
* true if you want to optimize the number of bytes needed to write the length value
|
* true if you want to optimize the number of bytes needed to write the length value
|
||||||
*/
|
*/
|
||||||
public final long readLong(ByteBuf buffer, boolean optimizePositive) {
|
public static
|
||||||
|
long readLong(ByteBuf buffer, boolean optimizePositive) {
|
||||||
int b = buffer.readByte();
|
int b = buffer.readByte();
|
||||||
long result = b & 0x7F;
|
long result = b & 0x7F;
|
||||||
if ((b & 0x80) != 0) {
|
if ((b & 0x80) != 0) {
|
||||||
@ -256,15 +258,17 @@ public class OptimizeUtilsByteBuf {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* FROM KRYO
|
* FROM KRYO
|
||||||
*
|
* <p>
|
||||||
* Writes a 1-9 byte long.
|
* Writes a 1-9 byte long.
|
||||||
*
|
*
|
||||||
* @param optimizePositive
|
* @param optimizePositive
|
||||||
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
|
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
|
||||||
* inefficient (9 bytes).
|
* bytes).
|
||||||
|
*
|
||||||
* @return the number of bytes written.
|
* @return the number of bytes written.
|
||||||
*/
|
*/
|
||||||
public final int writeLong(ByteBuf buffer, long value, boolean optimizePositive) {
|
public static
|
||||||
|
int writeLong(ByteBuf buffer, long value, boolean optimizePositive) {
|
||||||
if (!optimizePositive) {
|
if (!optimizePositive) {
|
||||||
value = value << 1 ^ value >> 63;
|
value = value << 1 ^ value >> 63;
|
||||||
}
|
}
|
||||||
@ -342,12 +346,13 @@ public class OptimizeUtilsByteBuf {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* FROM KRYO
|
* FROM KRYO
|
||||||
*
|
* <p>
|
||||||
* look at buffer, and see if we can read the length of the long off of it (from the reader index).
|
* look at buffer, and see if we can read the length of the long off of it (from the reader index).
|
||||||
*
|
*
|
||||||
* @return 0 if we could not read anything, >0 for the number of bytes for the long on the buffer
|
* @return 0 if we could not read anything, >0 for the number of bytes for the long on the buffer
|
||||||
*/
|
*/
|
||||||
public final int canReadLong(ByteBuf buffer) {
|
public static
|
||||||
|
int canReadLong(ByteBuf buffer) {
|
||||||
int position = buffer.readerIndex();
|
int position = buffer.readerIndex();
|
||||||
try {
|
try {
|
||||||
int remaining = buffer.readableBytes();
|
int remaining = buffer.readableBytes();
|
||||||
|
@ -56,7 +56,6 @@ import java.util.jar.JarFile;
|
|||||||
* <p/>
|
* <p/>
|
||||||
* To determine if we have hardware accelerated AES java -XX:+PrintFlagsFinal -version | grep UseAES
|
* To determine if we have hardware accelerated AES java -XX:+PrintFlagsFinal -version | grep UseAES
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public final
|
public final
|
||||||
class Crypto {
|
class Crypto {
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ import java.io.OutputStream;
|
|||||||
/**
|
/**
|
||||||
* AES crypto functions
|
* AES crypto functions
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"unused", "Duplicates"})
|
@SuppressWarnings({"Duplicates"})
|
||||||
public final
|
public final
|
||||||
class CryptoAES {
|
class CryptoAES {
|
||||||
private static final int ivSize = 16;
|
private static final int ivSize = 16;
|
||||||
@ -37,6 +37,8 @@ class CryptoAES {
|
|||||||
/**
|
/**
|
||||||
* AES encrypts data with a specified key.
|
* AES encrypts data with a specified key.
|
||||||
*
|
*
|
||||||
|
* @param aesIV
|
||||||
|
* must be a nonce (unique value) !!
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
*
|
*
|
||||||
@ -57,12 +59,14 @@ class CryptoAES {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
||||||
* <p/>
|
* <p>
|
||||||
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
||||||
* data.
|
* data.
|
||||||
* <p/>
|
* <p>
|
||||||
* AES encrypts data with a specified key.
|
* AES encrypts data with a specified key.
|
||||||
*
|
*
|
||||||
|
* @param aesIV
|
||||||
|
* must be a nonce (unique value) !!
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
*
|
*
|
||||||
@ -86,18 +90,15 @@ class CryptoAES {
|
|||||||
/**
|
/**
|
||||||
* AES encrypts data with a specified key.
|
* AES encrypts data with a specified key.
|
||||||
*
|
*
|
||||||
|
* @param aesIV
|
||||||
|
* must be a nonce (unique value) !!
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
*
|
*
|
||||||
* @return true if successful
|
* @return true if successful
|
||||||
*/
|
*/
|
||||||
public static
|
public static
|
||||||
boolean encryptStreamWithIV(GCMBlockCipher aesEngine,
|
boolean encryptStreamWithIV(GCMBlockCipher aesEngine, byte[] aesKey, byte[] aesIV, InputStream in, OutputStream out, Logger logger) {
|
||||||
byte[] aesKey,
|
|
||||||
byte[] aesIV,
|
|
||||||
InputStream in,
|
|
||||||
OutputStream out,
|
|
||||||
Logger logger) {
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
out.write(aesIV);
|
out.write(aesIV);
|
||||||
@ -113,12 +114,14 @@ class CryptoAES {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
||||||
* <p/>
|
* <p>
|
||||||
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
||||||
* data.
|
* data.
|
||||||
* <p/>
|
* <p>
|
||||||
* AES encrypts data with a specified key.
|
* AES encrypts data with a specified key.
|
||||||
*
|
*
|
||||||
|
* @param aesIV
|
||||||
|
* must be a nonce (unique value) !!
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
*
|
*
|
||||||
@ -148,6 +151,8 @@ class CryptoAES {
|
|||||||
/**
|
/**
|
||||||
* AES encrypts data with a specified key.
|
* AES encrypts data with a specified key.
|
||||||
*
|
*
|
||||||
|
* @param aesIV
|
||||||
|
* must be a nonce (unique value) !!
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
*
|
*
|
||||||
@ -158,30 +163,7 @@ class CryptoAES {
|
|||||||
int length = data.length;
|
int length = data.length;
|
||||||
|
|
||||||
CipherParameters aesIVAndKey = new ParametersWithIV(new KeyParameter(aesKey), aesIV);
|
CipherParameters aesIVAndKey = new ParametersWithIV(new KeyParameter(aesKey), aesIV);
|
||||||
aesEngine.init(true, aesIVAndKey);
|
return encrypt(aesEngine, aesIVAndKey, data, length, logger);
|
||||||
|
|
||||||
int minSize = aesEngine.getOutputSize(length);
|
|
||||||
byte[] outBuf = new byte[minSize];
|
|
||||||
|
|
||||||
int actualLength = aesEngine.processBytes(data, 0, length, outBuf, 0);
|
|
||||||
|
|
||||||
try {
|
|
||||||
actualLength += aesEngine.doFinal(outBuf, actualLength);
|
|
||||||
} catch (Exception e) {
|
|
||||||
if (logger != null) {
|
|
||||||
logger.error("Unable to perform AES cipher.", e);
|
|
||||||
}
|
|
||||||
return new byte[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (outBuf.length == actualLength) {
|
|
||||||
return outBuf;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
byte[] result = new byte[actualLength];
|
|
||||||
System.arraycopy(outBuf, 0, result, 0, result.length);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -193,42 +175,45 @@ class CryptoAES {
|
|||||||
* @return length of encrypted data, -1 if there was an error.
|
* @return length of encrypted data, -1 if there was an error.
|
||||||
*/
|
*/
|
||||||
public static
|
public static
|
||||||
int encrypt(dorkbox.util.crypto.bouncycastle.GCMBlockCipher_ByteBuf aesEngine,
|
byte[] encrypt(GCMBlockCipher aesEngine, CipherParameters aesIVAndKey, byte[] data, int length, Logger logger) {
|
||||||
CipherParameters aesIVAndKey,
|
|
||||||
io.netty.buffer.ByteBuf inBuffer,
|
|
||||||
io.netty.buffer.ByteBuf outBuffer,
|
|
||||||
int length,
|
|
||||||
Logger logger) {
|
|
||||||
|
|
||||||
aesEngine.reset();
|
aesEngine.reset();
|
||||||
aesEngine.init(true, aesIVAndKey);
|
aesEngine.init(true, aesIVAndKey);
|
||||||
|
|
||||||
length = aesEngine.processBytes(inBuffer, outBuffer, length);
|
int minSize = aesEngine.getOutputSize(length);
|
||||||
|
byte[] outArray = new byte[minSize];
|
||||||
|
|
||||||
|
int actualLength = aesEngine.processBytes(data, 0, length, outArray, 0);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
length += aesEngine.doFinal(outBuffer);
|
actualLength += aesEngine.doFinal(outArray, actualLength);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (logger != null) {
|
if (logger != null) {
|
||||||
logger.debug("Unable to perform AES cipher.", e);
|
logger.error("Unable to perform AES cipher.", e);
|
||||||
}
|
}
|
||||||
return -1;
|
return new byte[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
// specify where the encrypted data is at
|
if (outArray.length == actualLength) {
|
||||||
outBuffer.readerIndex(0);
|
return outArray;
|
||||||
outBuffer.writerIndex(length);
|
}
|
||||||
|
else {
|
||||||
return length;
|
byte[] result = new byte[actualLength];
|
||||||
|
System.arraycopy(outArray, 0, result, 0, result.length);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
||||||
* <p/>
|
* <p>
|
||||||
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
||||||
* data.
|
* data.
|
||||||
* <p/>
|
* <p>
|
||||||
* AES encrypts data with a specified key.
|
* AES encrypts data with a specified key.
|
||||||
*
|
*
|
||||||
|
* @param aesIV
|
||||||
|
* must be a nonce (unique value) !!
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
*
|
*
|
||||||
@ -268,12 +253,14 @@ class CryptoAES {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
||||||
* <p/>
|
* <p>
|
||||||
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
||||||
* data.
|
* data.
|
||||||
* <p/>
|
* <p>
|
||||||
* AES encrypt from one stream to another.
|
* AES encrypt from one stream to another.
|
||||||
*
|
*
|
||||||
|
* @param aesIV
|
||||||
|
* must be a nonce (unique value) !!
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
*
|
*
|
||||||
@ -316,6 +303,8 @@ class CryptoAES {
|
|||||||
*
|
*
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
|
* @param aesIV
|
||||||
|
* must be a nonce (unique value) !!
|
||||||
*
|
*
|
||||||
* @return true if successful
|
* @return true if successful
|
||||||
*/
|
*/
|
||||||
@ -352,7 +341,7 @@ class CryptoAES {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AES decrypt (if the aes IV is included in the data)
|
* AES decrypt (if the aes IV is included in the data). IV must be a nonce (unique value) !!
|
||||||
*
|
*
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
@ -372,11 +361,11 @@ class CryptoAES {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
||||||
* <p/>
|
* <p>
|
||||||
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
||||||
* data.
|
* data.
|
||||||
* <p/>
|
* <p>
|
||||||
* AES decrypt (if the aes IV is included in the data)
|
* AES decrypt (if the aes IV is included in the data). IV must be a nonce (unique value)
|
||||||
*
|
*
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
@ -396,7 +385,7 @@ class CryptoAES {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AES decrypt (if the aes IV is included in the data)
|
* AES decrypt (if the aes IV is included in the data. IV must be a nonce (unique value)
|
||||||
*
|
*
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
@ -420,11 +409,11 @@ class CryptoAES {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
||||||
* <p/>
|
* <p>
|
||||||
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
||||||
* data.
|
* data.
|
||||||
* <p/>
|
* <p>
|
||||||
* AES decrypt (if the aes IV is included in the data)
|
* AES decrypt (if the aes IV is included in the data). IV must be a nonce (unique value)
|
||||||
*
|
*
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
@ -450,6 +439,8 @@ class CryptoAES {
|
|||||||
/**
|
/**
|
||||||
* AES decrypt (if we already know the aes IV -- and it's NOT included in the data)
|
* AES decrypt (if we already know the aes IV -- and it's NOT included in the data)
|
||||||
*
|
*
|
||||||
|
* @param aesIV
|
||||||
|
* must be a nonce (unique value) !!
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
*
|
*
|
||||||
@ -485,52 +476,16 @@ class CryptoAES {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* AES decrypt (if we already know the aes IV -- and it's NOT included in the data)
|
|
||||||
*
|
|
||||||
* @param logger
|
|
||||||
* may be null, if no log output is necessary
|
|
||||||
*
|
|
||||||
* @return length of decrypted data, -1 if there was an error.
|
|
||||||
*/
|
|
||||||
public static
|
|
||||||
int decrypt(dorkbox.util.crypto.bouncycastle.GCMBlockCipher_ByteBuf aesEngine,
|
|
||||||
ParametersWithIV aesIVAndKey,
|
|
||||||
io.netty.buffer.ByteBuf bufferWithData,
|
|
||||||
io.netty.buffer.ByteBuf bufferTempData,
|
|
||||||
int length,
|
|
||||||
Logger logger) {
|
|
||||||
|
|
||||||
aesEngine.reset();
|
|
||||||
aesEngine.init(false, aesIVAndKey);
|
|
||||||
|
|
||||||
// ignore the start position
|
|
||||||
// we also do NOT want to have the same start position for the altBuffer, since it could then grow larger than the buffer capacity.
|
|
||||||
length = aesEngine.processBytes(bufferWithData, bufferTempData, length);
|
|
||||||
|
|
||||||
try {
|
|
||||||
length += aesEngine.doFinal(bufferTempData);
|
|
||||||
} catch (Exception e) {
|
|
||||||
if (logger != null) {
|
|
||||||
logger.debug("Unable to perform AES cipher.", e);
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
bufferTempData.readerIndex(0);
|
|
||||||
bufferTempData.writerIndex(length);
|
|
||||||
|
|
||||||
return length;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
||||||
* <p/>
|
* <p>
|
||||||
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
||||||
* data.
|
* data.
|
||||||
* <p/>
|
* <p>
|
||||||
* AES decrypt (if we already know the aes IV -- and it's NOT included in the data)
|
* AES decrypt (if we already know the aes IV -- and it's NOT included in the data)
|
||||||
*
|
*
|
||||||
|
* @param aesIV
|
||||||
|
* must be a nonce (unique value) !!
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
*
|
*
|
||||||
@ -572,6 +527,8 @@ class CryptoAES {
|
|||||||
/**
|
/**
|
||||||
* AES decrypt from one stream to another.
|
* AES decrypt from one stream to another.
|
||||||
*
|
*
|
||||||
|
* @param aesIV
|
||||||
|
* must be a nonce (unique value) !!
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
*
|
*
|
||||||
@ -610,12 +567,14 @@ class CryptoAES {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
|
||||||
* <p/>
|
* <p>
|
||||||
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
|
||||||
* data.
|
* data.
|
||||||
* <p/>
|
* <p>
|
||||||
* AES decrypt from one stream to another.
|
* AES decrypt from one stream to another.
|
||||||
*
|
*
|
||||||
|
* @param aesIV
|
||||||
|
* must be a nonce (unique value) !!
|
||||||
* @param logger
|
* @param logger
|
||||||
* may be null, if no log output is necessary
|
* may be null, if no log output is necessary
|
||||||
*
|
*
|
||||||
|
@ -33,7 +33,6 @@ import java.security.SecureRandom;
|
|||||||
/**
|
/**
|
||||||
* This is here just for keeping track of how this is done. This should NOT be used, and instead use ECC crypto.
|
* This is here just for keeping track of how this is done. This should NOT be used, and instead use ECC crypto.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public final
|
public final
|
||||||
class CryptoRSA {
|
class CryptoRSA {
|
||||||
|
@ -21,7 +21,6 @@ import java.util.Arrays;
|
|||||||
/**
|
/**
|
||||||
* An implementation of the <a href="http://www.tarsnap.com/scrypt/scrypt.pdf"/>scrypt</a> key derivation function.
|
* An implementation of the <a href="http://www.tarsnap.com/scrypt/scrypt.pdf"/>scrypt</a> key derivation function.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public final
|
public final
|
||||||
class CryptoSCrypt {
|
class CryptoSCrypt {
|
||||||
/**
|
/**
|
||||||
|
@ -15,48 +15,10 @@
|
|||||||
*/
|
*/
|
||||||
package dorkbox.util.crypto;
|
package dorkbox.util.crypto;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import dorkbox.util.Base64Fast;
|
||||||
import java.io.ByteArrayInputStream;
|
import dorkbox.util.crypto.signers.BcECDSAContentSignerBuilder;
|
||||||
import java.io.FileWriter;
|
import dorkbox.util.crypto.signers.BcECDSAContentVerifierProviderBuilder;
|
||||||
import java.io.IOException;
|
import org.bouncycastle.asn1.*;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.Writer;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.math.BigInteger;
|
|
||||||
import java.security.InvalidKeyException;
|
|
||||||
import java.security.KeyFactory;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.security.NoSuchProviderException;
|
|
||||||
import java.security.PrivateKey;
|
|
||||||
import java.security.Provider;
|
|
||||||
import java.security.PublicKey;
|
|
||||||
import java.security.Security;
|
|
||||||
import java.security.SignatureException;
|
|
||||||
import java.security.cert.CertificateEncodingException;
|
|
||||||
import java.security.cert.CertificateException;
|
|
||||||
import java.security.cert.X509Certificate;
|
|
||||||
import java.security.spec.DSAParameterSpec;
|
|
||||||
import java.security.spec.InvalidKeySpecException;
|
|
||||||
import java.security.spec.RSAPrivateCrtKeySpec;
|
|
||||||
import java.security.spec.RSAPublicKeySpec;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.Enumeration;
|
|
||||||
|
|
||||||
import org.bouncycastle.asn1.ASN1EncodableVector;
|
|
||||||
import org.bouncycastle.asn1.ASN1Encoding;
|
|
||||||
import org.bouncycastle.asn1.ASN1InputStream;
|
|
||||||
import org.bouncycastle.asn1.ASN1Integer;
|
|
||||||
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
|
|
||||||
import org.bouncycastle.asn1.ASN1Primitive;
|
|
||||||
import org.bouncycastle.asn1.ASN1Sequence;
|
|
||||||
import org.bouncycastle.asn1.ASN1Set;
|
|
||||||
import org.bouncycastle.asn1.ASN1TaggedObject;
|
|
||||||
import org.bouncycastle.asn1.BERSet;
|
|
||||||
import org.bouncycastle.asn1.DERBMPString;
|
|
||||||
import org.bouncycastle.asn1.DEROctetString;
|
|
||||||
import org.bouncycastle.asn1.DERSequence;
|
|
||||||
import org.bouncycastle.asn1.DERSet;
|
|
||||||
import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
|
import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
|
||||||
import org.bouncycastle.asn1.cms.ContentInfo;
|
import org.bouncycastle.asn1.cms.ContentInfo;
|
||||||
import org.bouncycastle.asn1.cms.IssuerAndSerialNumber;
|
import org.bouncycastle.asn1.cms.IssuerAndSerialNumber;
|
||||||
@ -118,9 +80,33 @@ import org.bouncycastle.operator.bc.BcRSAContentVerifierProviderBuilder;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import dorkbox.util.Base64Fast;
|
import java.io.BufferedWriter;
|
||||||
import dorkbox.util.crypto.signers.BcECDSAContentSignerBuilder;
|
import java.io.ByteArrayInputStream;
|
||||||
import dorkbox.util.crypto.signers.BcECDSAContentVerifierProviderBuilder;
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.Writer;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.KeyFactory;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.NoSuchProviderException;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.Provider;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
import java.security.Security;
|
||||||
|
import java.security.SignatureException;
|
||||||
|
import java.security.cert.CertificateEncodingException;
|
||||||
|
import java.security.cert.CertificateException;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
|
import java.security.spec.DSAParameterSpec;
|
||||||
|
import java.security.spec.InvalidKeySpecException;
|
||||||
|
import java.security.spec.RSAPrivateCrtKeySpec;
|
||||||
|
import java.security.spec.RSAPublicKeySpec;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
|
||||||
public class CryptoX509 {
|
public class CryptoX509 {
|
||||||
|
|
||||||
@ -139,7 +125,6 @@ public class CryptoX509 {
|
|||||||
/**
|
/**
|
||||||
* @return true if saving the x509 certificate to a PEM format file was successful
|
* @return true if saving the x509 certificate to a PEM format file was successful
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static boolean convertToPemFile(X509Certificate x509cert, String fileName) {
|
public static boolean convertToPemFile(X509Certificate x509cert, String fileName) {
|
||||||
boolean failed = false;
|
boolean failed = false;
|
||||||
Writer output = null;
|
Writer output = null;
|
||||||
@ -1269,7 +1254,6 @@ public class CryptoX509 {
|
|||||||
*
|
*
|
||||||
* Code is present but commented out, as it was a PITA to figure it out, as documentation is lacking....
|
* Code is present but commented out, as it was a PITA to figure it out, as documentation is lacking....
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static void loadKeystore(String keystoreLocation, String alias, char[] passwd, char[] keypasswd) {
|
public static void loadKeystore(String keystoreLocation, String alias, char[] passwd, char[] keypasswd) {
|
||||||
// FileInputStream fileIn = new FileInputStream(keystoreLocation);
|
// FileInputStream fileIn = new FileInputStream(keystoreLocation);
|
||||||
// KeyStore keyStore = KeyStore.getInstance("JKS");
|
// KeyStore keyStore = KeyStore.getInstance("JKS");
|
||||||
|
@ -1,455 +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.
|
|
||||||
*
|
|
||||||
* Copyright (c) 2000 - 2015 The Legion of the Bouncy Castle Inc.
|
|
||||||
* (http://www.bouncycastle.org)
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
* copy of this software and associated documentation files (the "Software"),
|
|
||||||
* to deal in the Software without restriction, including without limitation
|
|
||||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
||||||
* and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
* Software is furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included
|
|
||||||
* in all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
* DEALINGS IN THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
package dorkbox.util.crypto.bouncycastle;
|
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import io.netty.buffer.Unpooled;
|
|
||||||
import org.bouncycastle.crypto.BlockCipher;
|
|
||||||
import org.bouncycastle.crypto.CipherParameters;
|
|
||||||
import org.bouncycastle.crypto.DataLengthException;
|
|
||||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
|
||||||
import org.bouncycastle.crypto.modes.AEADBlockCipher;
|
|
||||||
import org.bouncycastle.crypto.params.AEADParameters;
|
|
||||||
import org.bouncycastle.crypto.params.KeyParameter;
|
|
||||||
import org.bouncycastle.crypto.params.ParametersWithIV;
|
|
||||||
import org.bouncycastle.util.Arrays;
|
|
||||||
import org.bouncycastle.util.Pack;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements the Galois/Counter mode (GCM) detailed in
|
|
||||||
* NIST Special Publication 800-38D.
|
|
||||||
*/
|
|
||||||
public class GCMBlockCipher_ByteBuf
|
|
||||||
implements AEADBlockCipher
|
|
||||||
{
|
|
||||||
private static final int BLOCK_SIZE = 16;
|
|
||||||
private static final byte[] ZEROES = new byte[BLOCK_SIZE];
|
|
||||||
|
|
||||||
// not final due to a compiler bug
|
|
||||||
private BlockCipher cipher;
|
|
||||||
private Tables8kGCMMultiplier_ByteBuf multiplier;
|
|
||||||
|
|
||||||
// These fields are set by init and not modified by processing
|
|
||||||
private boolean forEncryption;
|
|
||||||
private int macSize;
|
|
||||||
private byte[] nonce;
|
|
||||||
private byte[] A;
|
|
||||||
private byte[] H;
|
|
||||||
private ByteBuf initS;
|
|
||||||
private byte[] J0;
|
|
||||||
|
|
||||||
// These fields are modified during processing
|
|
||||||
private byte[] bufBlock;
|
|
||||||
private byte[] macBlock;
|
|
||||||
private ByteBuf S;
|
|
||||||
private byte[] counter;
|
|
||||||
private int bufOff;
|
|
||||||
private long totalLength;
|
|
||||||
|
|
||||||
public GCMBlockCipher_ByteBuf(BlockCipher c)
|
|
||||||
{
|
|
||||||
if (c.getBlockSize() != BLOCK_SIZE)
|
|
||||||
{
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"cipher required with a block size of " + BLOCK_SIZE + ".");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.cipher = c;
|
|
||||||
this.multiplier = new Tables8kGCMMultiplier_ByteBuf();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public BlockCipher getUnderlyingCipher()
|
|
||||||
{
|
|
||||||
return this.cipher;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getAlgorithmName()
|
|
||||||
{
|
|
||||||
return this.cipher.getAlgorithmName() + "/GCM";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void init(boolean forEncryption, CipherParameters params)
|
|
||||||
throws IllegalArgumentException
|
|
||||||
{
|
|
||||||
this.forEncryption = forEncryption;
|
|
||||||
this.macBlock = null;
|
|
||||||
|
|
||||||
KeyParameter keyParam;
|
|
||||||
|
|
||||||
if (params instanceof AEADParameters)
|
|
||||||
{
|
|
||||||
AEADParameters param = (AEADParameters)params;
|
|
||||||
|
|
||||||
this.nonce = param.getNonce();
|
|
||||||
this.A = param.getAssociatedText();
|
|
||||||
|
|
||||||
int macSizeBits = param.getMacSize();
|
|
||||||
if (macSizeBits < 96 || macSizeBits > 128 || macSizeBits % 8 != 0)
|
|
||||||
{
|
|
||||||
throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.macSize = macSizeBits / 8;
|
|
||||||
keyParam = param.getKey();
|
|
||||||
}
|
|
||||||
else if (params instanceof ParametersWithIV)
|
|
||||||
{
|
|
||||||
ParametersWithIV param = (ParametersWithIV)params;
|
|
||||||
|
|
||||||
this.nonce = param.getIV();
|
|
||||||
this.A = null;
|
|
||||||
this.macSize = 16;
|
|
||||||
keyParam = (KeyParameter)param.getParameters();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new IllegalArgumentException("invalid parameters passed to GCM");
|
|
||||||
}
|
|
||||||
|
|
||||||
int bufLength = forEncryption ? BLOCK_SIZE : BLOCK_SIZE + this.macSize;
|
|
||||||
this.bufBlock = new byte[bufLength];
|
|
||||||
|
|
||||||
if (this.nonce == null || this.nonce.length < 1)
|
|
||||||
{
|
|
||||||
throw new IllegalArgumentException("IV must be at least 1 byte");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.A == null)
|
|
||||||
{
|
|
||||||
// Avoid lots of null checks
|
|
||||||
this.A = new byte[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cipher always used in forward mode
|
|
||||||
// if keyParam is null we're reusing the last key.
|
|
||||||
if (keyParam != null)
|
|
||||||
{
|
|
||||||
this.cipher.init(true, keyParam);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO This should be configurable by init parameters
|
|
||||||
// (but must be 16 if nonce length not 12) (BLOCK_SIZE?)
|
|
||||||
// this.tagLength = 16;
|
|
||||||
|
|
||||||
this.H = new byte[BLOCK_SIZE];
|
|
||||||
this.cipher.processBlock(ZEROES, 0, this.H, 0);
|
|
||||||
this.multiplier.init(this.H);
|
|
||||||
|
|
||||||
this.initS = Unpooled.wrappedBuffer(gHASH(this.A));
|
|
||||||
|
|
||||||
if (this.nonce.length == 12)
|
|
||||||
{
|
|
||||||
this.J0 = new byte[16];
|
|
||||||
System.arraycopy(this.nonce, 0, this.J0, 0, this.nonce.length);
|
|
||||||
this.J0[15] = 0x01;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
this.J0 = gHASH(this.nonce);
|
|
||||||
byte[] X = new byte[16];
|
|
||||||
packLength((long)this.nonce.length * 8, X, 8);
|
|
||||||
xor(this.J0, X);
|
|
||||||
this.multiplier.multiplyH(this.J0);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.S = this.initS.copy();
|
|
||||||
this.counter = Arrays.clone(this.J0);
|
|
||||||
this.bufOff = 0;
|
|
||||||
this.totalLength = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] getMac() {
|
|
||||||
return Arrays.clone(this.macBlock);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getOutputSize(int len) {
|
|
||||||
if (this.forEncryption) {
|
|
||||||
return len + this.bufOff + this.macSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
return len + this.bufOff - this.macSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getUpdateOutputSize(int len) {
|
|
||||||
return (len + this.bufOff) / BLOCK_SIZE * BLOCK_SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
// MODIFIED
|
|
||||||
@Override
|
|
||||||
public int processByte(byte in, byte[] out, int outOff)
|
|
||||||
throws DataLengthException
|
|
||||||
{
|
|
||||||
// DO NOTHING
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// MODIFIED
|
|
||||||
public int processBytes(ByteBuf in, ByteBuf out, int len)
|
|
||||||
throws DataLengthException
|
|
||||||
{
|
|
||||||
out.clear();
|
|
||||||
|
|
||||||
int didRead = 0;
|
|
||||||
int resultLen = 0;
|
|
||||||
|
|
||||||
while (didRead < len) {
|
|
||||||
int buffOffRead = this.bufOff + len - didRead;
|
|
||||||
|
|
||||||
if (buffOffRead >= this.bufBlock.length) {
|
|
||||||
int amtToRead = this.bufBlock.length - this.bufOff;
|
|
||||||
didRead += amtToRead;
|
|
||||||
|
|
||||||
in.readBytes(this.bufBlock, this.bufOff, amtToRead);
|
|
||||||
|
|
||||||
gCTRBlock(this.bufBlock, BLOCK_SIZE, out);
|
|
||||||
if (!this.forEncryption) {
|
|
||||||
System.arraycopy(this.bufBlock, BLOCK_SIZE, this.bufBlock, 0, this.macSize);
|
|
||||||
}
|
|
||||||
resultLen += BLOCK_SIZE;
|
|
||||||
this.bufOff = this.bufBlock.length - BLOCK_SIZE;
|
|
||||||
} else {
|
|
||||||
int read = len - didRead;
|
|
||||||
in.readBytes(this.bufBlock, this.bufOff, read);
|
|
||||||
this.bufOff += read;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return resultLen;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int doFinal(ByteBuf out) throws IllegalStateException, InvalidCipherTextException {
|
|
||||||
int extra = this.bufOff;
|
|
||||||
if (!this.forEncryption) {
|
|
||||||
if (extra < this.macSize) {
|
|
||||||
throw new InvalidCipherTextException("data too short");
|
|
||||||
}
|
|
||||||
extra -= this.macSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (extra > 0) {
|
|
||||||
byte[] tmp = new byte[BLOCK_SIZE];
|
|
||||||
System.arraycopy(this.bufBlock, 0, tmp, 0, extra);
|
|
||||||
gCTRBlock(tmp, extra, out);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Final gHASH
|
|
||||||
byte[] X = new byte[16];
|
|
||||||
packLength((long) this.A.length * 8, X, 0);
|
|
||||||
packLength(this.totalLength * 8, X, 8);
|
|
||||||
|
|
||||||
xor(this.S, X);
|
|
||||||
this.multiplier.multiplyH(this.S);
|
|
||||||
|
|
||||||
// TODO Fix this if tagLength becomes configurable
|
|
||||||
// T = MSBt(GCTRk(J0,S))
|
|
||||||
byte[] tag = new byte[BLOCK_SIZE];
|
|
||||||
this.cipher.processBlock(this.J0, 0, tag, 0);
|
|
||||||
xor(tag, this.S);
|
|
||||||
|
|
||||||
int resultLen = extra;
|
|
||||||
|
|
||||||
// We place into macBlock our calculated value for T
|
|
||||||
this.macBlock = new byte[this.macSize];
|
|
||||||
System.arraycopy(tag, 0, this.macBlock, 0, this.macSize);
|
|
||||||
|
|
||||||
if (this.forEncryption) {
|
|
||||||
// Append T to the message
|
|
||||||
out.writeBytes(this.macBlock, 0, this.macSize);
|
|
||||||
resultLen += this.macSize;
|
|
||||||
} else {
|
|
||||||
// Retrieve the T value from the message and compare to calculated
|
|
||||||
// one
|
|
||||||
byte[] msgMac = new byte[this.macSize];
|
|
||||||
System.arraycopy(this.bufBlock, extra, msgMac, 0, this.macSize);
|
|
||||||
if (!Arrays.constantTimeAreEqual(this.macBlock, msgMac)) {
|
|
||||||
throw new InvalidCipherTextException("mac check in GCM failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
reset(false);
|
|
||||||
|
|
||||||
return resultLen;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void reset() {
|
|
||||||
reset(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void reset(boolean clearMac) {
|
|
||||||
this.S = this.initS != null ? this.initS.copy() : Unpooled.buffer();
|
|
||||||
this.counter = Arrays.clone(this.J0);
|
|
||||||
this.bufOff = 0;
|
|
||||||
this.totalLength = 0;
|
|
||||||
|
|
||||||
if (this.bufBlock != null) {
|
|
||||||
Arrays.fill(this.bufBlock, (byte) 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (clearMac) {
|
|
||||||
this.macBlock = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.cipher.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
// MODIFIED
|
|
||||||
private void gCTRBlock(byte[] buf, int bufCount, ByteBuf out) {
|
|
||||||
// inc(counter);
|
|
||||||
for (int i = 15; i >= 12; --i) {
|
|
||||||
byte b = (byte) (this.counter[i] + 1 & 0xff);
|
|
||||||
this.counter[i] = b;
|
|
||||||
|
|
||||||
if (b != 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] tmp = new byte[BLOCK_SIZE];
|
|
||||||
this.cipher.processBlock(this.counter, 0, tmp, 0);
|
|
||||||
|
|
||||||
byte[] hashBytes;
|
|
||||||
if (this.forEncryption) {
|
|
||||||
System.arraycopy(ZEROES, bufCount, tmp, bufCount, BLOCK_SIZE - bufCount);
|
|
||||||
hashBytes = tmp;
|
|
||||||
} else {
|
|
||||||
hashBytes = buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = bufCount - 1; i >= 0; --i) {
|
|
||||||
tmp[i] ^= buf[i];
|
|
||||||
}
|
|
||||||
out.writeBytes(tmp, 0, bufCount);
|
|
||||||
|
|
||||||
// gHASHBlock(hashBytes);
|
|
||||||
xor(this.S, hashBytes);
|
|
||||||
this.multiplier.multiplyH(this.S);
|
|
||||||
|
|
||||||
this.totalLength += bufCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
private byte[] gHASH(byte[] b)
|
|
||||||
{
|
|
||||||
byte[] Y = new byte[16];
|
|
||||||
|
|
||||||
for (int pos = 0; pos < b.length; pos += 16)
|
|
||||||
{
|
|
||||||
byte[] X = new byte[16];
|
|
||||||
int num = Math.min(b.length - pos, 16);
|
|
||||||
System.arraycopy(b, pos, X, 0, num);
|
|
||||||
xor(Y, X);
|
|
||||||
this.multiplier.multiplyH(Y);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Y;
|
|
||||||
}
|
|
||||||
|
|
||||||
// private void gHASHBlock(byte[] block)
|
|
||||||
// {
|
|
||||||
// xor(S, block);
|
|
||||||
// multiplier.multiplyH(S);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// private static void inc(byte[] block)
|
|
||||||
// {
|
|
||||||
// for (int i = 15; i >= 12; --i)
|
|
||||||
// {
|
|
||||||
// byte b = (byte)((block[i] + 1) & 0xff);
|
|
||||||
// block[i] = b;
|
|
||||||
//
|
|
||||||
// if (b != 0)
|
|
||||||
// {
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
private static void xor(ByteBuf block, byte[] val) {
|
|
||||||
for (int i = 15; i >= 0; --i) {
|
|
||||||
byte b = (byte) (block.getByte(i) ^ val[i]);
|
|
||||||
block.setByte(i, b);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void xor(byte[] block, ByteBuf val) {
|
|
||||||
for (int i = 15; i >= 0; --i) {
|
|
||||||
block[i] ^= val.getByte(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void xor(byte[] block, byte[] val) {
|
|
||||||
for (int i = 15; i >= 0; --i) {
|
|
||||||
block[i] ^= val[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void packLength(long count, byte[] bs, int off) {
|
|
||||||
Pack.intToBigEndian((int)(count >>> 32), bs, off);
|
|
||||||
Pack.intToBigEndian((int)count, bs, off + 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
// MODIFIED
|
|
||||||
@Override
|
|
||||||
public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff) throws DataLengthException {
|
|
||||||
// DO NOTHING
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// MODIFIED
|
|
||||||
@Override
|
|
||||||
public int doFinal(byte[] out, int outOff) throws IllegalStateException, InvalidCipherTextException {
|
|
||||||
// DO NOTHING
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void processAADByte(byte arg0) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void processAADBytes(byte[] arg0, int arg1, int arg2) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,191 +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.
|
|
||||||
*
|
|
||||||
* Copyright (c) 2000 - 2015 The Legion of the Bouncy Castle Inc.
|
|
||||||
* (http://www.bouncycastle.org)
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
* copy of this software and associated documentation files (the "Software"),
|
|
||||||
* to deal in the Software without restriction, including without limitation
|
|
||||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
||||||
* and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
* Software is furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included
|
|
||||||
* in all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
* DEALINGS IN THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
package dorkbox.util.crypto.bouncycastle;
|
|
||||||
|
|
||||||
import org.bouncycastle.util.Arrays;
|
|
||||||
import org.bouncycastle.util.Pack;
|
|
||||||
|
|
||||||
public abstract class GCMUtil_ByteBuf
|
|
||||||
{
|
|
||||||
static byte[] oneAsBytes()
|
|
||||||
{
|
|
||||||
byte[] tmp = new byte[16];
|
|
||||||
tmp[0] = (byte)0x80;
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int[] oneAsInts()
|
|
||||||
{
|
|
||||||
int[] tmp = new int[4];
|
|
||||||
tmp[0] = 0x80000000;
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int[] asInts(byte[] bs)
|
|
||||||
{
|
|
||||||
int[] us = new int[4];
|
|
||||||
us[0] = Pack.bigEndianToInt(bs, 0);
|
|
||||||
us[1] = Pack.bigEndianToInt(bs, 4);
|
|
||||||
us[2] = Pack.bigEndianToInt(bs, 8);
|
|
||||||
us[3] = Pack.bigEndianToInt(bs, 12);
|
|
||||||
return us;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void multiply(byte[] block, byte[] val)
|
|
||||||
{
|
|
||||||
byte[] tmp = Arrays.clone(block);
|
|
||||||
byte[] c = new byte[16];
|
|
||||||
|
|
||||||
for (int i = 0; i < 16; ++i)
|
|
||||||
{
|
|
||||||
byte bits = val[i];
|
|
||||||
for (int j = 7; j >= 0; --j)
|
|
||||||
{
|
|
||||||
if ((bits & 1 << j) != 0)
|
|
||||||
{
|
|
||||||
xor(c, tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean lsb = (tmp[15] & 1) != 0;
|
|
||||||
shiftRight(tmp);
|
|
||||||
if (lsb)
|
|
||||||
{
|
|
||||||
// R = new byte[]{ 0xe1, ... };
|
|
||||||
// GCMUtil.xor(v, R);
|
|
||||||
tmp[0] ^= (byte)0xe1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
System.arraycopy(c, 0, block, 0, 16);
|
|
||||||
}
|
|
||||||
|
|
||||||
// P is the value with only bit i=1 set
|
|
||||||
static void multiplyP(int[] x)
|
|
||||||
{
|
|
||||||
boolean lsb = (x[3] & 1) != 0;
|
|
||||||
shiftRight(x);
|
|
||||||
if (lsb)
|
|
||||||
{
|
|
||||||
// R = new int[]{ 0xe1000000, 0, 0, 0 };
|
|
||||||
// xor(v, R);
|
|
||||||
x[0] ^= 0xe1000000;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void multiplyP8(int[] x)
|
|
||||||
{
|
|
||||||
// for (int i = 8; i != 0; --i)
|
|
||||||
// {
|
|
||||||
// multiplyP(x);
|
|
||||||
// }
|
|
||||||
|
|
||||||
int lsw = x[3];
|
|
||||||
shiftRightN(x, 8);
|
|
||||||
for (int i = 7; i >= 0; --i)
|
|
||||||
{
|
|
||||||
if ((lsw & 1 << i) != 0)
|
|
||||||
{
|
|
||||||
x[0] ^= 0xe1000000 >>> 7 - i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void shiftRight(byte[] block)
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
int bit = 0;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
int b = block[i] & 0xff;
|
|
||||||
block[i] = (byte) (b >>> 1 | bit);
|
|
||||||
if (++i == 16)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
bit = (b & 1) << 7;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void shiftRight(int[] block)
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
int bit = 0;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
int b = block[i];
|
|
||||||
block[i] = b >>> 1 | bit;
|
|
||||||
if (++i == 4)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
bit = b << 31;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void shiftRightN(int[] block, int n)
|
|
||||||
{
|
|
||||||
int i = 0;
|
|
||||||
int bits = 0;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
int b = block[i];
|
|
||||||
block[i] = b >>> n | bits;
|
|
||||||
if (++i == 4)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
bits = b << 32 - n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void xor(byte[] block, byte[] val)
|
|
||||||
{
|
|
||||||
for (int i = 15; i >= 0; --i)
|
|
||||||
{
|
|
||||||
block[i] ^= val[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void xor(int[] block, int[] val)
|
|
||||||
{
|
|
||||||
for (int i = 3; i >= 0; --i)
|
|
||||||
{
|
|
||||||
block[i] ^= val[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,174 +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.
|
|
||||||
*
|
|
||||||
* Copyright (c) 2000 - 2015 The Legion of the Bouncy Castle Inc.
|
|
||||||
* (http://www.bouncycastle.org)
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
* copy of this software and associated documentation files (the "Software"),
|
|
||||||
* to deal in the Software without restriction, including without limitation
|
|
||||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
||||||
* and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
* Software is furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included
|
|
||||||
* in all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
* DEALINGS IN THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
package dorkbox.util.crypto.bouncycastle;
|
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
|
||||||
import org.bouncycastle.util.Pack;
|
|
||||||
|
|
||||||
public class Tables8kGCMMultiplier_ByteBuf
|
|
||||||
{
|
|
||||||
private final int[][][] M = new int[32][16][];
|
|
||||||
|
|
||||||
public void init(byte[] H)
|
|
||||||
{
|
|
||||||
this.M[0][0] = new int[4];
|
|
||||||
this.M[1][0] = new int[4];
|
|
||||||
this.M[1][8] = GCMUtil_ByteBuf.asInts(H);
|
|
||||||
|
|
||||||
for (int j = 4; j >= 1; j >>= 1)
|
|
||||||
{
|
|
||||||
int[] tmp = new int[4];
|
|
||||||
System.arraycopy(this.M[1][j + j], 0, tmp, 0, 4);
|
|
||||||
|
|
||||||
GCMUtil_ByteBuf.multiplyP(tmp);
|
|
||||||
this.M[1][j] = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
int[] tmp = new int[4];
|
|
||||||
System.arraycopy(this.M[1][1], 0, tmp, 0, 4);
|
|
||||||
|
|
||||||
GCMUtil_ByteBuf.multiplyP(tmp);
|
|
||||||
this.M[0][8] = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int j = 4; j >= 1; j >>= 1)
|
|
||||||
{
|
|
||||||
int[] tmp = new int[4];
|
|
||||||
System.arraycopy(this.M[0][j + j], 0, tmp, 0, 4);
|
|
||||||
|
|
||||||
GCMUtil_ByteBuf.multiplyP(tmp);
|
|
||||||
this.M[0][j] = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
for (int j = 2; j < 16; j += j)
|
|
||||||
{
|
|
||||||
for (int k = 1; k < j; ++k)
|
|
||||||
{
|
|
||||||
int[] tmp = new int[4];
|
|
||||||
System.arraycopy(this.M[i][j], 0, tmp, 0, 4);
|
|
||||||
|
|
||||||
GCMUtil_ByteBuf.xor(tmp, this.M[i][k]);
|
|
||||||
this.M[i][j + k] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (++i == 32)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i > 1)
|
|
||||||
{
|
|
||||||
this.M[i][0] = new int[4];
|
|
||||||
for(int j = 8; j > 0; j >>= 1)
|
|
||||||
{
|
|
||||||
int[] tmp = new int[4];
|
|
||||||
System.arraycopy(this.M[i - 2][j], 0, tmp, 0, 4);
|
|
||||||
|
|
||||||
GCMUtil_ByteBuf.multiplyP8(tmp);
|
|
||||||
this.M[i][j] = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void multiplyH(byte[] x)
|
|
||||||
{
|
|
||||||
// assert x.Length == 16;
|
|
||||||
|
|
||||||
int[] z = new int[4];
|
|
||||||
for (int i = 15; i >= 0; --i)
|
|
||||||
{
|
|
||||||
// GCMUtil.xor(z, M[i + i][x[i] & 0x0f]);
|
|
||||||
int[] m = this.M[i + i][x[i] & 0x0f];
|
|
||||||
z[0] ^= m[0];
|
|
||||||
z[1] ^= m[1];
|
|
||||||
z[2] ^= m[2];
|
|
||||||
z[3] ^= m[3];
|
|
||||||
// GCMUtil.xor(z, M[i + i + 1][(x[i] & 0xf0) >>> 4]);
|
|
||||||
m = this.M[i + i + 1][(x[i] & 0xf0) >>> 4];
|
|
||||||
z[0] ^= m[0];
|
|
||||||
z[1] ^= m[1];
|
|
||||||
z[2] ^= m[2];
|
|
||||||
z[3] ^= m[3];
|
|
||||||
}
|
|
||||||
|
|
||||||
Pack.intToBigEndian(z, x, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void multiplyH(ByteBuf x)
|
|
||||||
{
|
|
||||||
// assert x.Length == 16;
|
|
||||||
|
|
||||||
int[] z = new int[4];
|
|
||||||
for (int i = 15; i >= 0; --i)
|
|
||||||
{
|
|
||||||
// GCMUtil.xor(z, M[i + i][x[i] & 0x0f]);
|
|
||||||
int[] m = this.M[i + i][x.getByte(i) & 0x0f];
|
|
||||||
z[0] ^= m[0];
|
|
||||||
z[1] ^= m[1];
|
|
||||||
z[2] ^= m[2];
|
|
||||||
z[3] ^= m[3];
|
|
||||||
// GCMUtil.xor(z, M[i + i + 1][(x[i] & 0xf0) >>> 4]);
|
|
||||||
m = this.M[i + i + 1][(x.getByte(i) & 0xf0) >>> 4];
|
|
||||||
z[0] ^= m[0];
|
|
||||||
z[1] ^= m[1];
|
|
||||||
z[2] ^= m[2];
|
|
||||||
z[3] ^= m[3];
|
|
||||||
}
|
|
||||||
|
|
||||||
intToBigEndian(z, x, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void intToBigEndian(int[] ns, ByteBuf bs, int off) {
|
|
||||||
for (int i = 0; i < ns.length; ++i) {
|
|
||||||
intToBigEndian(ns[i], bs, off);
|
|
||||||
off += 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void intToBigEndian(int n, ByteBuf bs, int off) {
|
|
||||||
bs.setByte(off, (byte) (n >>> 24));
|
|
||||||
bs.setByte(++off, (byte) (n >>> 16));
|
|
||||||
bs.setByte(++off, (byte) (n >>> 8));
|
|
||||||
bs.setByte(++off, (byte) n);
|
|
||||||
}
|
|
||||||
}
|
|
@ -34,7 +34,6 @@ import java.io.IOException;
|
|||||||
public
|
public
|
||||||
class BcECDSAContentVerifierProviderBuilder extends BcContentVerifierProviderBuilder {
|
class BcECDSAContentVerifierProviderBuilder extends BcContentVerifierProviderBuilder {
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public
|
public
|
||||||
BcECDSAContentVerifierProviderBuilder(DigestAlgorithmIdentifierFinder digestAlgorithmFinder) {
|
BcECDSAContentVerifierProviderBuilder(DigestAlgorithmIdentifierFinder digestAlgorithmFinder) {
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,6 @@ import java.awt.*;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public
|
public
|
||||||
class PropertiesProvider {
|
class PropertiesProvider {
|
||||||
|
|
||||||
|
@ -33,7 +33,6 @@ import java.util.concurrent.locks.ReentrantLock;
|
|||||||
* <p/>
|
* <p/>
|
||||||
* Be wary of opening the database file in different JVM instances. Even with file-locks, you can corrupt the data.
|
* Be wary of opening the database file in different JVM instances. Even with file-locks, you can corrupt the data.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
|
||||||
class DiskStorage implements Storage {
|
class DiskStorage implements Storage {
|
||||||
private final DelayTimer timer;
|
private final DelayTimer timer;
|
||||||
private final ByteArrayWrapper defaultKey;
|
private final ByteArrayWrapper defaultKey;
|
||||||
|
@ -23,13 +23,10 @@ import dorkbox.util.bytes.ByteArrayWrapper;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.nio.channels.FileLock;
|
import java.nio.channels.FileLock;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public
|
public
|
||||||
class Metadata {
|
class Metadata {
|
||||||
// The length of a key in the index.
|
// The length of a key in the index.
|
||||||
|
@ -23,7 +23,6 @@ import java.io.IOException;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public
|
public
|
||||||
interface Storage {
|
interface Storage {
|
||||||
/**
|
/**
|
||||||
@ -87,7 +86,6 @@ interface Storage {
|
|||||||
* @param key The key used to check if data already exists.
|
* @param key The key used to check if data already exists.
|
||||||
* @param data This is the default value, and if there is no value with the key in the DB this default value will be saved.
|
* @param data This is the default value, and if there is no value with the key in the DB this default value will be saved.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
<T> T getAndPut(ByteArrayWrapper key, T data) throws IOException;
|
<T> T getAndPut(ByteArrayWrapper key, T data) throws IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,7 +25,6 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@ -46,7 +45,6 @@ import java.util.concurrent.locks.ReentrantLock;
|
|||||||
// Also, file locks on linux are ADVISORY. if an app doesn't care about locks, then it can do stuff -- even if locked by another app
|
// Also, file locks on linux are ADVISORY. if an app doesn't care about locks, then it can do stuff -- even if locked by another app
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
class StorageBase {
|
class StorageBase {
|
||||||
private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());
|
private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());
|
||||||
|
|
||||||
|
@ -45,7 +45,6 @@ import static org.junit.Assert.*;
|
|||||||
|
|
||||||
public class UByteTest {
|
public class UByteTest {
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testValueOfByte() {
|
public void testValueOfByte() {
|
||||||
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
|
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
|
||||||
@ -53,7 +52,6 @@ public class UByteTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testValueOfByteCaching() {
|
public void testValueOfByteCaching() {
|
||||||
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
|
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
|
||||||
@ -63,7 +61,6 @@ public class UByteTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testValueOfShort() {
|
public void testValueOfShort() {
|
||||||
for (int i = UByte.MIN_VALUE; i <= UByte.MAX_VALUE; i++) {
|
for (int i = UByte.MIN_VALUE; i <= UByte.MAX_VALUE; i++) {
|
||||||
@ -71,7 +68,6 @@ public class UByteTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testValueOfShortInvalid() {
|
public void testValueOfShortInvalid() {
|
||||||
try {
|
try {
|
||||||
@ -86,7 +82,6 @@ public class UByteTest {
|
|||||||
catch (NumberFormatException e) {}
|
catch (NumberFormatException e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testValueOfInt() {
|
public void testValueOfInt() {
|
||||||
for (int i = UByte.MIN_VALUE; i <= UByte.MAX_VALUE; i++) {
|
for (int i = UByte.MIN_VALUE; i <= UByte.MAX_VALUE; i++) {
|
||||||
@ -94,7 +89,6 @@ public class UByteTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testValueOfIntInvalid() {
|
public void testValueOfIntInvalid() {
|
||||||
try {
|
try {
|
||||||
@ -109,7 +103,6 @@ public class UByteTest {
|
|||||||
catch (NumberFormatException e) {}
|
catch (NumberFormatException e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testValueOfLong() {
|
public void testValueOfLong() {
|
||||||
for (int i = UByte.MIN_VALUE; i <= UByte.MAX_VALUE; i++) {
|
for (int i = UByte.MIN_VALUE; i <= UByte.MAX_VALUE; i++) {
|
||||||
@ -117,7 +110,6 @@ public class UByteTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testValueOfLongInvalid() {
|
public void testValueOfLongInvalid() {
|
||||||
try {
|
try {
|
||||||
@ -132,7 +124,6 @@ public class UByteTest {
|
|||||||
catch (NumberFormatException e) {}
|
catch (NumberFormatException e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testSerializeDeserialize() throws ClassNotFoundException, IOException {
|
public void testSerializeDeserialize() throws ClassNotFoundException, IOException {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
@ -48,7 +48,6 @@ class UIntegerTest {
|
|||||||
private static final int CACHE_SIZE = 256;
|
private static final int CACHE_SIZE = 256;
|
||||||
private static final int NEAR_MISS_OFFSET = 4;
|
private static final int NEAR_MISS_OFFSET = 4;
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public
|
public
|
||||||
void testValueOfLong() {
|
void testValueOfLong() {
|
||||||
@ -57,7 +56,6 @@ class UIntegerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public
|
public
|
||||||
void testValueOfLongCachingShift() {
|
void testValueOfLongCachingShift() {
|
||||||
@ -68,7 +66,6 @@ class UIntegerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public
|
public
|
||||||
void testValueOfLongCachingNear() {
|
void testValueOfLongCachingNear() {
|
||||||
@ -79,7 +76,6 @@ class UIntegerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public
|
public
|
||||||
void testValueOfLongNoCachingShift() {
|
void testValueOfLongNoCachingShift() {
|
||||||
@ -90,7 +86,6 @@ class UIntegerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public
|
public
|
||||||
void testValueOfLongNoCachingNear() {
|
void testValueOfLongNoCachingNear() {
|
||||||
@ -101,7 +96,6 @@ class UIntegerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public
|
public
|
||||||
void testValueOfLongInvalid() {
|
void testValueOfLongInvalid() {
|
||||||
@ -117,7 +111,6 @@ class UIntegerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public
|
public
|
||||||
void testSerializeDeserialize() throws ClassNotFoundException, IOException {
|
void testSerializeDeserialize() throws ClassNotFoundException, IOException {
|
||||||
|
@ -169,7 +169,6 @@ public class UNumberTest {
|
|||||||
testCastable(ULong.MAX_VALUE, ulong(ULong.MAX_VALUE));
|
testCastable(ULong.MAX_VALUE, ulong(ULong.MAX_VALUE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
public void testObjectMethods() {
|
public void testObjectMethods() {
|
||||||
assertEquals(ubyte((byte) 0), ubyte((byte) 0));
|
assertEquals(ubyte((byte) 0), ubyte((byte) 0));
|
||||||
@ -226,7 +225,7 @@ public class UNumberTest {
|
|||||||
|
|
||||||
// Test utility methods
|
// Test utility methods
|
||||||
|
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
|
@SuppressWarnings({ "rawtypes", "unchecked"})
|
||||||
private void testComparable(List<String> strings, UNumber... numbers) {
|
private void testComparable(List<String> strings, UNumber... numbers) {
|
||||||
List<UNumber> list = new ArrayList<UNumber>(asList(numbers));
|
List<UNumber> list = new ArrayList<UNumber>(asList(numbers));
|
||||||
Collections.sort((List) list);
|
Collections.sort((List) list);
|
||||||
|
Loading…
Reference in New Issue
Block a user