Skip to content

Commit

Permalink
Merge pull request #287 from Omegaice/master
Browse files Browse the repository at this point in the history
Change hashing to ignore line feed differences between windows and linux
  • Loading branch information
rheimus authored Nov 28, 2021
2 parents 670df8d + c285152 commit 509ce1f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/test/resources/hash_lf.txt text eol=lf
src/test/resources/hash_crlf.txt text eol=crlf
42 changes: 38 additions & 4 deletions src/main/java/com/superzanti/serversync/files/FileHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
import com.superzanti.serversync.util.Logger;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.Arrays;

public class FileHash {
public static String hashFile(Path file) {
try (
try {
InputStream stream = null;
if( FileHash.isBinaryFile(file) ){
stream = Files.newInputStream(file);
}else{
stream = new ByteArrayInputStream(String.join("", Files.readAllLines(file, StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8));
}

DigestInputStream in = new DigestInputStream(
new BufferedInputStream(Files.newInputStream(file)),
new BufferedInputStream(stream),
MessageDigest.getInstance("SHA-256")
)
) {
);

byte[] buffer = new byte[8192];
while (in.read(buffer) > -1) { }
return String.format("%064x", new BigInteger(1, in.getMessageDigest().digest()));
Expand All @@ -26,4 +38,26 @@ public static String hashFile(Path file) {
}
return "";
}

private static boolean isBinaryFile(Path f) throws IOException {
String[] textMine = {"text", "application/xml", "application/json", "application/javascript", "application/vnd.ms-excel"};

String type = Files.probeContentType(f);
if (type == null) {
//type couldn't be determined, guess via first 8192 bytes
try (InputStream stream = new BufferedInputStream(Files.newInputStream(f))) {
byte[] buffer = new byte[8192];
int read = stream.read(buffer);
for( int i = 0; i < read; i++ ){
if(buffer[i] == 0x00) return true;
}
return false;
}
} else if (Arrays.stream(textMine).anyMatch(type::startsWith)) {
return false;
} else {
//type isn't text
return true;
}
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/superzanti/serversync/LineFeedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.superzanti.serversync;

import java.nio.file.Paths;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.superzanti.serversync.files.FileHash;
import com.superzanti.serversync.util.Logger;

class LineFeedTest {

public LineFeedTest() {
Logger.instantiate("test");
}

@Test
@DisplayName("Should hash text files ignoring line feeds")
void textHash() {
String hashLF = FileHash.hashFile(Paths.get(this.getClass().getResource("/hash_lf.txt").getPath()));
String hashCRLF = FileHash.hashFile(Paths.get(this.getClass().getResource("/hash_crlf.txt").getPath()));
assertEquals(hashLF, hashCRLF);
}
}
1 change: 1 addition & 0 deletions src/test/resources/hash_crlf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a file that has specific line endings.
1 change: 1 addition & 0 deletions src/test/resources/hash_lf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a file that has specific line endings.

0 comments on commit 509ce1f

Please sign in to comment.