-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyFileWriter.java
More file actions
82 lines (69 loc) · 2.7 KB
/
MyFileWriter.java
File metadata and controls
82 lines (69 loc) · 2.7 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
import java.io.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.math.BigInteger;
import java.security.MessageDigest;
public class MyFileWriter {
static void writeHiddenPass(String filename, String contents) {
try (BufferedOutputStream writer =
new BufferedOutputStream(new FileOutputStream(filename))) {
writer.write(contents.getBytes());
} catch (IOException e) {
}
}
static void writeInHiddenVault(String hiddenVault, String filename, String contents) {
File folder = new File(hiddenVault);
File file = new File(folder, filename);
try (BufferedOutputStream writer =
new BufferedOutputStream(new FileOutputStream(file))) {
writer.write(contents.getBytes());
} catch (IOException e) {
}
}
private static void printTotalFileSize(String... fileNames) {
long totalSize = 0;
for (String fileName : fileNames) {
File file = new File(fileName);
if (file.exists()) {
totalSize += file.length();
}
}
System.out.println("Total size of all files: " + totalSize + " bytes");
}
private static void toString(String fileName) {
File f = new File(fileName);
System.out.println(f.toString());
}
// help from geeksforgeeks.com:https://www.geeksforgeeks.org/java/sha-256-hash-in-java
private static String toHexString(byte[] hash) {
BigInteger number = new BigInteger(1, hash);
StringBuilder hexString = new StringBuilder(number.toString(16));
while (hexString.length() < 64) {
hexString.insert(0, '0');
}
return hexString.toString();
}
public static String hashFile(String path) {
try {
byte[] data = Files.readAllBytes(Paths.get(path));
MessageDigest md = MessageDigest.getInstance("SHA-256");
return toHexString(md.digest(data));
} catch (NoSuchFileException e) {
System.err.println("File not found: " + path);
} catch (NoSuchAlgorithmException e) {
System.err.println("SHA-256 not available");
} catch (java.io.IOException e) {
System.err.println("I/O error: " + e.getMessage());
}
return null;
}
public static void main(String[] args) {
writeHiddenPass(".my_pass.txt", "the safest password:12345");
writeInHiddenVault(".my_hidden_vault", "my_stuff.txt",
"pizza, dogs, Matthew");
// printFileSize("test.txt");
// printFileSize("test1.txt");
}
}