forked from oras-project/oras-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigestUtils.java
113 lines (103 loc) · 3.41 KB
/
DigestUtils.java
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
package land.oras.utils;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;
import land.oras.OrasException;
import org.jspecify.annotations.NullMarked;
/**
* Digest utilities
*/
@NullMarked
public final class DigestUtils {
/**
* Utils class
*/
private DigestUtils() {}
/**
* Calculate the sha256 digest of a byte array
* @param bytes The bytes
* @return The digest
*/
public static String sha256(byte[] bytes) {
return digest("SHA-256", bytes);
}
/**
* Calculate the sha256 digest of a file
* @param file The file
* @return The digest
*/
public static String sha256(Path file) {
return digest("SHA-256", file);
}
/**
* Calculate the digest of a file
* @param algorithm The algorithm
* @param path The path
* @return The digest
*/
public static String digest(String algorithm, Path path) {
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
try (InputStream is = Files.newInputStream(path, StandardOpenOption.READ)) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
digest.update(buffer, 0, bytesRead);
}
}
byte[] hashBytes = digest.digest();
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b));
}
return "%s:%s".formatted(algorithm.toLowerCase().replaceAll("-", ""), sb.toString());
} catch (Exception e) {
throw new OrasException("Failed to calculate digest", e);
}
}
/**
* Calculate the digest of a byte array
* @param algorithm The algorithm
* @param bytes bytes
* @return The digest
*/
public static String digest(String algorithm, byte[] bytes) {
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
byte[] hashBytes = digest.digest(bytes);
// Convert the byte array to hex
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b));
}
return "%s:%s".formatted(algorithm.toLowerCase().replaceAll("-", ""), sb.toString());
} catch (Exception e) {
throw new OrasException("Failed to calculate digest", e);
}
}
/**
* Calculate the sha256 digest of a InputStream
* @param input The input
* @return The digest
*/
public static String sha256(InputStream input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
digest.update(buffer, 0, bytesRead);
}
byte[] hashBytes = digest.digest();
StringBuilder sb = new StringBuilder();
for (byte b : hashBytes) {
sb.append(String.format("%02x", b));
}
return "sha256:%s".formatted(sb.toString());
} catch (Exception e) {
throw new OrasException("Failed to calculate digest", e);
}
}
}