Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Sleuthkit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IRT
Sleuthkit
Commits
2c39dcf1
Commit
2c39dcf1
authored
4 years ago
by
apriestman
Browse files
Options
Downloads
Patches
Plain Diff
Cleanup
parent
2b67cb41
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bindings/java/src/org/sleuthkit/datamodel/HashUtility.java
+60
-51
60 additions, 51 deletions
bindings/java/src/org/sleuthkit/datamodel/HashUtility.java
with
60 additions
and
51 deletions
bindings/java/src/org/sleuthkit/datamodel/HashUtility.java
+
60
−
51
View file @
2c39dcf1
...
@@ -38,12 +38,20 @@ public class HashUtility {
...
@@ -38,12 +38,20 @@ public class HashUtility {
private
final
static
int
BUFFER_SIZE
=
16
*
1024
;
private
final
static
int
BUFFER_SIZE
=
16
*
1024
;
/**
* Calculate hashes of the content object.
static
public
List
<
HashValue
>
calculateHashes
(
Content
content
,
Collection
<
HashType
>
hashTypes
)
throws
TskCoreException
{
*
List
<
HashValue
>
results
=
new
ArrayList
<>();
* @param content The content object to hash
* @param hashTypes The types of hash to compute
*
* @return A list of the hash results
*
* @throws TskCoreException
*/
static
public
List
<
HashResult
>
calculateHashes
(
Content
content
,
Collection
<
HashType
>
hashTypes
)
throws
TskCoreException
{
List
<
HashResult
>
results
=
new
ArrayList
<>();
Map
<
HashType
,
MessageDigest
>
digests
=
new
HashMap
<>();
Map
<
HashType
,
MessageDigest
>
digests
=
new
HashMap
<>();
for
(
HashType
type
:
hashTypes
)
{
for
(
HashType
type
:
hashTypes
)
{
try
{
try
{
digests
.
put
(
type
,
MessageDigest
.
getInstance
(
type
.
getName
()));
digests
.
put
(
type
,
MessageDigest
.
getInstance
(
type
.
getName
()));
...
@@ -51,33 +59,33 @@ static public List<HashValue> calculateHashes(Content content, Collection<HashTy
...
@@ -51,33 +59,33 @@ static public List<HashValue> calculateHashes(Content content, Collection<HashTy
throw
new
TskCoreException
(
"No algorithm found matching name "
+
type
.
getName
(),
ex
);
throw
new
TskCoreException
(
"No algorithm found matching name "
+
type
.
getName
(),
ex
);
}
}
}
}
// Read in byte size chunks and update the hash value with the data.
// Read in byte size chunks and update the hash value with the data.
byte
[]
data
=
new
byte
[
BUFFER_SIZE
];
byte
[]
data
=
new
byte
[
BUFFER_SIZE
];
int
totalChunks
=
(
int
)
Math
.
ceil
((
double
)
content
.
getSize
()
/
(
double
)
BUFFER_SIZE
);
int
totalChunks
=
(
int
)
Math
.
ceil
((
double
)
content
.
getSize
()
/
(
double
)
BUFFER_SIZE
);
int
read
;
int
read
;
for
(
int
i
=
0
;
i
<
totalChunks
;
i
++)
{
for
(
int
i
=
0
;
i
<
totalChunks
;
i
++)
{
try
{
try
{
read
=
content
.
read
(
data
,
i
*
BUFFER_SIZE
,
BUFFER_SIZE
);
read
=
content
.
read
(
data
,
i
*
BUFFER_SIZE
,
BUFFER_SIZE
);
}
catch
(
TskCoreException
ex
)
{
}
catch
(
TskCoreException
ex
)
{
throw
new
TskCoreException
(
"Error reading data at address "
+
i
*
BUFFER_SIZE
+
" from content with ID: "
+
content
.
getId
(),
ex
);
throw
new
TskCoreException
(
"Error reading data at address "
+
i
*
BUFFER_SIZE
+
" from content with ID: "
+
content
.
getId
(),
ex
);
}
}
// Only update with the read bytes.
// Only update with the read bytes.
if
(
read
==
BUFFER_SIZE
)
{
if
(
read
==
BUFFER_SIZE
)
{
for
(
HashType
type
:
hashTypes
)
{
for
(
HashType
type
:
hashTypes
)
{
digests
.
get
(
type
).
update
(
data
);
digests
.
get
(
type
).
update
(
data
);
}
}
}
else
{
}
else
{
byte
[]
subData
=
Arrays
.
copyOfRange
(
data
,
0
,
read
);
byte
[]
subData
=
Arrays
.
copyOfRange
(
data
,
0
,
read
);
for
(
HashType
type
:
hashTypes
)
{
for
(
HashType
type
:
hashTypes
)
{
digests
.
get
(
type
).
update
(
subData
);
digests
.
get
(
type
).
update
(
subData
);
}
}
}
}
}
}
for
(
HashType
type
:
hashTypes
)
{
for
(
HashType
type
:
hashTypes
)
{
results
.
add
(
new
Hash
Value
(
type
,
DatatypeConverter
.
printHexBinary
(
digests
.
get
(
type
).
digest
()).
toLowerCase
()));
results
.
add
(
new
Hash
Result
(
type
,
DatatypeConverter
.
printHexBinary
(
digests
.
get
(
type
).
digest
()).
toLowerCase
()));
}
}
return
results
;
return
results
;
}
}
...
@@ -131,41 +139,42 @@ public static boolean isNoDataMd5(String md5) {
...
@@ -131,41 +139,42 @@ public static boolean isNoDataMd5(String md5) {
/**
/**
* Utility class to hold a hash value along with its type.
* Utility class to hold a hash value along with its type.
*/
*/
public
static
class
HashValue
{
public
static
class
HashResult
{
HashType
type
;
String
value
;
private
final
HashType
type
;
private
final
String
value
;
public
HashValue
(
HashType
type
,
String
value
)
{
public
HashResult
(
HashType
type
,
String
value
)
{
this
.
type
=
type
;
this
.
type
=
type
;
this
.
value
=
value
;
this
.
value
=
value
;
}
}
public
HashType
getType
()
{
public
HashType
getType
()
{
return
type
;
return
type
;
}
}
public
String
getValue
()
{
public
String
getValue
()
{
return
value
;
return
value
;
}
}
}
}
/**
/**
* Hash types that can be calculated.
* Hash types that can be calculated.
*/
*/
public
enum
HashType
{
public
enum
HashType
{
MD5
(
"MD5"
),
MD5
(
"MD5"
),
SHA256
(
"SHA-256"
);
SHA256
(
"SHA-256"
);
private
final
String
name
;
// This should be the string expected by MessageDigest
private
final
String
name
;
// This should be the string expected by MessageDigest
HashType
(
String
name
)
{
HashType
(
String
name
)
{
this
.
name
=
name
;
this
.
name
=
name
;
}
}
String
getName
()
{
String
getName
()
{
return
name
;
return
name
;
}
}
}
}
/**
/**
* Calculate the MD5 hash for the given FsContent and store it in the
* Calculate the MD5 hash for the given FsContent and store it in the
...
@@ -177,7 +186,7 @@ String getName() {
...
@@ -177,7 +186,7 @@ String getName() {
*
*
* @throws java.io.IOException
* @throws java.io.IOException
*
*
* @deprecated
* @deprecated
Use calculateHashes() instead
*/
*/
@Deprecated
@Deprecated
static
public
String
calculateMd5
(
AbstractFile
file
)
throws
IOException
{
static
public
String
calculateMd5
(
AbstractFile
file
)
throws
IOException
{
...
@@ -205,7 +214,7 @@ static public String calculateMd5(AbstractFile file) throws IOException {
...
@@ -205,7 +214,7 @@ static public String calculateMd5(AbstractFile file) throws IOException {
@Deprecated
@Deprecated
static
public
String
calculateMd5Hash
(
Content
content
)
throws
IOException
{
static
public
String
calculateMd5Hash
(
Content
content
)
throws
IOException
{
try
{
try
{
List
<
Hash
Value
>
results
=
calculateHashes
(
content
,
Arrays
.
asList
(
HashType
.
MD5
));
List
<
Hash
Result
>
results
=
calculateHashes
(
content
,
Arrays
.
asList
(
HashType
.
MD5
));
return
results
.
stream
()
return
results
.
stream
()
.
filter
(
result
->
result
.
getType
().
equals
(
HashType
.
MD5
))
.
filter
(
result
->
result
.
getType
().
equals
(
HashType
.
MD5
))
.
findFirst
().
get
().
getValue
();
.
findFirst
().
get
().
getValue
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment