-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHashing.java
More file actions
81 lines (65 loc) · 2.61 KB
/
Hashing.java
File metadata and controls
81 lines (65 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.nio.file.*;
import java.security.MessageDigest;
import java.util.zip.Deflater;
import java.io.ByteArrayOutputStream;
public class Hashing {
public static boolean compress = false;
//helper method for hashString
public static String hashStringfilePath(String filePath){
try {
byte[] Bytes = Files.readAllBytes(Paths.get(filePath));
return hashString(Bytes);
} catch (Exception e) {
System.out.println("Cant find file: " + filePath);
}
return null;
}
//Hashes with the SHA-1 algorithim
public static String hashString(byte[] Bytes){
try{
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] hashBytes = digest.digest(Bytes);
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
hexString.append(String.format("%02x", b));
}
//System.out.println("The hash for " + Files.readString(Paths.get(filePath)) + " is: " + hexString.toString());
return hexString.toString();
} catch (Exception e){
System.out.println("No SHA-1 encription");
}
return null;
}
//https://docs.oracle.com/javase/8/docs/api/java/util/zip/Deflater.html
//https://www.baeldung.com/java-compress-uncompress-byte-array
public static byte[] fileCompressor(String filePath){
try {
byte[] Bytes = Files.readAllBytes(Paths.get(filePath));
Deflater deflater = new Deflater();
deflater.setInput(Bytes);
deflater.finish();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (!deflater.finished()) {
int compressedSize = deflater.deflate(buffer);
outputStream.write(buffer, 0, compressedSize);
}
return outputStream.toByteArray();
} catch(java.io.UnsupportedEncodingException ex) {
System.out.println("UnsupportedEncodingExceptipon");
} catch (Exception e) {
System.out.println("git stuff prob not found (fileCompressor)" + filePath);
}
return null;
}
public static String CompressOrNot(String filePath){
String SHAHash = "";
if (compress == true){
SHAHash = Hashing.hashString(fileCompressor(filePath));
//System.out.println("Compressed shahash is: " + SHAHash);
} else{
SHAHash = Hashing.hashStringfilePath(filePath);
}
return SHAHash;
}
}