Created
March 12, 2020 08:03
-
-
Save jaideepheer/07e3f2449d413f960b57aceb7cebe429 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
public class SaltedSBox { | |
private final Random rand; | |
/** | |
* Creates a generator to get randomly generated SBoxes. | |
* The salt can be used to generate same sequence of SBoxes on different executions. | |
* @param salt the seed for the random generation. | |
*/ | |
public SaltedSBox(long salt) | |
{ | |
rand = new Random(salt); | |
} | |
/** | |
* Call to get the next SBox. | |
* @return the next generated SBox as a Byte[4][16]. | |
*/ | |
public Byte[][] getNextSBox() { | |
Byte[][] sbox = new Byte[4][16]; | |
for (int j = 0; j < 4; j++) { | |
List<Byte> row = | |
IntStream.rangeClosed(0, 15) | |
.boxed() | |
.map(Integer::byteValue) | |
.collect(Collectors.toList()); | |
Collections.shuffle(row, rand); | |
row.toArray(sbox[j]); | |
} | |
return sbox; | |
} | |
// example usage | |
public static void main(String[] args) { | |
SaltedSBox s = new SaltedSBox(1234567890); | |
int countToGenerate = 3; | |
for (int i = 0; i < countToGenerate; i++) { | |
System.out.println("SBox "+(i+1)+": "+Arrays.deepToString(s.getNextSBox())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment