|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package jdk.internal.module; |
|
|
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.io.UncheckedIOException; |
|
import java.lang.module.ModuleReader; |
|
import java.lang.module.ModuleReference; |
|
import java.nio.charset.StandardCharsets; |
|
import java.security.MessageDigest; |
|
import java.security.NoSuchAlgorithmException; |
|
import java.util.Arrays; |
|
import java.util.Collections; |
|
import java.util.HashMap; |
|
import java.util.Map; |
|
import java.util.Objects; |
|
import java.util.Set; |
|
import java.util.TreeMap; |
|
import java.util.function.Supplier; |
|
|
|
/** |
|
* The result of hashing the contents of a number of module artifacts. |
|
*/ |
|
|
|
public final class ModuleHashes { |
|
|
|
|
|
|
|
*/ |
|
public static interface HashSupplier { |
|
byte[] generate(String algorithm); |
|
} |
|
|
|
private final String algorithm; |
|
private final Map<String, byte[]> nameToHash; |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
ModuleHashes(String algorithm, Map<String, byte[]> nameToHash) { |
|
this.algorithm = Objects.requireNonNull(algorithm); |
|
this.nameToHash = Collections.unmodifiableMap(nameToHash); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public String algorithm() { |
|
return algorithm; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public Set<String> names() { |
|
return nameToHash.keySet(); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public byte[] hashFor(String mn) { |
|
return nameToHash.get(mn); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public Map<String, byte[]> hashes() { |
|
return nameToHash; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private static byte[] computeHash(ModuleReader reader, String algorithm) { |
|
MessageDigest md; |
|
try { |
|
md = MessageDigest.getInstance(algorithm); |
|
} catch (NoSuchAlgorithmException e) { |
|
throw new IllegalArgumentException(e); |
|
} |
|
try { |
|
byte[] buf = new byte[32*1024]; |
|
reader.list().sorted().forEach(rn -> { |
|
md.update(rn.getBytes(StandardCharsets.UTF_8)); |
|
try (InputStream in = reader.open(rn).orElseThrow()) { |
|
int n; |
|
while ((n = in.read(buf)) > 0) { |
|
md.update(buf, 0, n); |
|
} |
|
} catch (IOException ioe) { |
|
throw new UncheckedIOException(ioe); |
|
} |
|
}); |
|
} catch (IOException ioe) { |
|
throw new UncheckedIOException(ioe); |
|
} |
|
return md.digest(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static byte[] computeHash(Supplier<ModuleReader> supplier, String algorithm) { |
|
try (ModuleReader reader = supplier.get()) { |
|
return computeHash(reader, algorithm); |
|
} catch (IOException ioe) { |
|
throw new UncheckedIOException(ioe); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static ModuleHashes generate(Set<ModuleReference> mrefs, String algorithm) { |
|
Map<String, byte[]> nameToHash = new TreeMap<>(); |
|
for (ModuleReference mref : mrefs) { |
|
try (ModuleReader reader = mref.open()) { |
|
byte[] hash = computeHash(reader, algorithm); |
|
nameToHash.put(mref.descriptor().name(), hash); |
|
} catch (IOException ioe) { |
|
throw new UncheckedIOException(ioe); |
|
} |
|
} |
|
return new ModuleHashes(algorithm, nameToHash); |
|
} |
|
|
|
@Override |
|
public int hashCode() { |
|
int h = algorithm.hashCode(); |
|
for (Map.Entry<String, byte[]> e : nameToHash.entrySet()) { |
|
h = h * 31 + e.getKey().hashCode(); |
|
h = h * 31 + Arrays.hashCode(e.getValue()); |
|
} |
|
return h; |
|
} |
|
|
|
@Override |
|
public boolean equals(Object obj) { |
|
if (!(obj instanceof ModuleHashes)) |
|
return false; |
|
ModuleHashes other = (ModuleHashes) obj; |
|
if (!algorithm.equals(other.algorithm) |
|
|| nameToHash.size() != other.nameToHash.size()) |
|
return false; |
|
for (Map.Entry<String, byte[]> e : nameToHash.entrySet()) { |
|
String name = e.getKey(); |
|
byte[] hash = e.getValue(); |
|
if (!Arrays.equals(hash, other.nameToHash.get(name))) |
|
return false; |
|
} |
|
return true; |
|
} |
|
|
|
@Override |
|
public String toString() { |
|
StringBuilder sb = new StringBuilder(algorithm); |
|
sb.append(" "); |
|
nameToHash.entrySet() |
|
.stream() |
|
.sorted(Map.Entry.comparingByKey()) |
|
.forEach(e -> { |
|
sb.append(e.getKey()); |
|
sb.append("="); |
|
byte[] ba = e.getValue(); |
|
for (byte b : ba) { |
|
sb.append(String.format("%02x", b & 0xff)); |
|
} |
|
}); |
|
return sb.toString(); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public static class Builder { |
|
final String algorithm; |
|
final Map<String, byte[]> nameToHash; |
|
|
|
Builder(String algorithm, int initialCapacity) { |
|
this.nameToHash = new HashMap<>(initialCapacity); |
|
this.algorithm = Objects.requireNonNull(algorithm); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public Builder hashForModule(String mn, byte[] hash) { |
|
nameToHash.put(mn, hash); |
|
return this; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public ModuleHashes build() { |
|
if (!nameToHash.isEmpty()) { |
|
return new ModuleHashes(algorithm, nameToHash); |
|
} else { |
|
return null; |
|
} |
|
} |
|
} |
|
} |