mirror of
https://github.com/github/codeql.git
synced 2026-04-29 18:55:14 +02:00
Crypto: Formatting test cases, more removal of non-ascii
This commit is contained in:
@@ -14,35 +14,36 @@ import java.nio.file.Files;
|
||||
import java.io.IOException;
|
||||
|
||||
public class UniversalFlowTest {
|
||||
|
||||
public void simpleAESEncryption() throws Exception {
|
||||
String algorithm = "AES";
|
||||
String otherAlgorithm = loadAlgorithmFromDisk();
|
||||
String algorithm = "AES";
|
||||
String otherAlgorithm = loadAlgorithmFromDisk();
|
||||
|
||||
// Randomly select between the known algorithm and the one loaded from disk
|
||||
String selectedAlgorithm = (new Random().nextInt(2) == 0) ? algorithm : otherAlgorithm;
|
||||
// Randomly select between the known algorithm and the one loaded from disk
|
||||
String selectedAlgorithm = (new Random().nextInt(2) == 0) ? algorithm : otherAlgorithm;
|
||||
|
||||
KeyGenerator keyGen = KeyGenerator.getInstance(selectedAlgorithm);
|
||||
keyGen.init(256); // 256-bit AES key.
|
||||
SecretKey key = keyGen.generateKey();
|
||||
String algorithm2 = "AES/GCM/NoPadding";
|
||||
Cipher cipher = Cipher.getInstance(algorithm2);
|
||||
byte[] iv = new byte[12]; // 12-byte IV recommended for GCM.
|
||||
new SecureRandom().nextBytes(iv);
|
||||
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv); // 128-bit authentication tag.
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key, gcmSpec);
|
||||
byte[] encryptedData = cipher.doFinal("Sensitive Data".getBytes());
|
||||
}
|
||||
KeyGenerator keyGen = KeyGenerator.getInstance(selectedAlgorithm);
|
||||
keyGen.init(256); // 256-bit AES key.
|
||||
SecretKey key = keyGen.generateKey();
|
||||
String algorithm2 = "AES/GCM/NoPadding";
|
||||
Cipher cipher = Cipher.getInstance(algorithm2);
|
||||
byte[] iv = new byte[12]; // 12-byte IV recommended for GCM.
|
||||
new SecureRandom().nextBytes(iv);
|
||||
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv); // 128-bit authentication tag.
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key, gcmSpec);
|
||||
byte[] encryptedData = cipher.doFinal("Sensitive Data".getBytes());
|
||||
}
|
||||
|
||||
// Method to load algorithm from disk
|
||||
private String loadAlgorithmFromDisk() {
|
||||
try {
|
||||
// Implementation to load algorithm name from a file
|
||||
Path path = Paths.get("algorithm.txt");
|
||||
return Files.readString(path).trim();
|
||||
} catch (IOException e) {
|
||||
// Fallback to default algorithm if loading fails
|
||||
System.err.println("Failed to load algorithm from disk: " + e.getMessage());
|
||||
return "AES";
|
||||
private String loadAlgorithmFromDisk() {
|
||||
try {
|
||||
// Implementation to load algorithm name from a file
|
||||
Path path = Paths.get("algorithm.txt");
|
||||
return Files.readString(path).trim();
|
||||
} catch (IOException e) {
|
||||
// Fallback to default algorithm if loading fails
|
||||
System.err.println("Failed to load algorithm from disk: " + e.getMessage());
|
||||
return "AES";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user