public final class CryptoUtils
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static byte[] |
addModBlockSize(int blocksize,
byte[] unsignedBigInt1,
byte[] unsignedBigInt2)
Add the given two unsigned integers.
|
static int |
compareBlock(byte[] a,
byte[] b)
Checks two byte blocks for equality.
|
static int |
compareBlock(byte[] a,
int aOff,
byte[] b,
int bOff,
int len)
Check the specified sub-sequences of the given byte arrays for equality.
|
static int |
compareBlock(int[] a,
int[] b)
Checks two integer blocks for equality.
|
static int |
compareBlock(int[] a,
int aOff,
int[] b,
int bOff,
int len)
Check the specified sub-sequences of the given integer arrays for equality.
|
static int |
compareBlock(long[] a,
int aOff,
long[] b,
int bOff,
int len)
Check the specified sub-sequences of the given long integer arrays for
equality.
|
static int |
compareBlock(long[] a,
long[] b)
Checks two long integer blocks for equality.
|
static byte[] |
concatenate(byte[] a1,
byte[] a2)
Concatenate the given byte arrays.
|
static int[] |
concatenate(int[] a1,
int[] a2)
Concatenate the given integer arrays.
|
static long[] |
concatenate(long[] a1,
long[] a2)
Concatenate the given long integer arrays.
|
static void |
copyBlock(byte[] src,
byte[] dst)
Copies one byte block to another.
|
static void |
copyBlock(byte[] src,
int srcOff,
byte[] dst,
int dstOff,
int len)
Copies the specified byte sequence of the given source array to the
specified destination array.
|
static void |
copyBlock(int[] src,
int[] dst)
Copies one integer block to another.
|
static void |
copyBlock(int[] src,
int srcOff,
int[] dst,
int dstOff,
int len)
Copies the specified integer sequence of the given source array to the
specified destination array.
|
static void |
copyBlock(long[] src,
int srcOff,
long[] dst,
int dstOff,
int len)
Copies the specified long sequence of the given source array to the
specified destination array.
|
static void |
copyBlock(long[] src,
long[] dst)
Copies one long block to another.
|
static boolean |
equalsBlock(byte[] a,
byte[] b)
Checks two byte blocks for equality.
|
static boolean |
equalsBlock(byte[] a,
int aOff,
byte[] b,
int bOff,
int len)
Check the specified sub-sequences of the given byte arrays for equality.
|
static boolean |
equalsBlock(int[] a,
int[] b)
Checks two integer blocks for equality.
|
static boolean |
equalsBlock(int[] a,
int aOff,
int[] b,
int bOff,
int len)
Check the specified sub-sequences of the given integer arrays for equality.
|
static boolean |
equalsBlock(long[] a,
int aOff,
long[] b,
int bOff,
int len)
Check the specified sub-sequences of the given long integer arrays for
equality.
|
static boolean |
equalsBlock(long[] a,
long[] b)
Checks two long integer blocks for equality.
|
static int[] |
extGcd(int a,
int b)
Deprecated.
use NumberTheory.extGcd() instead
|
static void |
fillBlock(byte[] block,
byte b)
Fills a block with a given byte.
|
static void |
fillBlock(byte[] block,
int blockOff,
byte b,
int len)
Fills the specified subsequence of the given byte array with the given
byte.
|
static void |
fillBlock(int[] block,
int b)
Fills a block with a given integer.
|
static void |
fillBlock(int[] block,
int blockOff,
int b,
int len)
Fills the specified subsequence of the given integer array with the given
integer.
|
static void |
fillBlock(long[] block,
int blockOff,
long b,
int len)
Fills the specified subsequence of the given long integer array with the
given long.
|
static void |
fillBlock(long[] block,
long b)
Fills a block with a given long integer.
|
static int |
gcd(int a,
int b)
Deprecated.
use NumberTheory.gcd() instead
|
static java.math.BigInteger |
getStrongPrime(int x,
java.util.Random random)
Deprecated.
use NumberTheory.getStrongPrime() instead
|
static byte[] |
increment(byte[] b)
Increment the byte array by one.
|
static byte[] |
incrementExtended(byte[] number)
This method increments the given byte array and return the result.
|
static void |
randomBlock(byte[] block)
Fills the given byte array with random bytes.
|
static void |
randomBlock(byte[] block,
int off,
int len)
Fills the specified sub-array of the given byte array with random bytes.
|
static byte[] |
resizeArray(byte[] array,
int len)
Deprecated.
Use the method in iaik.utils.Util instead.
|
static void |
reverseArray(byte[] array,
int off,
int len)
Reverses the order of the bytes in the given array.
|
static boolean |
secureEqualsBlock(byte[] a,
byte[] b)
Checks two byte blocks for equality.
|
static boolean |
secureEqualsBlock(byte[] a,
int aOff,
byte[] b,
int bOff,
int len)
Check the specified sub-sequences of the given byte arrays for equality.
|
static boolean |
secureEqualsBlock(int[] a,
int[] b)
Checks two integer blocks for equality.
|
static boolean |
secureEqualsBlock(int[] a,
int aOff,
int[] b,
int bOff,
int len)
Check the specified sub-sequences of the given integer arrays for equality.
|
static boolean |
secureEqualsBlock(long[] a,
int aOff,
long[] b,
int bOff,
int len)
Check the specified sub-sequences of the given long integer arrays for
equality.
|
static boolean |
secureEqualsBlock(long[] a,
long[] b)
Checks two long integer blocks for equality.
|
static byte[] |
shiftLeft(byte[] v)
Shift left the bits in the given byte array by one position.
|
static int[] |
shiftLeft(int[] v)
Shift left the bits in the given integer array by one position.
|
static long[] |
shiftLeft(long[] v)
Shift left the bits in the given long integer array by one position.
|
static byte[] |
shiftRight(byte[] v)
Shift right the bits in the given byte array by one position.
|
static int[] |
shiftRight(int[] v)
Shift right the bits in the given integer array by one position.
|
static long[] |
shiftRight(long[] v)
Shift right the bits in the given long integer array by one position.
|
static void |
spreadIntsToBytes(int[] inInts,
int inOff,
byte[] outBytes,
int outOff,
int intLen)
Spreads ints into bytes.
|
static void |
spreadIntsToBytesLE(int[] inInts,
int inOff,
byte[] outBytes,
int outOff,
int intLen)
Spreads ints into bytes in little endian bytes ordering.
|
static void |
spreadIntToBytes(int in,
byte[] out,
int offset)
Spread one int int bytes in big endian byte order.
|
static void |
spreadIntToBytesLE(int in,
byte[] out,
int offset)
Spread one int int bytes in little endian byte order.
|
static void |
spreadLongsToBytes(long[] inLongs,
int inOff,
byte[] outBytes,
int outOff,
int longLen)
Spreads long integers into bytes.
|
static void |
spreadLongsToBytesLE(long[] inLongs,
int inOff,
byte[] outBytes,
int outOff,
int longLen)
Spreads longs into bytes in little endian bytes ordering.
|
static void |
spreadShortsToBytesBE(int[] inShorts,
int inOff,
byte[] outBytes,
int outOff,
int shortLen)
Spreads shorts into bytes in big-endian.
|
static void |
spreadShortsToBytesLE(int[] inShorts,
int inOff,
byte[] outBytes,
int outOff,
int shortLen)
Spreads shorts into bytes in little-endian.
|
static int |
squashBytesToInt(byte[] inBytes,
int inOff)
Squashes four bytes down to one int in big endian byte order.
|
static void |
squashBytesToInts(byte[] inBytes,
int inOff,
int[] outInts,
int outOff,
int intLen)
Squashes bytes down to ints.
|
static void |
squashBytesToIntsLE(byte[] inBytes,
int inOff,
int[] outInts,
int outOff,
int intLen)
Squashes bytes down to ints assuming little endian byte ordering.
|
static int |
squashBytesToIntsLEPadZero(byte[] inBytes,
int inOff,
int[] outInts,
int outOff,
int byteLen) |
static void |
squashBytesToIntsPadZero(byte[] inBytes,
int inOff,
int[] outInts,
int outOff,
int byteLen) |
static void |
squashBytesToLongs(byte[] inBytes,
int inOff,
long[] outLongs,
int outOff,
int longLen)
Squashes bytes down to longs.
|
static void |
squashBytesToLongsLE(byte[] inBytes,
int inOff,
long[] outLongs,
int outOff,
int longLen)
Squashes bytes down to longs assuming little endian byte ordering.
|
static void |
squashBytesToShortsBE(byte[] inBytes,
int inOff,
int[] outShorts,
int outOff,
int shortLen)
Squashes bytes down to shorts in big-endian.
|
static void |
squashBytesToShortsLE(byte[] inBytes,
int inOff,
int[] outShorts,
int outOff,
int shortLen)
Squashes bytes down to shorts in little-endian.
|
static void |
xorBlock(byte[] a,
byte[] b,
byte[] dst)
XORs two byte blocks.
|
static void |
xorBlock(byte[] a,
int aOff,
byte[] b,
int bOff,
byte[] dst,
int dstOff,
int len)
XORs the specified sub-arrays of the given byte blocks.
|
static void |
xorBlock(int[] a,
int[] b,
int[] dst)
XORs two integer blocks.
|
static void |
xorBlock(int[] a,
int aOff,
int[] b,
int bOff,
int[] dst,
int dstOff,
int len)
XORs the specified sub-arrays of the given integer blocks.
|
static void |
xorBlock(long[] a,
int aOff,
long[] b,
int bOff,
long[] dst,
int dstOff,
int len)
XORs the specified sub-arrays of the given long integer blocks.
|
static void |
xorBlock(long[] a,
long[] b,
long[] dst)
XORs two long integer blocks.
|
static void |
zeroBlock(byte[] block)
Fills the given byte array with zeros.
|
static void |
zeroBlock(byte[] block,
int off,
int len)
Fills the specified sub-array of the given byte array with zeros.
|
static void |
zeroBlock(int[] block)
Fill an integer array with zeros.
|
static void |
zeroBlock(int[] block,
int off,
int len)
Fill part of an integer array with zeros.
|
static void |
zeroBlock(long[] block)
Fill a long array with zeros.
|
static void |
zeroBlock(long[] block,
int off,
int len)
Fill part of a long array with zeros.
|
public static void zeroBlock(byte[] block, int off, int len)
Starting at the given off
position, len
bytes of
the given array are set to zero. To, for instance, set three bytes to zero,
starting at position 2, use:
byte[] block = ...; CryptoUtils.zeroBlock(block, 2, 3);
block
- the byte array of which some bytes have to be set to zerooff
- the offset indicating the start position within the byte array;
the following len
bytes are set to zerolen
- the number of bytes to be set to zero, starting at
off
public static void zeroBlock(int[] block, int off, int len)
public static void zeroBlock(long[] block, int off, int len)
public static void zeroBlock(byte[] block)
block
- the byte array to be filled with zerospublic static void zeroBlock(int[] block)
public static void zeroBlock(long[] block)
public static void randomBlock(byte[] block, int off, int len)
This method uses Math.random()
for generating random numbers.
Starting at the given off
position, len
bytes of
the given array are randomly generated. To, for instance, set ten bytes at
random, starting at position 2, use:
byte[] block = new byte[20]; CryptoUtils.randomBlock(block, 2, 10);As more secure alternative you may use a SecureRandom PRNG.
block
- the byte array of which some bytes shall be randomly generatedoff
- the offset indicating the start position within the byte array;
the following len
bytes will be randomly generatedlen
- the number of bytes to be randomly generated, starting at
off
public static void randomBlock(byte[] block)
This method uses Math.random()
for generating random numbers, e.g.:
byte[] block = new byte[20]; CryptoUtils.randomBlock(block);As more secure alternative you may use a SecureRandom PRNG:
SecureRandom rand = ...; byte[] block = new byte[20]; rand.nextBytes(block);
block
- the byte array to be filled with random bytespublic static void xorBlock(byte[] a, int aOff, byte[] b, int bOff, byte[] dst, int dstOff, int len)
Beginning at aOff
, len
bytes of the given
a
array are XORed with the corresponding bytes of the
b
array, beginning at position bOff
. The result
is written to the destination byte array (dst
), starting at
position dstOff
, e.g.:
byte[] a = ...; byte[] b = ...; byte[] dst = new byte[8]; CryptoUtils.xorBlock(a, 0, b, 0, dst, 0, 8);
a
- the first byte array supplying bytes to be XORedaOff
- the offset indicating the start position within the first byte
array; the following len
bytes will be XORed with the
corresponding number of bytes of the second byte arrayb
- the second byte array supplying bytes to be XORedbOff
- the offset indicating the start position within the second byte
array; the following len
bytes will be XORed with the
corresponding number of bytes of the first byte arraydst
- the destination byte array to which the result of the XOR
operation is writtendstOff
- the offset indicating the start position within the destination
byte array, to which the result of the XOR operation is writtenlen
- the number of bytes to be XORedpublic static void xorBlock(int[] a, int aOff, int[] b, int bOff, int[] dst, int dstOff, int len)
Beginning at aOff
, len
integers of the given
a
array are XORed with the corresponding integer of the
b
array, beginning at position bOff
. The result
is written to the destination integer array (dst
), starting at
position dstOff
, e.g.:
int[] a = ...; int[] b = ...; int[] dst = new int[8]; CryptoUtils.xorBlock(a, 0, b, 0, dst, 0, 8);
a
- the first integer array supplying integers to be XORedaOff
- the offset indicating the start position within the first integer
array; the following len
integers will be XORed with
the corresponding number of integers of the second integer arrayb
- the second integer array supplying integers to be XORedbOff
- the offset indicating the start position within the second integer
array; the following len
integers will be XORed with
the corresponding number of integers of the first integer arraydst
- the destination integer array to which the result of the XOR
operation is writtendstOff
- the offset indicating the start position within the destination
integer array, to which the result of the XOR operation is writtenlen
- the number of integers to be XORedpublic static void xorBlock(long[] a, int aOff, long[] b, int bOff, long[] dst, int dstOff, int len)
Beginning at aOff
, len
long integers of the given
a
array are XORed with the corresponding long integer of the
b
array, beginning at position bOff
. The result
is written to the destination long integer array (dst
),
starting at position dstOff
, e.g.:
long[] a = ...; long[] b = ...; long[] dst = new long[8]; CryptoUtils.xorBlock(a, 0, b, 0, dst, 0, 8);
a
- the first long integer array supplying long integers to be XORedaOff
- the offset indicating the start position within the first long
integer array; the following len
long integers will
be XORed with the corresponding number of long integers of the
second long integer arrayb
- the second long integer array supplying long integers to be XORedbOff
- the offset indicating the start position within the second long
integer array; the following len
long integers will
be XORed with the corresponding number of longs of the first long
integer arraydst
- the destination long integer array to which the result of the XOR
operation is writtendstOff
- the offset indicating the start position within the destination
long integer array, to which the result of the XOR operation is
writtenlen
- the number of long integers to be XORedpublic static void xorBlock(byte[] a, byte[] b, byte[] dst)
Every byte of the given a
array is XORed with the
corresponding byte of the b
array. The result is written to
the specified destination array, e.g.:
byte[] a = ...; byte[] b = ...; byte[] dst = new byte[8]; CryptoUtils.xorBlock(a, b, dst);
a
- the first byte array to be XORedb
- the second byte array to be XOReddst
- the destination byte array to which the result is writtenpublic static void xorBlock(int[] a, int[] b, int[] dst)
Every integer of the given a
array is XORed with the
corresponding integer of the b
array. The result is written to
the specified destination array, e.g.:
int[] a = ...; int[] b = ...; int[] dst = new int[8]; CryptoUtils.xorBlock(a, b, dst);
a
- the first integer array to be XORedb
- the second integer array to be XOReddst
- the destination integer array to which the result is writtenpublic static void xorBlock(long[] a, long[] b, long[] dst)
Every long integer of the given a
array is XORed with the
corresponding long integer of the b
array. The result is
written to the specified destination array, e.g.:
long[] a = ...; long[] b = ...; long[] dst = new long[8]; CryptoUtils.xorBlock(a, b, dst);
a
- the first long integer array to be XORedb
- the second long integer array to be XOReddst
- the destination long integer array to which the result is writtenpublic static void copyBlock(byte[] src, int srcOff, byte[] dst, int dstOff, int len)
Beginning at the specified srcOff
position, len
bytes of the source array are copied to the given destination array,
starting at dstOff
.
src
- the source byte arraysrcOff
- the offset indicating the start position within the first byte
array; the following len
bytes will be copied to the
destination arraydst
- the destination array to which to copy the bytesdstOff
- the offset indicating the start position within the destination
byte array, to which the bytes shall be copiedlen
- the number of bytes to be copiedpublic static void copyBlock(byte[] src, byte[] dst)
src
- the source byte arraydst
- the destination array to which to copy the bytespublic static void copyBlock(int[] src, int srcOff, int[] dst, int dstOff, int len)
Beginning at the specified srcOff
position, len
integers of the source array are copied to the given destination array,
starting at dstOff
.
src
- the source integer arraysrcOff
- the offset indicating the start position within the first integer
array; the following len
integers will be copied to
the destination arraydst
- the destination array to which to copy the integersdstOff
- the offset indicating the start position within the destination
integer array, to which the integers shall be copiedlen
- the number of integers to be copiedpublic static void copyBlock(int[] src, int[] dst)
src
- the source integer arraydst
- the destination array to which to copy the integerspublic static void copyBlock(long[] src, int srcOff, long[] dst, int dstOff, int len)
Beginning at the specified srcOff
position, len
longs of the source array are copied to the given destination array,
starting at dstOff
.
src
- the source long arraysrcOff
- the offset indicating the start position within the first long
array; the following len
long integers will be copied
to the destination arraydst
- the destination array to which to copy the longsdstOff
- the offset indicating the start position within the destination
long array, to which the longs shall be copiedlen
- the number of longs to be copiedpublic static void copyBlock(long[] src, long[] dst)
src
- the source long integer arraydst
- the destination array to which to copy the long integerspublic static boolean equalsBlock(byte[] a, int aOff, byte[] b, int bOff, int len)
a
- the first byte arrayaOff
- the offset indicating the start position within the first byte
array; the following len
bytes are compared with the
corresponding number of bytes of the second byte arrayb
- the second byte arraybOff
- the offset indicating the start position within the second byte
array; the following len
bytes are compared with the
corresponding number of bytes of the first byte arraylen
- the number of bytes to be comparedtrue
if the two sub-arrays have the same contents,
false
if notpublic static boolean equalsBlock(int[] a, int aOff, int[] b, int bOff, int len)
a
- the first integer arrayaOff
- the offset indicating the start position within the first integer
array; the following len
integers are compared with
the corresponding number of integers of the second integer arrayb
- the second integer arraybOff
- the offset indicating the start position within the second integer
array; the following len
integers are compared with
the corresponding number of integers of the first integer arraylen
- the number of integers to be comparedtrue
if the two sub-arrays have the same contents,
false
if notpublic static boolean equalsBlock(long[] a, int aOff, long[] b, int bOff, int len)
a
- the first long integer arrayaOff
- the offset indicating the start position within the first long
integer array; the following len
long integers are
compared with the corresponding number of long integers of the
second long integer arrayb
- the second long integer arraybOff
- the offset indicating the start position within the second long
integer array; the following len
long integers are
compared with the corresponding number of long integers of the
first long integer arraylen
- the number of long integers to be comparedtrue
if the two sub-arrays have the same contents,
false
if notpublic static boolean equalsBlock(byte[] a, byte[] b)
a
- the first byte array to be compared with the second byte arrayb
- the second byte array to be compared with the first byte arraytrue
if the two blocks have the same contents,
false
if notpublic static boolean equalsBlock(int[] a, int[] b)
a
- the first integer array to be compared with the second integer
arrayb
- the second integer array to be compared with the first integer
arraytrue
if the two blocks have the same contents,
false
if notpublic static boolean equalsBlock(long[] a, long[] b)
a
- the first long array to be compared with the second long integer
arrayb
- the second long array to be compared with the first long integer
arraytrue
if the two blocks have the same contents,
false
if notpublic static boolean secureEqualsBlock(byte[] a, int aOff, byte[] b, int bOff, int len)
len
steps.a
- the first byte arrayaOff
- the offset indicating the start position within the first byte
array; the following len
bytes are compared with the
corresponding number of bytes of the second byte arrayb
- the second byte arraybOff
- the offset indicating the start position within the second byte
array; the following len
bytes are compared with the
corresponding number of bytes of the first byte arraylen
- the number of bytes to be comparedtrue
if the two sub-arrays have the same contents,
false
if notpublic static boolean secureEqualsBlock(int[] a, int aOff, int[] b, int bOff, int len)
len
steps.a
- the first integer arrayaOff
- the offset indicating the start position within the first integer
array; the following len
integers are compared with
the corresponding number of integers of the second integer arrayb
- the second integer arraybOff
- the offset indicating the start position within the second integer
array; the following len
integers are compared with
the corresponding number of integers of the first integer arraylen
- the number of integers to be comparedtrue
if the two sub-arrays have the same contents,
false
if notpublic static boolean secureEqualsBlock(long[] a, int aOff, long[] b, int bOff, int len)
len
steps.a
- the first long integer arrayaOff
- the offset indicating the start position within the first long
integer array; the following len
long integers are
compared with the corresponding number of long integers of the
second long integer arrayb
- the second long integer arraybOff
- the offset indicating the start position within the second long
integer array; the following len
long integers are
compared with the corresponding number of longs of the first long
integer arraylen
- the number of long integers to be comparedtrue
if the two sub-arrays have the same contents,
false
if notpublic static boolean secureEqualsBlock(byte[] a, byte[] b)
len
steps.a
- the first byte array to be compared with the second byte arrayb
- the second byte array to be compared with the first byte arraytrue
if the two blocks have the same contents,
false
if notpublic static boolean secureEqualsBlock(int[] a, int[] b)
len
steps.a
- the first integer array to be compared with the second integer
arrayb
- the second integer array to be compared with the first integer
arraytrue
if the two blocks have the same contents,
false
if notpublic static boolean secureEqualsBlock(long[] a, long[] b)
len
steps.a
- the first long integer array to be compared with the second long
arrayb
- the second long integer array to be compared with the first long
arraytrue
if the two blocks have the same contents,
false
if notpublic static int compareBlock(byte[] a, int aOff, byte[] b, int bOff, int len)
a
- the first byte arrayaOff
- the offset indicating the start position within the first byte
array; the following len
bytes are compared with the
corresponding number of bytes of the second byte arrayb
- the second byte arraybOff
- the offset indicating the start position within the second byte
array; the following len
bytes are compared with the
corresponding number of bytes of the first byte arraylen
- the number of bytes to be compared-1
if the two sub-arrays have the same contents, the
position of the first difference otherwisepublic static int compareBlock(int[] a, int aOff, int[] b, int bOff, int len)
a
- the first integer arrayaOff
- the offset indicating the start position within the first integer
array; the following len
integers are compared with
the corresponding number of integers of the second integer arrayb
- the second integer arraybOff
- the offset indicating the start position within the second integer
array; the following len
integers are compared with
the corresponding number of integers of the first integer arraylen
- the number of integer to be compared-1
if the two sub-arrays have the same contents, the
position of the first difference otherwisepublic static int compareBlock(long[] a, int aOff, long[] b, int bOff, int len)
a
- the first long integer arrayaOff
- the offset indicating the start position within the first long
integer array; the following len
long integers are
compared with the corresponding number of long integers of the
second long arrayb
- the second long integer arraybOff
- the offset indicating the start position within the second long
integer array; the following len
long integers are
compared with the corresponding number of long integers of the
first long arraylen
- the number of long integer to be compared-1
if the two sub-arrays have the same contents, the
position of the first difference otherwisepublic static int compareBlock(byte[] a, byte[] b)
a
- the first byte array to be compared with the second byte arrayb
- the second byte array to be compared with the first byte array-1
if the two blocks have the same contents, the
position of the first difference otherwise (if the two arrays
differ in their length only the output of this method will be the
length of the smaller array)public static int compareBlock(int[] a, int[] b)
a
- the first integer array to be compared with the second integer
arrayb
- the second integer array to be compared with the first integer
array-1
if the two blocks have the same contents, the
position of the first difference otherwise (if the two arrays
differ in their length only the output of this method will be the
length of the smaller array)public static int compareBlock(long[] a, long[] b)
a
- the first long integer array to be compared with the second long
integer arrayb
- the second long integer array to be compared with the first long
integer array-1
if the two blocks have the same contents, the
position of the first difference otherwise (if the two arrays
differ in their length only the output of this method will be the
length of the smaller array)public static void fillBlock(byte[] block, int blockOff, byte b, int len)
After calling this method all bytes of the given subarray will have the same value, e.g.
will set block[1] ... block[3] tobyte[] block = ...; CryptoUtils.fillBlock(block, 2, (byte)0xBA, 3);
0xBA
.block
- the byte array to which the specified byte shall be written
repeatedlyblockOff
- the offset indicating the start position within the given byte
array; the following len
bytes will be set to
b
b
- the byte value to be written repeatedly to the specified sub arraylen
- the number of bytes that have to be set to b
,
beginning at len
public static void fillBlock(int[] block, int blockOff, int b, int len)
After calling this method all integers of the given subarray will have the same value, e.g.
will set block[1] ... block[3] toint[] block = ...; CryptoUtils.fillBlock(block, 2, 0xBA, 3);
0xBA
.block
- the integer array to which the specified integer shall be written
repeatedlyblockOff
- the offset indicating the start position within the given integer
array; the following len
integers will be set to
b
b
- the integer value to be written repeatedly to the specified sub
arraylen
- the number of integers that have to be set to b
,
beginning at len
public static void fillBlock(long[] block, int blockOff, long b, int len)
After calling this method all long integers of the given subarray will have the same value, e.g.
will set block[1] ... block[3] tolong[] block = ...; CryptoUtils.fillBlock(block, 2, 0xBA, 3);
0xBA
.block
- the long integer array to which the specified long integer shall
be written repeatedlyblockOff
- the offset indicating the start position within the given long
integer array; the following len
long integers will
be set to b
b
- the long integer value to be written repeatedly to the specified
sub arraylen
- the number of long integers that have to be set to b
,
beginning at len
public static void fillBlock(byte[] block, byte b)
After calling this method all the bytes of the given byte array will have the same value.
block
- the byte array that shall be filled with the given byteb
- the byte value for filling the arraypublic static void fillBlock(int[] block, int b)
After calling this method all the integers of the given integer array will have the same value.
block
- the integer array that shall be filled with the given integerb
- the integer value for filling the arraypublic static void fillBlock(long[] block, long b)
After calling this method all the longs of the given long integer array will have the same value.
block
- the long integer array that shall be filled with the given long
integerb
- the long integer value for filling the arraypublic static void spreadIntsToBytes(int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen)
In Java, the primitive data type byte internally is represented as 8
bit signed number, and int denotes a signed 32 bit number. This
method transforms the int values of the given inInts
array
into proper byte values for the specified outBytes
array. The
transforming computation will yield four bytes for each int value of the
specified sequence of the input int array. So, if the given
inInts
array will consist of n
ints, the
following "transformation" will take place (when both inOff
and outOff
, indicating the start position from where to read
the int values from the inInts
array and the destination
position where to write the resulting byte values to the
outBytes
array, are set to 0
):
The last parameter valueinInts[0] goes to outBytes[0...3] inInts[1] goes to outBytes[4...7] ... inInts[n-1] goes to outInts[n*4-4...n*4-1]
intLen
denotes the size
of the input int array. To spread, for instance, a sequence of 2 ints, set
intLen
to 2
: int[] in = new int[2]; byte[] out = new byte[8]; ... CryptoUtils.spreadLongsToBytes(in, 0, out, 0, 2);
inInts
- the int array supplying the integers to be spread to bytesinOff
- the offset indicating the start position within the input int
array; the following intLen
integers will be spread
to bytesoutBytes
- the byte array to which the resulting byte values are written,
starting at position outOff
outOff
- the offset indicating the start position within the destination
byte array, to which the resulting byte values are writtenintLen
- the number of int values that have to spread to bytespublic static void spreadIntToBytes(int in, byte[] out, int offset)
public static void spreadIntsToBytesLE(int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen)
public static void spreadIntToBytesLE(int in, byte[] out, int offset)
public static void spreadLongsToBytes(long[] inLongs, int inOff, byte[] outBytes, int outOff, int longLen)
In Java, the primitive data type byte internally is represented as 8
bit signed number, and long denotes a signed 64 bit number. This
method transforms the long values of the given inLongs
array
into proper byte values for the specified outBytes
array. The
transforming computation will yield eight bytes for each long value of the
specified sequence of the input long array. So, if the given
inLongs
array will consist of n
longs, the
following "transformation" will take place (when both inOff
and outOff
, indicating the start position from where to read
the long values from the inLongs
array and the destination
position where to write the resulting byte values to the
outBytes
array, are set to 0
):
The last parameter valueinLongs[0] goes to outBytes[0...7] inLongs[1] goes to outBytes[8...15] ... inLongs[n-1] goes to outInts[n*8-8...n*8-1]
longLen
denotes the
size of the input long array. To spread, for instance, a sequence of 2
long, set longLen
to 2
: long[] in = new long[2]; byte[] out = new byte[16]; ... CryptoUtils.spreadLongsToBytes(in, 0, out, 0, 2);
inLongs
- the long array supplying the integers to be spread to bytesinOff
- the offset indicating the start position within the input int
array; the following intLen
integers will be spread
to bytesoutBytes
- the byte array to which the resulting byte values are written,
starting at position outOff
outOff
- the offset indicating the start position within the destination
byte array, to which the resulting byte values are writtenlongLen
- the number of long values that have to spread to bytespublic static void spreadLongsToBytesLE(long[] inLongs, int inOff, byte[] outBytes, int outOff, int longLen)
public static void squashBytesToLongs(byte[] inBytes, int inOff, long[] outLongs, int outOff, int longLen)
In Java, the primitive data type byte internally is represented as 8
bit signed number, and longs denotes a signed 64 bit number. This
method transforms the bytes of the given inBytes
array into
proper long values for the specified outLongs
int array. The
bytes are grouped in sequences each of it consisting of 8 bytes (64 bits)
to compute the corresponding int values. So, if the given
inBytes
array will consist of n
bytes (
n = 8 * m;
m >= 1
), the following "transformation" will take place
(when both inOff
and outOff
, indicating the start
position from where to read the bytes from the inBytes
array
and the destination position where to write the resulting long values to
the outLongs
array, are set to 0
):
The last parameter valueinBytes[0...7] goes to outLongs[0] inBytes[8...15] goes to outLongs[1] ... inBytes[n-8...n-1] goes to outLongs[n/8-1]
longLen
denotes the
number of the resulting long values. To squash, for instance, a sequence of
16 bytes, set longLen
to 2
indicating that two
long values will result from the transforming computations: byte[] in = new byte[16]; long[] out = new long[2]; ... CryptoUtils.squashBytesToInts(in, 0, out, 0, 2);
inBytes
- the byte array supplying the bytes to be squashed to intsinOff
- the offset indicating the start position within the input byte
array; the following 4 * intLen
bytes will be
squashed to longsoutLongs
- the long array to which the resulting int values are written,
starting at position outOff
outOff
- the offset indicating the start position within the destination
int array, to which the resulting int values are writtenlongLen
- the number of long values that will result from the
"bytes-to-longs" transformationpublic static void squashBytesToLongsLE(byte[] inBytes, int inOff, long[] outLongs, int outOff, int longLen)
public static void squashBytesToInts(byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen)
In Java, the primitive data type byte internally is represented as 8
bit signed number, and int denotes a signed 32 bit number. This
method transforms the bytes of the given inBytes
array into
proper int values for the specified outInts
int array. The
bytes are grouped in sequences each of it consisting of 4 bytes (32 bits)
to compute the corresponding int values. So, if the given
inBytes
array will consist of n
bytes (
n = 4 * m;
m >= 1
), the following "transformation" will take place
(when both inOff
and outOff
, indicating the start
position from where to read the bytes from the inBytes
array
and the destination position where to write the resulting int values to the
outInts
array, are set to 0
):
The last parameter valueinBytes[0...3] goes to outInts[0] inBytes[4...7] goes to outInts[1] ... inBytes[n-4...n-1] goes to outInts[n/4-1]
intLen
denotes the
number of the resulting int values. To squash, for instance, a sequence of
8 bytes, set intLen
to 2
indicating that two int
values will result from the transforming computations: byte[] in = new byte[8]; int[] out = new int[2]; ... CryptoUtils.squashBytesToInts(in, 0, out, 0, 2);
inBytes
- the byte array supplying the bytes to be squashed to intsinOff
- the offset indicating the start position within the input byte
array; the following 4 * intLen
bytes will be
squashed to intsoutInts
- the int array to which the resulting int values are written,
starting at position outOff
outOff
- the offset indicating the start position within the destination
int array, to which the resulting int values are writtenintLen
- the number of int values that will result from the "bytes-to-ints"
transformationpublic static int squashBytesToInt(byte[] inBytes, int inOff)
public static void squashBytesToIntsPadZero(byte[] inBytes, int inOff, int[] outInts, int outOff, int byteLen)
public static int squashBytesToIntsLEPadZero(byte[] inBytes, int inOff, int[] outInts, int outOff, int byteLen)
public static void squashBytesToIntsLE(byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen)
public static void squashBytesToShortsBE(byte[] inBytes, int inOff, int[] outShorts, int outOff, int shortLen)
In Java, the primitive data type byte internally is represented as 8
bit signed number, and short denotes a signed 16 bit number. This
method transforms the bytes of the given inBytes
array into
proper short values for the specified outShorts
int array. The
bytes are grouped in sequences each of it consisting of 2 bytes (16 bits)
to compute the corresponding short values. So, if the given
inBytes
array will consist of n
bytes (
n = 2 * m;
m >= 1
), the following "transformation" will take place
(when both inOff
and outOff
, indicating the start
position from where to read the bytes from the inBytes
array
and the destination position where to write the resulting short values to
the outShorts
array, are set to 0
):
The last parameter valueinBytes[0...1] goes to outShorts[0] inBytes[2...3] goes to outShorts[1] ... inBytes[n-2...n-1] goes to outShorts[n/2-1]
shortLen
denotes the number of the
resulting short values. To squash, for instance, a sequence of 8 bytes, set
shortLen
to 4
indicating that four short values
will result from the transforming computations (note that
outShorts
is decleared as an int[] array; only the 16 low bits
are significant): Each single "two-byte-to-one-short" transformation is performed according to the big-endian scheme where the leftmost byte (with a lower address) is most significant, mapping inBytes[i] to the lower byte of the corresponding short value and inBytes[i+1] to the higher byte, e.g.:byte[] in = new byte[8]; int[] out = new int[4]; ... CryptoUtils.squashBytesToShortsBE(in, 0, out, 0, 4);
two bytes: 00000000 00000001 {00, 01} result in one short: 00000000 00000001 {1}
inBytes
- the byte array supplying the bytes to be squashed to shortsinOff
- the offset indicating the start position within the input byte
array; the following 2 * shortLen
bytes will be
squashed to shortsoutShorts
- the int array to which the resulting short values are written,
starting at position outOff
outOff
- the offset indicating the start position within the destination
int (short) array, to which the resulting short values are writtenshortLen
- the number of short values that will result from the
"bytes-to-shorts" transformationpublic static void squashBytesToShortsLE(byte[] inBytes, int inOff, int[] outShorts, int outOff, int shortLen)
In Java, the primitive data type byte internally is represented as 8
bit signed number, and short denotes a signed 16 bit number. This
method transforms the bytes of the given inBytes
array into
proper short values for the specified outShorts
int array. The
bytes are grouped in sequences each of it consisting of 2 bytes (16 bits)
to compute the corresponding short values. So, if the given
inBytes
array will consist of n
bytes (
n = 2 * m;
m >= 1
), the following "transformation" will take place
(when both inOff
and outOff
, indicating the start
position from where to read the bytes from the inBytes
array
and the destination position where to write the resulting short values to
the outShorts
array, are set to 0
):
The last parameter valueinBytes[0...1] goes to outShorts[0] inBytes[2...3] goes to outShorts[1] ... inBytes[n-2...n-1] goes to outShorts[n/2-1]
shortLen
denotes the number of the
resulting short values. To squash, for instance, a sequence of 8 bytes, set
shortLen
to 4
indicating that four short values
will result from the transforming computations (note that
outShorts
is decleared as an int[] array; only the 16 low bits
are significant): Each single "two-byte-to-one-short" transformation is performed according to the little-endian scheme where the rightmost byte (with a higher address) is most significant, mapping inBytes[i] to the higher byte of the corresponding short value and inBytes[i+1] to the lower byte, e.g.:byte[] in = new byte[8]; int[] out = new int[4]; ... CryptoUtils.squashBytesToShortsBE(in, 0, out, 0, 4);
two bytes: 00000000 00000001 {00, 01} result in one short: 00000001 00000000 {256}
inBytes
- the byte array supplying the bytes to be squashed to shortsinOff
- the offset indicating the start position within the input byte
array; the following 2 * shortLen
bytes will be
squashed to shortsoutShorts
- the int array to which the resulting short values are written,
starting at position outOff
outOff
- the offset indicating the start position within the destination
int (short) array, to which the resulting short values are writtenshortLen
- the number of short values that will result from the
"bytes-to-shorts" transformationpublic static void spreadShortsToBytesBE(int[] inShorts, int inOff, byte[] outBytes, int outOff, int shortLen)
In Java, the primitive data type byte internally is represented as 8
bit signed number, and short denotes a signed 16 bit number. This
method transforms the short values of the given inShorts
array
into proper byte values for the specified outBytes
array. The
transforming computation will yield two bytes for each short value of the
specified sequence of the input short array. So, if the given
inShorts
array will consist of n
shorts, the
following "transformation" will take place (when both inOff
and outOff
, indicating the start position from where to read
the short values from the inShorts
array and the destination
position where to write the resulting byte values to the
outBytes
array, are set to 0
):
The last parameter valueinShorts[0] goes to outBytes[0...1] inShorts[1] goes to outBytes[2...3] ... inShorts[n-1] goes to outInts[n*2-2...n*2-1]
shortLen
denotes the
size of the input int (short) array. To spread, for instance, a sequence of
4 shorts, set shortLen
to 4
(note that
inShorts
is decleared as an int[] array; only the 16 low bits
are significant): Each single "one-short-to-two-bytes" transformation is performed according to the big-endian scheme where the leftmost byte (with a lower address) is most significant, mapping the lower byte of an input short value to the correspondingint[] in = new int[4]; byte[] out = new byte[8]; ... CryptoUtils.squashBytesToInts(in, 0, out, 0, 4);
outBytes[i]
byte and
the higher byte to outBytes[i+1]
, e.g.: one short: 00000000 00000001 {1} results in two bytes: 00000000 00000001 {00, 01}
inShorts
- the int array supplying the shorts to be spread to bytesinOff
- the offset indicating the start position within the input int
(short) array; the following shortLen
shorts will be
spread to bytesoutBytes
- the byte array to which the resulting byte values are written,
starting at position outOff
outOff
- the offset indicating the start position within the destination
byte array, to which the resulting byte values are writtenshortLen
- the number of short values that have to spread to bytespublic static void spreadShortsToBytesLE(int[] inShorts, int inOff, byte[] outBytes, int outOff, int shortLen)
In Java, the primitive data type byte internally is represented as 8
bit signed number, and short denotes a signed 16 bit number. This
method transforms the short values of the given inShorts
array
into proper byte values for the specified outBytes
array. The
transforming computation will yield two bytes for each short value of the
specified sequence of the input short array. So, if the given
inShorts
array will consist of n
shorts, the
following "transformation" will take place (when both inOff
and outOff
, indicating the start position from where to read
the short values from the inShorts
array and the destination
position where to write the resulting byte values to the
outBytes
array, are set to 0
):
The last parameter valueinShorts[0] goes to outBytes[0...1] inShorts[1] goes to outBytes[2...3] ... inShorts[n-1] goes to outInts[n*2-2...n*2-1]
shortLen
denotes the
size of the input int (short) array. To spread, for instance, a sequence of
4 shorts, set shortLen
to 4
(note that
inShorts
is decleared as an int[] array; only the 16 low bits
are significant): Each single "one-short-to-two-bytes" transformation is performed according to the little-endian scheme where the rightmost byte (with a higher address) is most significant, mapping the lower byte of an input short value to the correspondingint[] in = new int[4]; byte[] out = new byte[8]; ... CryptoUtils.squashBytesToInts(in, 0, out, 0, 4);
outBytes[i]
byte and
the higher byte to outBytes[i-1]
, e.g.: one short: 00000001 00000000 {256} results in two bytes: 00000000 00000001 {00, 01}
inShorts
- the int array supplying the shorts to be spread to bytesinOff
- the offset indicating the start position within the input int
(short) array; the following shortLen
shorts will be
spread to bytesoutBytes
- the byte array to which the resulting byte values are written,
starting at position outOff
outOff
- the offset indicating the start position within the destination
byte array, to which the resulting byte values are writtenshortLen
- the number of short values that have to spread to bytespublic static byte[] resizeArray(byte[] array, int len)
array
- the array to resizelen
- the new length of the arraypublic static void reverseArray(byte[] array, int off, int len)
array
- the array to be "reversed"off
- the start offset into the arraylen
- the number of bytes to be involved in the reversing operation,
starting at offpublic static byte[] increment(byte[] b)
public static byte[] incrementExtended(byte[] number)
number
- The given number to increment.public static byte[] concatenate(byte[] a1, byte[] a2)
a1
- The first part.a2
- The second part.public static int[] concatenate(int[] a1, int[] a2)
a1
- The first part.a2
- The second part.public static long[] concatenate(long[] a1, long[] a2)
a1
- The first part.a2
- The second part.public static java.math.BigInteger getStrongPrime(int x, java.util.Random random)
public static int gcd(int a, int b)
public static int[] extGcd(int a, int b)
public static byte[] shiftRight(byte[] v)
v
- The byte array.public static int[] shiftRight(int[] v)
v
- The integer array.public static long[] shiftRight(long[] v)
v
- The integer array.public static byte[] shiftLeft(byte[] v)
v
- The byte array.public static int[] shiftLeft(int[] v)
v
- The byte array.public static long[] shiftLeft(long[] v)
v
- The byte array.public static byte[] addModBlockSize(int blocksize, byte[] unsignedBigInt1, byte[] unsignedBigInt2)
null
, the result is the same as the other
argument. If both are null
, the result is null
.
This method always operates inside the block size, if the result is longer
it is simply stripped. The resulting array will always have a length of
blocksize. Leading zeros will be inserted if necessary.blocksize
- The size to which the byte array will be truncatedunsignedBigInt1
- The first unsigned big integer.unsignedBigInt2
- The second unsigned big integer.