-
-
Notifications
You must be signed in to change notification settings - Fork 315
Set QuickXorHash as checksum to compare file changes #17311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c7fe8f7
Add tests.
dkocher dfef899
Add UUID.
dkocher e2a8f37
Set UUID as checksum to compare file changes.
dkocher fc84dbc
Review
dkocher 2cce356
Review.
dkocher 284686b
Add null check.
dkocher 1105ed5
Extract constant.
dkocher facb3cd
Rename.
dkocher 4fced78
Update dependency.
dkocher 193f493
Revert "Add UUID."
dkocher b19a8bd
Add support to parse Base64 encoded hex strings.
dkocher 857620d
Adjust to failure thrown parsing hexadecimal string.
dkocher 5fb166e
Adjust tests.
dkocher 27e69bc
Parse QuickXorHash.
dkocher 9934617
Fix test.
dkocher 6c9d000
Fix test.
dkocher 32f07b6
Fallback to Hex parsing.
dkocher a64c143
Check cancelation flag.
dkocher 9c1423b
Check cancelation flag.
dkocher 46bc3b1
Make stricter.
dkocher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -18,6 +18,9 @@ | |
* [email protected] | ||
*/ | ||
|
||
import ch.cyberduck.core.exception.UnsupportedException; | ||
|
||
import org.apache.commons.codec.DecoderException; | ||
import org.apache.commons.codec.binary.Base64; | ||
import org.apache.commons.codec.binary.Hex; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
@@ -42,11 +45,16 @@ public Checksum(final HashAlgorithm algorithm, final byte[] digest) { | |
this.base64 = Base64.encodeBase64String(digest); | ||
} | ||
|
||
public Checksum(final HashAlgorithm algorithm, final String hexString) { | ||
public Checksum(final HashAlgorithm algorithm, final String hexString) throws UnsupportedException { | ||
this.algorithm = algorithm; | ||
this.hash = hexString; | ||
this.hex = hexString; | ||
this.base64 = null; | ||
try { | ||
this.base64 = Base64.encodeBase64String(Hex.decodeHex(hexString)); | ||
} | ||
catch(DecoderException e) { | ||
throw new UnsupportedException(e); | ||
} | ||
} | ||
|
||
public Checksum(final HashAlgorithm algorithm, final String hexString, final String base64String) { | ||
|
@@ -65,41 +73,58 @@ public Checksum(final Checksum other) { | |
|
||
@Override | ||
public String toString() { | ||
return hash; | ||
return hex; | ||
} | ||
|
||
public static Checksum parse(final String hash) { | ||
if(StringUtils.isBlank(hash)) { | ||
return Checksum.NONE; | ||
} | ||
switch(hash.length()) { | ||
case 8: | ||
if(hash.matches("[a-fA-F0-9]{8}")) { | ||
return new Checksum(HashAlgorithm.crc32, hash); | ||
} | ||
break; | ||
case 32: | ||
if(hash.matches("[a-fA-F0-9]{32}")) { | ||
return new Checksum(HashAlgorithm.md5, hash); | ||
} | ||
break; | ||
case 40: | ||
if(hash.matches("[a-fA-F0-9]{40}")) { | ||
return new Checksum(HashAlgorithm.sha1, hash); | ||
} | ||
break; | ||
case 64: | ||
if(hash.matches("[A-Fa-f0-9]{64}")) { | ||
return new Checksum(HashAlgorithm.sha256, hash); | ||
} | ||
break; | ||
case 128: | ||
if(hash.matches("[A-Fa-f0-9]{128}")) { | ||
return new Checksum(HashAlgorithm.sha512, hash); | ||
} | ||
break; | ||
default: | ||
log.warn("Failure to detect algorithm for checksum {}", hash); | ||
// Check for Base64 with proper padding | ||
if(hash.matches("^[A-Za-z0-9+/]+={0,2}$") && hash.length() % 4 == 0) { | ||
final Checksum result = parseHex(Hex.encodeHexString(Base64.decodeBase64(hash))); | ||
if(result != Checksum.NONE) { | ||
return new Checksum(result.algorithm, result.hex, hash); | ||
} | ||
} | ||
// Parse as hex string | ||
return parseHex(hash); | ||
} | ||
|
||
private static Checksum parseHex(final String hexString) { | ||
try { | ||
switch(hexString.length()) { | ||
case 8: | ||
if(hexString.matches("[a-fA-F0-9]{8}")) { | ||
return new Checksum(HashAlgorithm.crc32, hexString); | ||
} | ||
break; | ||
case 32: | ||
if(hexString.matches("[a-fA-F0-9]{32}")) { | ||
return new Checksum(HashAlgorithm.md5, hexString); | ||
} | ||
break; | ||
case 40: | ||
if(hexString.matches("[a-fA-F0-9]{40}")) { | ||
return new Checksum(HashAlgorithm.sha1, hexString); | ||
} | ||
break; | ||
case 64: | ||
if(hexString.matches("[A-Fa-f0-9]{64}")) { | ||
return new Checksum(HashAlgorithm.sha256, hexString); | ||
} | ||
break; | ||
case 128: | ||
if(hexString.matches("[A-Fa-f0-9]{128}")) { | ||
return new Checksum(HashAlgorithm.sha512, hexString); | ||
} | ||
break; | ||
default: | ||
log.warn("Failure to detect algorithm for checksum {}", hexString); | ||
} | ||
} | ||
catch(UnsupportedException e) { | ||
return Checksum.NONE; | ||
} | ||
return Checksum.NONE; | ||
} | ||
|
@@ -116,7 +141,7 @@ public boolean equals(final Object o) { | |
if(algorithm != checksum.algorithm) { | ||
return false; | ||
} | ||
if(!StringUtils.equalsIgnoreCase(hash, checksum.hash)) { | ||
if(!StringUtils.equalsIgnoreCase(hex, checksum.hex)) { | ||
return false; | ||
} | ||
return true; | ||
|
@@ -125,7 +150,7 @@ public boolean equals(final Object o) { | |
@Override | ||
public int hashCode() { | ||
int result = algorithm != null ? algorithm.hashCode() : 0; | ||
result = 31 * result + (hash != null ? hash.hashCode() : 0); | ||
result = 31 * result + (hex != null ? hex.hashCode() : 0); | ||
return result; | ||
} | ||
|
||
|
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.