Skip to content

Commit d65f93b

Browse files
authored
Merge pull request #100 from trendmicro/bugfix_issue_99
Java fix for issue #99
2 parents 9e7d50c + 0034af2 commit d65f93b

File tree

14 files changed

+808
-468
lines changed

14 files changed

+808
-468
lines changed

java/.classpath

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
3-
<classpathentry kind="src" path="src/main/java"/>
4-
<classpathentry kind="src" path="src/test/java"/>
5-
<classpathentry kind="src" path="src/main/resources"/>
3+
<classpathentry kind="src" output="bin/main" path="src/main/java">
4+
<attributes>
5+
<attribute name="gradle_scope" value="main"/>
6+
<attribute name="gradle_used_by_scope" value="main,test"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="bin/main" path="src/main/resources">
10+
<attributes>
11+
<attribute name="gradle_scope" value="main"/>
12+
<attribute name="gradle_used_by_scope" value="main,test"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="src" output="bin/test" path="src/test/java">
16+
<attributes>
17+
<attribute name="gradle_scope" value="test"/>
18+
<attribute name="gradle_used_by_scope" value="test"/>
19+
<attribute name="test" value="true"/>
20+
</attributes>
21+
</classpathentry>
622
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7/"/>
723
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
8-
<classpathentry kind="output" path="bin"/>
24+
<classpathentry kind="output" path="bin/default"/>
925
</classpath>

java/.project

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<projectDescription>
33
<name>tlsh</name>
4-
<comment>Project tlsh created by Buildship.</comment>
4+
<comment></comment>
55
<projects>
66
</projects>
77
<buildSpec>
@@ -17,7 +17,7 @@
1717
</buildCommand>
1818
</buildSpec>
1919
<natures>
20-
<nature>org.eclipse.jdt.core.javanature</nature>
2120
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
21+
<nature>org.eclipse.jdt.core.javanature</nature>
2222
</natures>
2323
</projectDescription>
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
21
connection.project.dir=
32
eclipse.preferences.version=1
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
eclipse.preferences.version=1
2-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
1+
#
2+
#Tue Apr 20 07:46:11 EDT 2021
3+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
34
org.eclipse.jdt.core.compiler.compliance=1.7
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
7+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
8+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
9+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
10+
eclipse.preferences.version=1
11+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
412
org.eclipse.jdt.core.compiler.source=1.7
13+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error

java/README.md

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,16 @@ This port has no additional JAR dependencies and only requires the JRE.
66

77
## Use with gradle
88

9-
Pre-built versions of TLSH are hosted on bintray and can be used in gradle build scripts as follows:
9+
Pre-built TLSH libraries will be available on Maven Central soon but for now they
10+
must be built from source.
1011

11-
```
12-
repositories {
13-
jcenter()
14-
// ... other repositories
15-
}
16-
17-
dependencies {
18-
compile 'com.trendmicro:tlsh:3.7.1'
19-
20-
// ... other dependencies
21-
}
12+
```bash
13+
git clone https://github.com/trendmicro/tlsh.git
14+
cd tlsh/java
15+
./gradlew clean build
2216
```
2317

24-
## Use with Maven
25-
26-
Pre-built versions of TLSH can be used in a Maven pom.xml file as follows:
27-
28-
```xml
29-
<repositories>
30-
<repository>
31-
<id>jcenter</id>
32-
<url>https://jcenter.bintray.com/</url>
33-
</repository>
34-
<!-- ... other repositories -->
35-
</repositories>
36-
37-
<dependencies>
38-
<dependency>
39-
<groupId>com.trendmicro</groupId>
40-
<artifactId>tlsh</artifactId>
41-
<version>3.7.1</version>
42-
<type>pom</type>
43-
</dependency>
44-
<!-- ... other dependencies -->
45-
</dependencies>
46-
```
18+
This will produce JAR files in `build/libs` that can be included in other projects.
4719

4820
## Example
4921
```java

java/build.gradle

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,16 @@
66
* user guide available at https://docs.gradle.org/3.5/userguide/java_library_plugin.html
77
*/
88

9+
// Apply the required plugins
910
plugins {
10-
id "com.jfrog.bintray" version "1.7.3"
11+
id 'java'
12+
id 'java-library'
13+
id 'eclipse'
1114
}
1215

13-
// Apply the required plugins
14-
apply plugin: 'maven'
15-
apply plugin: 'maven-publish'
16-
apply plugin: 'java'
17-
apply plugin: 'java-library'
18-
1916
// In this section you declare where to find the dependencies of your project
2017
repositories {
21-
// Use jcenter for resolving your dependencies.
22-
// You can declare any Maven/Ivy/file repository here.
23-
jcenter()
18+
mavenCentral()
2419
}
2520

2621
dependencies {
@@ -29,7 +24,7 @@ dependencies {
2924
}
3025

3126
sourceCompatibility = 1.7
32-
version = '3.7.1'
27+
version = '4.5.0'
3328
def globalVersion = version
3429
group = 'com.trendmicro'
3530

@@ -40,41 +35,6 @@ jar {
4035
}
4136
}
4237

43-
// To upload to bintray, run
44-
// ./gradlew bintrayUpload
45-
bintray {
46-
user = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER')
47-
key = project.hasProperty('bintrayApiKey') ? project.property('bintrayApiKey') : System.getenv('BINTRAY_API_KEY')
48-
49-
pkg {
50-
repo = 'tlsh'
51-
name = 'TLSH'
52-
53-
version {
54-
name = globalVersion
55-
desc = 'TLSH - Trend Micro Locality Sensitive Hash'
56-
released = new Date()
57-
}
58-
59-
}
60-
61-
publications = ['tlsh']
62-
}
63-
64-
// Create the publication with the pom configuration:
65-
publishing {
66-
publications {
67-
tlsh(MavenPublication) {
68-
from components.java
69-
artifact javadocJar
70-
artifact sourcesJar
71-
groupId 'com.trendmicro'
72-
artifactId 'tlsh'
73-
version globalVersion
74-
}
75-
}
76-
}
77-
7838
task sourcesJar(type: Jar, dependsOn: classes) {
7939
classifier = 'sources'
8040
from sourceSets.main.allSource

java/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip

java/settings.gradle

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,4 @@
88
* in the user guide at https://docs.gradle.org/3.5/userguide/multi_project_builds.html
99
*/
1010

11-
/*
12-
// To declare projects as part of a multi-project build use the 'include' method
13-
include 'shared'
14-
include 'api'
15-
include 'services:webservice'
16-
*/
17-
1811
rootProject.name = 'tlsh'

java/src/main/java/com/trendmicro/tlsh/Tlsh.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,26 @@ public class Tlsh {
8787
* If the given string cannot be parsed correctly
8888
*/
8989
public static Tlsh fromTlshStr(String tlshStr) throws IllegalArgumentException {
90+
VersionOption versionOption = null;
9091
int[] checksum = null;
9192
int[] tmp_code = null;
9293
for (BucketOption bucketOption : BucketOption.values()) {
9394
for (ChecksumOption checksumOption : ChecksumOption.values()) {
94-
if (tlshStr.length() == hashStringLength(bucketOption, checksumOption)) {
95-
checksum = new int[checksumOption.getChecksumLength()];
96-
tmp_code = new int[bucketOption.getBucketCount() / 4];
97-
}
95+
for (VersionOption tryVersion : VersionOption.values()) {
96+
if (tlshStr.length() == hashStringLength(bucketOption, checksumOption, tryVersion)) {
97+
checksum = new int[checksumOption.getChecksumLength()];
98+
tmp_code = new int[bucketOption.getBucketCount() / 4];
99+
versionOption = tryVersion;
100+
break;
101+
}
102+
}
98103
}
99104
}
100105
if (checksum == null) {
101106
throw new IllegalArgumentException("Invalid hash string, length does not match any known encoding");
102107
}
103108

104-
int offset = 0;
109+
int offset = versionOption.getVersionString().length();
105110
for (int k = 0; k < checksum.length; k++) {
106111
checksum[k] = TlshUtil.from_hex_swapped(tlshStr, offset);
107112
offset += 2;
@@ -119,34 +124,44 @@ public static Tlsh fromTlshStr(String tlshStr) throws IllegalArgumentException {
119124
offset += 2;
120125
}
121126

122-
return new Tlsh(checksum, Lvalue, qRatios >> 4, qRatios & 0xF, tmp_code);
127+
return new Tlsh(versionOption, checksum, Lvalue, qRatios >> 4, qRatios & 0xF, tmp_code);
123128
}
124129

125130
/**
126131
* Get the length of the encoded output string for different
127132
* hash creation options
128133
*/
129-
private static int hashStringLength(BucketOption bucketOption, ChecksumOption checksumOption) {
130-
return (bucketOption.getBucketCount() / 2) + (checksumOption.getChecksumLength() * 2) + 4;
134+
private static int hashStringLength(BucketOption bucketOption, ChecksumOption checksumOption, VersionOption versionOption) {
135+
return versionOption.getVersionString().length() + (bucketOption.getBucketCount() / 2) + (checksumOption.getChecksumLength() * 2) + 4;
131136
}
132137

133138
/////////////////////////////////////////////////////////////
134139
// Instance stuff
135140
//
141+
private final VersionOption version;
136142
private final int[] checksum; // 1 or 3 bytes
137143
private final int Lvalue; // 1 byte
138144
private final int Q1ratio; // 4 bits
139145
private final int Q2ratio; // 4 bits
140146
private final int[] codes; // 32/64 bytes
141147

142-
Tlsh(int[] checksum, int lvalue, int q1ratio, int q2ratio, int[] codes) {
148+
Tlsh(VersionOption versionOption, int[] checksum, int lvalue, int q1ratio, int q2ratio, int[] codes) {
149+
this.version = versionOption;
143150
this.checksum = checksum;
144151
Lvalue = lvalue;
145152
Q1ratio = q1ratio;
146153
Q2ratio = q2ratio;
147154
this.codes = codes;
148155
}
149156

157+
/**
158+
* Return the version this hash was created with
159+
* @return the version this hash was created with
160+
*/
161+
public VersionOption getVersion() {
162+
return version;
163+
}
164+
150165
/**
151166
* Convert this object to a string; equivalent to {@link #getEncoded()}
152167
*
@@ -166,6 +181,7 @@ public String getEncoded() {
166181
// The C++ code reverses the order of some of the fields before
167182
// converting to hex, so copy that behaviour.
168183
StringBuilder sb = new StringBuilder(hashStringLength());
184+
sb.append(version.getVersionString());
169185

170186
for (int k = 0; k < checksum.length; k++) {
171187
TlshUtil.to_hex_swapped(checksum[k], sb);
@@ -185,7 +201,7 @@ public String getEncoded() {
185201
*/
186202
private int hashStringLength() {
187203
// extra 4 characters come from length and Q1 and Q2 ratio.
188-
return codes.length * 2 + checksum.length * 2 + 4;
204+
return version.getVersionString().length() + codes.length * 2 + checksum.length * 2 + 4;
189205
}
190206

191207
/**

0 commit comments

Comments
 (0)