Implement new API
This commit is contained in:
@@ -40,7 +40,7 @@ public class Obfuscated {
|
||||
/**
|
||||
* Build new obfuscation wrapper using latest obfuscation algorithm version.
|
||||
*
|
||||
* @param key master key to use for obfuscation
|
||||
* @param key master key to use for obfuscation
|
||||
* @param data data to obfuscate
|
||||
*/
|
||||
public Obfuscated(char[] key, String data) {
|
||||
@@ -50,7 +50,18 @@ public class Obfuscated {
|
||||
/**
|
||||
* Build new obfuscation wrapper using latest obfuscation algorithm version.
|
||||
*
|
||||
* @param key master key to use for obfuscation
|
||||
* @param key master key to use for obfuscation
|
||||
* @param data data to obfuscate
|
||||
* @param version version of the obfuscation algorithm to use
|
||||
*/
|
||||
public Obfuscated(char[] key, String data, int version) {
|
||||
this(key, data.getBytes(StandardCharsets.UTF_8), version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build new obfuscation wrapper using latest obfuscation algorithm version.
|
||||
*
|
||||
* @param key master key to use for obfuscation
|
||||
* @param data data to obfuscate
|
||||
*/
|
||||
public Obfuscated(char[] key, byte[] data) {
|
||||
@@ -60,8 +71,8 @@ public class Obfuscated {
|
||||
/**
|
||||
* Build new obfuscation wrapper using specified obfuscation algorithm version.
|
||||
*
|
||||
* @param key master key to use for obfuscation
|
||||
* @param data data to obfuscate
|
||||
* @param key master key to use for obfuscation
|
||||
* @param data data to obfuscate
|
||||
* @param version version of the obfuscation algorithm to use
|
||||
*/
|
||||
public Obfuscated(char[] key, byte[] data, int version) {
|
||||
|
||||
+14
@@ -36,6 +36,13 @@ public class ObfuscatedData {
|
||||
private final byte[] salt;
|
||||
private final byte[] cipherText;
|
||||
|
||||
/**
|
||||
* Build new obfuscated data container.
|
||||
*
|
||||
* @param version version of the algorithm used to obfuscate data
|
||||
* @param salt random salt bytes
|
||||
* @param cipherText obfuscated data
|
||||
*/
|
||||
public ObfuscatedData(int version, byte[] salt, byte[] cipherText) {
|
||||
this.version = version;
|
||||
this.salt = Arrays.copyOf(salt, salt.length);
|
||||
@@ -48,6 +55,13 @@ public class ObfuscatedData {
|
||||
Base64.toBase64String(cipherText));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse string containing obfuscated data.
|
||||
*
|
||||
* @param obfuscatedString obfuscated string to parse
|
||||
*
|
||||
* @return parsed data
|
||||
*/
|
||||
public static ObfuscatedData fromString(String obfuscatedString) {
|
||||
String[] parts = obfuscatedString.split("\\$");
|
||||
if (parts.length != 5) {
|
||||
|
||||
+5
@@ -28,6 +28,11 @@ package rs.in.zivanovic.obfuscator.impl;
|
||||
*/
|
||||
public class ObfuscatorException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* Wrap throwable into obfuscator exception.
|
||||
*
|
||||
* @param cause throwable to wrap
|
||||
*/
|
||||
public ObfuscatorException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
+9
@@ -40,6 +40,15 @@ public class PBEObfuscatorImpl implements Obfuscator {
|
||||
private final int iterations;
|
||||
private final int saltLen;
|
||||
|
||||
/**
|
||||
* Build new PBE-based obfuscator with specified parameters.
|
||||
*
|
||||
* @param version version number for this set of parameters
|
||||
* @param algo encryption algorithm to use
|
||||
* @param provider crypto provider
|
||||
* @param iterations number of key derivation rounds
|
||||
* @param saltLen length of random salt in bytes
|
||||
*/
|
||||
public PBEObfuscatorImpl(int version, String algo, String provider, int iterations, int saltLen) {
|
||||
this.algo = algo;
|
||||
this.provider = provider;
|
||||
|
||||
+4
@@ -41,6 +41,10 @@ public class V1ObfuscatorImpl extends PBEObfuscatorImpl {
|
||||
private static final int SALT_LEN = 8;
|
||||
private static final int ITERATION_COUNT = 16_000;
|
||||
|
||||
/**
|
||||
* Build new PBE-based obfuscator using PBEWithSHA256And128BitAES-CBC-BC with 8 bytes random salt and 16000
|
||||
* iterations.
|
||||
*/
|
||||
public V1ObfuscatorImpl() {
|
||||
super(VERSION, ALGO, PROVIDER, ITERATION_COUNT, SALT_LEN);
|
||||
}
|
||||
|
||||
@@ -51,6 +51,20 @@ public class NewApiTest {
|
||||
assertThat(unob, equalTo(dataS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObfuscateUnobfuscateArraysV1() {
|
||||
String o = new Obfuscated(key, dataBA, 1).toString();
|
||||
byte[] unob = new Unobfuscated(key, o).asByteArray();
|
||||
assertThat(unob, equalTo(dataBA));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObfuscateUnobfuscateStringsV1() {
|
||||
String o = new Obfuscated(key, dataS, 1).toString();
|
||||
String unob = new Unobfuscated(key, o).asString();
|
||||
assertThat(unob, equalTo(dataS));
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void testUnobfuscationClearsData() {
|
||||
String o = new Obfuscated(key, dataBA).toString();
|
||||
@@ -59,4 +73,14 @@ public class NewApiTest {
|
||||
unob.asByteArray();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testObfuscatedInvalidVersion() {
|
||||
String o = new Obfuscated(key, dataS, -1).toString();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testUnobfuscatedInvalidVersion() {
|
||||
String o = new Unobfuscated(key, "$rizobf$-1$F6Krv4H91RE=$NjytmuPjmfZbwQjqUWcbDg==").asString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user