-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,529 additions
and
576 deletions.
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
...llar/src/main/java/org/apache/metron/stellar/common/utils/hashing/tlsh/BitPairsTable.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.apache.metron.stellar.common.utils.hashing.tlsh; | ||
|
||
class BitPairsTable { | ||
|
||
private static final int BIT_PAIRS_DIFF_TABLE_SIZE = 256; | ||
|
||
private final int[][] table; | ||
|
||
BitPairsTable() { | ||
this.table = generateDefaultBitPairsDiffTable(); | ||
} | ||
|
||
private static int[][] generateDefaultBitPairsDiffTable() { | ||
int[][] result = new int[BIT_PAIRS_DIFF_TABLE_SIZE][BIT_PAIRS_DIFF_TABLE_SIZE]; | ||
|
||
for (int i = 0; i < BIT_PAIRS_DIFF_TABLE_SIZE; i++) { | ||
for (int j = 0; j < BIT_PAIRS_DIFF_TABLE_SIZE; j++) { | ||
int x = i; | ||
int y = j; | ||
int diff = 0; | ||
|
||
for (int z = 0; z < 4; z++) { | ||
int d = Math.abs(x % 4 - y % 4); | ||
|
||
if (d == 3) { | ||
diff += d * 2; | ||
} else { | ||
diff += d; | ||
} | ||
|
||
if (z < 3) { | ||
x /= 4; | ||
y /= 4; | ||
} | ||
} | ||
|
||
result[i][j] = diff; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public int getValue(int row, int column) { | ||
return table[row][column]; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...llar/src/main/java/org/apache/metron/stellar/common/utils/hashing/tlsh/SlidingWindow.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.apache.metron.stellar.common.utils.hashing.tlsh; | ||
|
||
|
||
import java.util.function.IntUnaryOperator; | ||
import java.util.stream.IntStream; | ||
|
||
public class SlidingWindow { | ||
public static final int DEFAULT_SIZE = 5; | ||
private final byte[] window; | ||
private int byteCount = 0; | ||
|
||
SlidingWindow() { | ||
this.window = new byte[DEFAULT_SIZE]; | ||
} | ||
|
||
public void put(final byte value) { | ||
int cursor = byteCount % window.length; | ||
window[cursor] = value; | ||
byteCount++; | ||
} | ||
|
||
public int[] getWindow() { | ||
final int startPosition = (byteCount - 1) % window.length; | ||
final IntUnaryOperator reverseIterate = i -> i == 0 ? window.length - 1 : i - 1; | ||
final IntUnaryOperator mapper = i -> window[i] & 0xFF; | ||
return IntStream.iterate(startPosition, reverseIterate) | ||
.limit(window.length) | ||
.map(mapper) | ||
.toArray(); | ||
} | ||
|
||
public int getByteCount() { | ||
return byteCount; | ||
} | ||
|
||
public int getWindowSize() { | ||
return window.length; | ||
} | ||
} |
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
Oops, something went wrong.