-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
83 lines (74 loc) · 2.85 KB
/
MyFileWriter.java
File metadata and controls
83 lines (74 loc) · 2.85 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
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MyFileWriter {
public static void createHiddenPasswordFile() {
Path p = Paths.get(".secretpassword.txt");
try (BufferedWriter bw = Files.newBufferedWriter(p, StandardCharsets.UTF_8)) {
bw.write("My$uperSecretP@ss");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createConfidentialInHiddenFolder() {
Path p = Paths.get(".classified", "confidential.dat");
try (BufferedWriter bw = Files.newBufferedWriter(p, StandardCharsets.UTF_8)) {
bw.write("Confidential information goes here.");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void printFileSize(String fileName) {
File file = new File(fileName);
if (file.exists() && file.isFile()) {
System.out.println("File size of " + fileName + ": " + file.length() + " bytes");
} else {
System.out.println("File " + fileName + " does not exist.");
}
}
public static String toString(InputStream input) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append(System.lineSeparator());
}
}
return sb.toString();
}
public static String hashFile(String filePath) throws IOException {
Path path = Paths.get(filePath);
if (!Files.exists(path) || !Files.isRegularFile(path)) {
throw new FileNotFoundException("File not found: " + filePath);
}
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-256 algorithm not available");
}
InputStream fis = Files.newInputStream(path);
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
md.update(buffer, 0, bytesRead);
}
fis.close();
byte[] hashBytes = md.digest();
StringBuilder hexString = new StringBuilder();
for (int i = 0; i < hashBytes.length; i++) {
String hex = Integer.toHexString(0xff & hashBytes[i]);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
public static void main(String[] args) {
createHiddenPasswordFile();
createConfidentialInHiddenFolder();
}
}