static {
Logger threefishLogger = Logger.getLogger(ThreefishImpl.class.getName());
}
public static void testThreefish(final int blockSize, final int rounds) {
final ThreefishImpl impl;
if(rounds <= 0) {
impl = new ThreefishImpl(blockSize);
} else {
impl = new ThreefishImpl(blockSize, rounds);
}
byte[] keyData = new byte[blockSize / Byte.SIZE]; // initialized to 00h values, used later on
final long[] tweak;
try {
SecureRandom rnd = SecureRandom.getInstance("SHA1PRNG");
rnd.nextBytes(keyData);
tweak = new long[] { rnd.nextLong(), rnd.nextLong() };
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
ThreefishSecretKey sk = new ThreefishSecretKey(keyData);
impl.init(sk, tweak);
final byte[] plain = "Maarten".getBytes();
final byte[] plainPadded = zeroPad(plain, impl.getBlockSize());
final long[] encryptedBlock = new long[impl.getBlockSize() / Long.SIZE];
final long[] plainBlock = lsbBytesToArrayOfLong(plainPadded);
System.out.printf("Threefish plainbl: %s%n", tohex(lsbArrayOfLongToBytes(plainBlock)));
// not needed: impl.init(sk, tweak);
impl.blockEncrypt(plainBlock, encryptedBlock);
System.out.printf("Threefish encrypt: %s%n", tohex(lsbArrayOfLongToBytes(encryptedBlock)));
long[] decryptedBlock= new long[encryptedBlock.length];
impl.blockDecrypt(encryptedBlock, decryptedBlock);
System.out.printf("Threefish decrypt: %s%n", tohex(lsbArrayOfLongToBytes(decryptedBlock)));
}