-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashService.java
More file actions
196 lines (152 loc) · 7.06 KB
/
Copy pathHashService.java
File metadata and controls
196 lines (152 loc) · 7.06 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.securefileshare.services;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
public class HashService {
public String generateSHA256(byte[] fileData) {
if (fileData == null || fileData.length == 0) {
throw new IllegalArgumentException("File data cannot be null or empty for hashing");
}
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(fileData);
return HexFormat.of().formatHex(hash);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-256 algorithm not available", e);
}
}
public String generateSHA256WithLog(byte[] fileData, String fileName) {
System.out.println("Generating SHA-256 hash for: " + fileName);
System.out.println("Data size: " + fileData.length + " bytes");
long startTime = System.currentTimeMillis();
String hash = generateSHA256(fileData);
long endTime = System.currentTimeMillis();
System.out.println("Hash generated in " + (endTime - startTime) + "ms");
System.out.println("SHA-256 Hash: " + hash);
System.out.println("Hash length: " + hash.length() + " characters");
return hash;
}
public String generateMD5(byte[] fileData) {
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] hash = digest.digest(fileData);
return HexFormat.of().formatHex(hash);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MD5 algorithm not available", e);
}
}
public String generateSHA512(String input) {
if (input == null || input.trim().isEmpty()) {
throw new IllegalArgumentException("Input cannot be null or empty");
}
try {
MessageDigest digest = MessageDigest.getInstance("SHA-512");
byte[] hash = digest.digest(input.getBytes());
return HexFormat.of().formatHex(hash);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-512 algorithm not available", e);
}
}
public String generateSHA512(byte[] data) {
if (data == null || data.length == 0) {
throw new IllegalArgumentException("Data cannot be null or empty");
}
try {
MessageDigest digest = MessageDigest.getInstance("SHA-512");
byte[] hash = digest.digest(data);
return HexFormat.of().formatHex(hash);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-512 algorithm not available", e);
}
}
public boolean verifyFileIntegrity(byte[] fileData, String expectedHash) {
if (expectedHash == null || expectedHash.trim().isEmpty()) {
throw new IllegalArgumentException("Expected hash cannot be null or empty");
}
System.out.println("Verifying file integrity...");
System.out.println("Expected hash: " + expectedHash.substring(0, Math.min(32, expectedHash.length())) + "...");
String actualHash = generateSHA256(fileData);
boolean matches = actualHash.equals(expectedHash);
System.out.println("Actual hash: " + actualHash.substring(0, Math.min(32, actualHash.length())) + "...");
System.out.println("Integrity check: " + (matches ? "✅ PASSED" : "❌ FAILED"));
if (!matches) {
System.err.println("WARNING: File integrity check failed!");
System.err.println("Expected: " + expectedHash);
System.err.println("Actual: " + actualHash);
}
return matches;
}
public boolean verifyFileIntegrityAdvanced(byte[] fileData, String expectedSHA256, String expectedMD5) {
boolean sha256Valid = false;
boolean md5Valid = false;
if (expectedSHA256 != null && !expectedSHA256.trim().isEmpty()) {
String actualSHA256 = generateSHA256(fileData);
sha256Valid = actualSHA256.equals(expectedSHA256);
System.out.println("SHA-256 check: " + (sha256Valid ? "✅" : "❌"));
}
if (expectedMD5 != null && !expectedMD5.trim().isEmpty()) {
String actualMD5 = generateMD5(fileData);
md5Valid = actualMD5.equals(expectedMD5);
System.out.println("MD5 check: " + (md5Valid ? "✅" : "❌"));
}
if (expectedSHA256 != null && expectedMD5 != null) {
return sha256Valid && md5Valid;
} else if (expectedSHA256 != null) {
return sha256Valid;
} else if (expectedMD5 != null) {
return md5Valid;
}
return false;
}
public String hashPassword(String password, String salt) {
if (password == null || password.trim().isEmpty()) {
throw new IllegalArgumentException("Password cannot be null or empty");
}
if (salt == null || salt.trim().isEmpty()) {
salt = generateRandomSalt();
}
String combined = password + salt;
String hash = generateSHA512(combined);
return salt + ":" + hash;
}
public boolean verifyPassword(String password, String storedHash) {
if (password == null || storedHash == null) {
return false;
}
String[] parts = storedHash.split(":", 2);
if (parts.length != 2) {
return false;
}
String salt = parts[0];
String expectedHash = parts[1];
String combined = password + salt;
String actualHash = generateSHA512(combined);
return actualHash.equals(expectedHash);
}
public String generateRandomSalt() {
byte[] salt = new byte[16];
new java.security.SecureRandom().nextBytes(salt);
return HexFormat.of().formatHex(salt);
}
public double calculateHashSimilarity(String hash1, String hash2) {
if (hash1 == null || hash2 == null || hash1.length() != hash2.length()) {
return 0.0;
}
int matchingChars = 0;
for (int i = 0; i < hash1.length(); i++) {
if (hash1.charAt(i) == hash2.charAt(i)) {
matchingChars++;
}
}
return (matchingChars * 100.0) / hash1.length();
}
public String generateShortHash(String fullHash, int length) {
if (fullHash == null || fullHash.length() < length) {
return fullHash;
}
return fullHash.substring(0, length) + "...";
}
public String generateFileHash(byte[] fileData) {
return generateSHA256(fileData);
}
}