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
No related branches found
No related tags found
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 {
private
final
static
int
BUFFER_SIZE
=
16
*
1024
;
static
public
List
<
HashValue
>
calculateHashes
(
Content
content
,
Collection
<
HashType
>
hashTypes
)
throws
TskCoreException
{
List
<
HashValue
>
results
=
new
ArrayList
<>();
/**
* Calculate hashes of the content object.
*
* @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
<>();
for
(
HashType
type
:
hashTypes
)
{
try
{
digests
.
put
(
type
,
MessageDigest
.
getInstance
(
type
.
getName
()));
...
...
@@ -51,33 +59,33 @@ static public List<HashValue> calculateHashes(Content content, Collection<HashTy
throw
new
TskCoreException
(
"No algorithm found matching name "
+
type
.
getName
(),
ex
);
}
}
// 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
read
;
for
(
int
i
=
0
;
i
<
totalChunks
;
i
++)
{
try
{
read
=
content
.
read
(
data
,
i
*
BUFFER_SIZE
,
BUFFER_SIZE
);
}
catch
(
TskCoreException
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.
if
(
read
==
BUFFER_SIZE
)
{
for
(
HashType
type
:
hashTypes
)
{
int
read
;
for
(
int
i
=
0
;
i
<
totalChunks
;
i
++)
{
try
{
read
=
content
.
read
(
data
,
i
*
BUFFER_SIZE
,
BUFFER_SIZE
);
}
catch
(
TskCoreException
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.
if
(
read
==
BUFFER_SIZE
)
{
for
(
HashType
type
:
hashTypes
)
{
digests
.
get
(
type
).
update
(
data
);
}
}
else
{
byte
[]
subData
=
Arrays
.
copyOfRange
(
data
,
0
,
read
);
for
(
HashType
type
:
hashTypes
)
{
digests
.
get
(
type
).
update
(
subData
);
}
}
}
}
}
else
{
byte
[]
subData
=
Arrays
.
copyOfRange
(
data
,
0
,
read
);
for
(
HashType
type
:
hashTypes
)
{
digests
.
get
(
type
).
update
(
subData
);
}
}
}
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
;
}
...
...
@@ -131,41 +139,42 @@ public static boolean isNoDataMd5(String md5) {
/**
* Utility class to hold a hash value along with its type.
*/
public
static
class
HashValue
{
HashType
type
;
String
value
;
public
HashValue
(
HashType
type
,
String
value
)
{
public
static
class
HashResult
{
private
final
HashType
type
;
private
final
String
value
;
public
HashResult
(
HashType
type
,
String
value
)
{
this
.
type
=
type
;
this
.
value
=
value
;
}
public
HashType
getType
()
{
return
type
;
}
public
String
getValue
()
{
return
value
;
}
}
/**
* Hash types that can be calculated.
*/
public
enum
HashType
{
MD5
(
"MD5"
),
SHA256
(
"SHA-256"
);
private
final
String
name
;
// This should be the string expected by MessageDigest
HashType
(
String
name
)
{
this
.
name
=
name
;
}
String
getName
()
{
return
name
;
}
}
MD5
(
"MD5"
),
SHA256
(
"SHA-256"
);
private
final
String
name
;
// This should be the string expected by MessageDigest
HashType
(
String
name
)
{
this
.
name
=
name
;
}
String
getName
()
{
return
name
;
}
}
/**
* Calculate the MD5 hash for the given FsContent and store it in the
...
...
@@ -177,7 +186,7 @@ String getName() {
*
* @throws java.io.IOException
*
* @deprecated
* @deprecated
Use calculateHashes() instead
*/
@Deprecated
static
public
String
calculateMd5
(
AbstractFile
file
)
throws
IOException
{
...
...
@@ -205,7 +214,7 @@ static public String calculateMd5(AbstractFile file) throws IOException {
@Deprecated
static
public
String
calculateMd5Hash
(
Content
content
)
throws
IOException
{
try
{
List
<
Hash
Value
>
results
=
calculateHashes
(
content
,
Arrays
.
asList
(
HashType
.
MD5
));
List
<
Hash
Result
>
results
=
calculateHashes
(
content
,
Arrays
.
asList
(
HashType
.
MD5
));
return
results
.
stream
()
.
filter
(
result
->
result
.
getType
().
equals
(
HashType
.
MD5
))
.
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