Skip to content

Commit

Permalink
errorprone :: MemberName - kff package (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdahlke authored Aug 30, 2024
1 parent b877c16 commit b0c4ffe
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 74 deletions.
18 changes: 9 additions & 9 deletions src/main/java/emissary/kff/ChecksumCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public ChecksumCalculator() throws NoSuchAlgorithmException {
* Constructor initializes specified algorithm
*
* @param alg string name of algorightm, e.g. SHA
* @param useCRC true if CRC32 should be calculated
* @param useCrc true if CRC32 should be calculated
* @throws NoSuchAlgorithmException if the algorithm isn't available
*/
public ChecksumCalculator(String alg, boolean useCRC) throws NoSuchAlgorithmException {
public ChecksumCalculator(String alg, boolean useCrc) throws NoSuchAlgorithmException {
this(new String[] {alg});
setUseCRC(useCRC);
setUseCrc(useCrc);
}

/**
Expand All @@ -63,7 +63,7 @@ public ChecksumCalculator(@Nullable String[] algs) throws NoSuchAlgorithmExcepti
if (algs != null && algs.length > 0) {
for (String alg : algs) {
if (alg.equals("CRC32")) {
setUseCRC(true);
setUseCrc(true);
} else if (alg.equals("SSDEEP")) {
setUseSsdeep(true);
} else {
Expand All @@ -83,7 +83,7 @@ public ChecksumCalculator(@Nullable Collection<String> algs) throws NoSuchAlgori
if (CollectionUtils.isNotEmpty(algs)) {
for (String alg : algs) {
if (alg.equals("CRC32")) {
setUseCRC(true);
setUseCrc(true);
} else if (alg.equals("SSDEEP")) {
setUseSsdeep(true);
} else {
Expand All @@ -97,7 +97,7 @@ public ChecksumCalculator(@Nullable Collection<String> algs) throws NoSuchAlgori
/**
* Determine if we are using CRC summing
*/
public boolean getUseCRC() {
public boolean getUseCrc() {
return (crc != null);
}

Expand All @@ -106,7 +106,7 @@ public boolean getUseCRC() {
*
* @param use true if CRC processing is desired
*/
public void setUseCRC(boolean use) {
public void setUseCrc(boolean use) {
if (use) {
crc = new CRC32();
} else {
Expand Down Expand Up @@ -160,7 +160,7 @@ public ChecksumResults digest(byte[] buffer) {

// Only use ssdeep if non-null
if (ssdeep != null) {
res.setSsdeep(ssdeep.fuzzy_hash(buffer));
res.setSsdeep(ssdeep.fuzzyHash(buffer));
}

return res;
Expand Down Expand Up @@ -207,7 +207,7 @@ public ChecksumResults digest(final SeekableByteChannelFactory sbcf) {
}

if (ssdeep != null) {
res.setSsdeep(ssdeep.fuzzy_hash(sbcf));
res.setSsdeep(ssdeep.fuzzyHash(sbcf));
}

return res;
Expand Down
52 changes: 22 additions & 30 deletions src/main/java/emissary/kff/EditDistance.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,8 @@ private static int min2(int x, int y) {
return (mx < my ? mx : my);
}

static int insert_cost = 1;
static int delete_cost = 1;

static int _iswap; /* swap_int temp variable */
static char _cswap; /* swap_char temp variable */

/* min2, min3 temp variables */
static int _mx;
static int _my;
static int _mz;
static int insertCost = 1;
static int deleteCost = 1;

// dynamic programming counters
static int row;
Expand All @@ -117,21 +109,21 @@ private static int min2(int x, int y) {
static int ins = 1;
static int del = 1;
static int ch = 3;
static int swap_cost = 5;
static int swapCost = 5;

static int from_len;
static int to_len;
static int fromLen;
static int toLen;

private static int ar(int x, int y, int index) {
return ((x == 0) ? y * del : ((y == 0) ? x * ins : buffer[mod(index)]));
}

private static int nw(int x, int y) {
return ar(x, y, index + from_len + 2);
return ar(x, y, index + fromLen + 2);
}

private static int n(int x, int y) {
return ar(x, y, index + from_len + 3);
return ar(x, y, index + fromLen + 3);
}

private static int w(int x, int y) {
Expand All @@ -147,33 +139,33 @@ private static int mod(int x) {
}

/*
* edit_distn -- returns the edit distance between two strings, or -1 on failure
* returns the edit distance between two strings, or -1 on failure
*/
public static int edit_distn(@Nullable byte[] from, int _from_len, @Nullable byte[] to, int _to_len) {
from_len = _from_len;
to_len = _to_len;
public static int calculateEditDistance(@Nullable byte[] from, int fromLen, @Nullable byte[] to, int toLen) {
EditDistance.fromLen = fromLen;
EditDistance.toLen = toLen;

if (from == null) {
if (to == null) {
return 0;
} else {
return to_len * insert_cost;
return EditDistance.toLen * insertCost;
}
} else if (to == null) {
return from_len * delete_cost;
return EditDistance.fromLen * deleteCost;
}

/* Initialize registers */

radix = 2 * from_len + 3;
radix = 2 * EditDistance.fromLen + 3;

/* Make from short enough to fit in the static storage, if it's at all possible */

if (from_len > to_len && from_len > STRLENTHRESHOLD) {
if (EditDistance.fromLen > EditDistance.toLen && EditDistance.fromLen > STRLENTHRESHOLD) {
int[] x = new int[1];
int[] y = new int[1];
x[0] = from_len;
y[0] = to_len;
x[0] = EditDistance.fromLen;
y[0] = EditDistance.toLen;
swapInt(x, y);
byte[][] xx = new byte[1][];
byte[][] yy = new byte[1][];
Expand All @@ -184,7 +176,7 @@ public static int edit_distn(@Nullable byte[] from, int _from_len, @Nullable byt

/* Allocate the array storage (from the heap if necessary) */

if (from_len <= STRLENTHRESHOLD) {
if (EditDistance.fromLen <= STRLENTHRESHOLD) {
buffer = store;
} else {
buffer = new int[radix];
Expand Down Expand Up @@ -222,7 +214,7 @@ public static int edit_distn(@Nullable byte[] from, int _from_len, @Nullable byt
buffer[index++] = min2(ins + del, (from[0] == to[0] ? 0 : ch));

low = buffer[mod(index + radix - 1)];
for (col = 1; col < from_len; col++) {
for (col = 1; col < EditDistance.fromLen; col++) {
buffer[index] = min3(col * del + ((from[col] == to[0]) ? 0 : ch), (col + 1) * del + ins, buffer[index - 1] + del);
if (buffer[index] < low) {
low = buffer[index];
Expand All @@ -231,12 +223,12 @@ public static int edit_distn(@Nullable byte[] from, int _from_len, @Nullable byt
}

/* Now handle the rest of the matrix */
for (row = 1; row < to_len; row++) {
for (col = 0; col < from_len; col++) {
for (row = 1; row < EditDistance.toLen; row++) {
for (col = 0; col < EditDistance.fromLen; col++) {
buffer[index] = min3(nw(row, col) + ((from[col] == to[row]) ? 0 : ch), n(row, col + 1) + ins, w(row + 1, col) + del);

if (from[col] == to[row - 1] && col > 0 && from[col - 1] == to[row]) {
buffer[index] = min2(buffer[index], nnww(row - 1, col - 1) + swap_cost);
buffer[index] = min2(buffer[index], nnww(row - 1, col - 1) + swapCost);
}

if (buffer[index] < low || col == 0) {
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/emissary/kff/SpamSumSignature.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SpamSumSignature {
* <p>
* Change a string into an array of bytes
*/
public static byte[] GetBytes(String str) {
public static byte[] getBytes(String str) {
byte[] r = new byte[str.length()];
for (int i = 0; i < r.length; i++) {
r[i] = (byte) str.charAt(i);
Expand All @@ -35,7 +35,7 @@ public static byte[] GetBytes(String str) {
* <p>
* Change a string into an array of bytes
*/
public static String GetString(byte[] hsh) {
public static String getString(byte[] hsh) {
String r = "";
for (int i = 0; i < hsh.length; i++) {
r += (char) hsh[i];
Expand Down Expand Up @@ -70,8 +70,8 @@ public SpamSumSignature(String signature) {
}

blockSize = Integer.parseInt(signature.substring(0, 0 + idx1));
hash1 = GetBytes(signature.substring(idx1 + 1, idx1 + 1 + idx2 - idx1 - 1));
hash2 = GetBytes(signature.substring(idx2 + 1));
hash1 = getBytes(signature.substring(idx1 + 1, idx1 + 1 + idx2 - idx1 - 1));
hash2 = getBytes(signature.substring(idx2 + 1));
}

public SpamSumSignature(long blockSize, byte[] hash1, byte[] hash2) {
Expand Down Expand Up @@ -128,8 +128,8 @@ public boolean isEqual(SpamSumSignature other) {

@Override
public String toString() {
String hashText1 = GetString(hash1);
String hashText2 = GetString(hash2);
String hashText1 = getString(hash1);
String hashText2 = getString(hash2);
return blockSize + ":" + hashText1 + ":" + hashText2;
}

Expand Down Expand Up @@ -171,6 +171,6 @@ public static void main(String[] args) {
SpamSumSignature sss1 = new SpamSumSignature(args[0]);
SpamSumSignature sss2 = new SpamSumSignature(args[1]);
Ssdeep ssdeep = new Ssdeep();
System.out.println("" + ssdeep.Compare(sss1, sss2));
System.out.println("" + ssdeep.compare(sss1, sss2));
}
}
18 changes: 9 additions & 9 deletions src/main/java/emissary/kff/Ssdeep.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public final class Ssdeep {
* Base64 encoding table. Given a 5-bit value {@code n}, position {@code n} in the array is the code point (expressed as
* a byte) that should appear.
*/
private static final byte[] b64Table = SpamSumSignature.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
private static final byte[] b64Table = SpamSumSignature.getBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");

/**
* Get the base64 encoding of the low 6 bits of the given value.
Expand Down Expand Up @@ -400,7 +400,7 @@ public Ssdeep() {}
* @param data The bytes to be hashed.
* @return The SpamSum signature for the bytes.
*/
public String fuzzy_hash(final byte[] data) {
public String fuzzyHash(final byte[] data) {
final SsContext ctx = new SsContext(data);
while (true) {
final SpamSumSignature signature = ctx.generateHash(data);
Expand All @@ -415,7 +415,7 @@ public String fuzzy_hash(final byte[] data) {
}
}

public String fuzzy_hash(final SeekableByteChannelFactory sbcf) {
public String fuzzyHash(final SeekableByteChannelFactory sbcf) {
final SsContext ctx = new SsContext(sbcf);
while (true) {
final SpamSumSignature signature = ctx.generateHash(sbcf);
Expand All @@ -437,7 +437,7 @@ public String fuzzy_hash(final SeekableByteChannelFactory sbcf) {
* @return The SpamSum signature for the file.
* @throws IOException If there is some I/O problem accessing the file.
*/
public String fuzzy_hash_file(final File file) throws IOException {
public String fuzzyHashFile(final File file) throws IOException {
try (final RandomAccessFile stream = new RandomAccessFile(file, "r")) {
final SsContext ctx = new SsContext(file);
while (true) {
Expand All @@ -462,8 +462,8 @@ public String fuzzy_hash_file(final File file) throws IOException {
* @return The SpamSum signature for the file.
* @throws IOException If there is some I/O problem accessing the file.
*/
public String fuzzy_hash_file(final String fileName) throws IOException {
return this.fuzzy_hash_file(new File(fileName));
public String fuzzyHashFile(final String fileName) throws IOException {
return this.fuzzyHashFile(new File(fileName));
}

/**
Expand Down Expand Up @@ -610,7 +610,7 @@ private static long scoreStrings(final byte[] s1, final byte[] s2, final long bl
// Compute the edit distance between the two strings. The edit
// distance gives us a pretty good idea of how closely related
// the two strings are.
long score = EditDistance.edit_distn(s1, len1, s2, len2);
long score = EditDistance.calculateEditDistance(s1, len1, s2, len2);
if (logger.isDebugEnabled()) {
logger.debug("edit_dist: {}", score);
}
Expand Down Expand Up @@ -654,7 +654,7 @@ private static long scoreStrings(final byte[] s1, final byte[] s2, final long bl
* @return The score for the two signatures. The value is in the range 0..100, where 0 is a terrible match and 100 is a
* great match.
*/
public int Compare(@Nullable final SpamSumSignature signature1, @Nullable final SpamSumSignature signature2) {
public int compare(@Nullable final SpamSumSignature signature1, @Nullable final SpamSumSignature signature2) {
if ((null == signature1) || (null == signature2)) {
return -1;
}
Expand Down Expand Up @@ -709,7 +709,7 @@ public static void main(final String[] args) throws Exception {
try (final InputStream is = Files.newInputStream(Paths.get(f))) {
final byte[] buffer = IOUtils.toByteArray(is);
// output format matches the original ssdeep program
System.out.println(ss.fuzzy_hash(buffer) + ",\"" + f + "\"");
System.out.println(ss.fuzzyHash(buffer) + ",\"" + f + "\"");
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/emissary/kff/ChecksumCalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void testSpecifiedArgWithCrc() {
assertEquals(2, algs.size(), "Two alg used for (string,boolean) ctor");
assertTrue(algs.contains("SHA-1"), "SHA-1 alg used for (string,boolean) ctor");
assertEquals(DATA_SHA1, cr.getHashString("SHA-1"), "SHA-1 computation");
assertTrue(cc.getUseCRC(), "Using CRC");
assertTrue(cc.getUseCrc(), "Using CRC");
assertTrue(algs.contains("CRC32"), "Using CRC and in alg set");
assertNotEquals(-1L, cr.getCrc(), "CRC computed");
} catch (NoSuchAlgorithmException ex) {
Expand All @@ -78,7 +78,7 @@ void testSpecifiedArgWitouthCrc() {
assertEquals(1, algs.size(), "One alg used for (string,boolean) ctor");
assertTrue(algs.contains("SHA-1"), "SHA-1 alg used for (string,boolean) ctor");
assertEquals(DATA_SHA1, cr.getHashString("SHA-1"), "SHA-1 computation");
assertFalse(cc.getUseCRC(), "Not using CRC");
assertFalse(cc.getUseCrc(), "Not using CRC");
assertEquals(-1L, cr.getCrc(), "CRC not computed");
} catch (NoSuchAlgorithmException ex) {
fail("Unable to get SHA-1 algorithm", ex);
Expand All @@ -97,7 +97,7 @@ void testSpecifiedArgWithCrcAsList() {
assertTrue(algs.contains("SHA-1"), "SHA-1 alg used for string[] ctor");
assertTrue(algs.contains("CRC32"), "CRC32 alg used for string[] ctor");
assertEquals(DATA_SHA1, cr.getHashString("SHA-1"), "SHA-1 computation");
assertTrue(cc.getUseCRC(), "Using CRC");
assertTrue(cc.getUseCrc(), "Using CRC");
assertNotEquals(-1L, cr.getCrc(), "CRC computed");
} catch (NoSuchAlgorithmException ex) {
fail("Unable to get SHA-1 algorithm", ex);
Expand All @@ -116,7 +116,7 @@ void testSpecifiedArgWithoutCrcAsList() {
Iterator<String> i = algs.iterator();
assertEquals("SHA-1", i.next(), "SHA-1 alg used for string[] ctor");
assertEquals(DATA_SHA1, cr.getHashString("SHA-1"), "SHA-1 computation");
assertFalse(cc.getUseCRC(), "Not using CRC");
assertFalse(cc.getUseCrc(), "Not using CRC");
assertEquals(-1L, cr.getCrc(), "CRC not computed");
} catch (NoSuchAlgorithmException ex) {
fail("Unable to get SHA-1 algorithm", ex);
Expand All @@ -142,7 +142,7 @@ void testMultipleShaVariantsSpecifiedAsList() {
assertEquals(DATA_SHA1, cr.getHashString("SHA-1"), "SHA-1 computation");
assertEquals(DATA_SHA256, cr.getHashString("SHA-256"), "SHA-256 computation");
assertEquals(DATA_SSDEEP, cr.getHashString("SSDEEP"), "SSDEEP computation");
assertFalse(cc.getUseCRC(), "Not using CRC");
assertFalse(cc.getUseCrc(), "Not using CRC");
assertEquals(-1L, cr.getCrc(), "CRC not computed");
} catch (NoSuchAlgorithmException ex) {
fail("Unable to get SHA-1 or SHA-256 algorithm", ex);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/emissary/kff/KffMemcachedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private static ChecksumResults createSums(KffMemcached mcd) throws NoSuchAlgorit
}


private KffMemcached createTestFilter(Boolean storeIdDupe, boolean simulateHit, @Nullable String _expectedKey)
private KffMemcached createTestFilter(Boolean storeIdDupe, boolean simulateHit, @Nullable String expectedKey)
throws IOException, NoSuchFieldException,
IllegalAccessException {
KffMemcached filter = new KffMemcached(TEST_ID_WITH_SPACES, "KFF", FilterType.DUPLICATE, mockMemcachedClient);
Expand All @@ -123,7 +123,7 @@ private KffMemcached createTestFilter(Boolean storeIdDupe, boolean simulateHit,
} else {
cacheResult = null;
}
this.expectedKey = _expectedKey;
this.expectedKey = expectedKey;
return filter;
}

Expand Down
Loading

0 comments on commit b0c4ffe

Please sign in to comment.