com.akeyless.crypto.xor.XorUtils Maven / Gradle / Ivy
package com.akeyless.crypto.xor;
import com.akeyless.exceptions.AkeylessRuntimeException;
import java.util.ArrayList;
public class XorUtils {
public static byte[] xorFragments(ArrayList fragments) throws AkeylessRuntimeException {
if(fragments.size() == 0) {
throw new AkeylessRuntimeException("Invalid operation, No fragment was provided");
}
int n = fragments.get(0).length;
byte[] res = fragments.get(0);
for (int i = 1; i < fragments.size(); i++) {
if(fragments.get(i).length != n ) {
throw new AkeylessRuntimeException("Invalid operation, the fragments are not in the same size");
}
res = xorBytes(res, fragments.get(i));
}
return res;
}
public static byte[] xorBytes(byte[] a, byte[] b) {
int maxLen = Math.max(a.length, b.length);
byte[] dst = new byte[maxLen];
for( int i = 0 ; i < maxLen; i++) {
dst[i] = (byte)(a[i] ^ b[i]);
}
return dst;
}
}