Skip to content

Commit

Permalink
[api] Uses sha-256 to avoid security warning (#2495)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu authored Mar 31, 2023
1 parent 212bc14 commit 6aedd50
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 16 deletions.
10 changes: 0 additions & 10 deletions api/src/main/java/ai/djl/repository/AbstractRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -264,15 +263,6 @@ private void untar(InputStream is, Path dir, boolean gzip) throws IOException {
}
}

protected static String md5hash(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
return Hex.toHexString(md.digest(input.getBytes(StandardCharsets.UTF_8)));
} catch (NoSuchAlgorithmException e) {
throw new AssertionError("MD5 algorithm not found.", e);
}
}

private static Map<String, String> parseQueryString(URI uri) {
try {
Map<String, String> map = new ConcurrentHashMap<>();
Expand Down
4 changes: 3 additions & 1 deletion api/src/main/java/ai/djl/repository/JarRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import ai.djl.Application;
import ai.djl.repository.zoo.DefaultModelZoo;
import ai.djl.util.Progress;
import ai.djl.util.Utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -122,7 +123,8 @@ private synchronized Metadata getMetadata() {
metadata = new Metadata.MatchAllMetadata();
metadata.setArtifactId(artifactId);
metadata.setArtifacts(Collections.singletonList(artifact));
String hash = md5hash(queryString == null ? uri.toString() : uri.toString() + queryString);
String hash =
Utils.hash(queryString == null ? uri.toString() : uri.toString() + queryString);
MRL mrl = model(Application.UNDEFINED, DefaultModelZoo.GROUP_ID, hash);
metadata.setRepositoryUri(mrl.toURI());

Expand Down
3 changes: 2 additions & 1 deletion api/src/main/java/ai/djl/repository/SimpleRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import ai.djl.repository.Artifact.Item;
import ai.djl.repository.zoo.DefaultModelZoo;
import ai.djl.util.Progress;
import ai.djl.util.Utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -192,7 +193,7 @@ private synchronized Metadata getMetadata() throws IOException {
files.put(artifactId, item);
artifact.setFiles(files);

String hash = md5hash(uri);
String hash = Utils.hash(uri);
MRL mrl = model(Application.UNDEFINED, DefaultModelZoo.GROUP_ID, hash);
metadata.setRepositoryUri(mrl.toURI());
} else {
Expand Down
3 changes: 2 additions & 1 deletion api/src/main/java/ai/djl/repository/SimpleUrlRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import ai.djl.Application;
import ai.djl.repository.zoo.DefaultModelZoo;
import ai.djl.util.Progress;
import ai.djl.util.Utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -124,7 +125,7 @@ private synchronized Metadata getMetadata() throws IOException {
metadata = new Metadata.MatchAllMetadata();
metadata.setArtifactId(artifactId);
metadata.setArtifacts(Collections.singletonList(artifact));
String hash = md5hash(uri.toString());
String hash = Utils.hash(uri.toString());
MRL mrl = model(Application.UNDEFINED, DefaultModelZoo.GROUP_ID, hash);
metadata.setRepositoryUri(mrl.toURI());
return metadata;
Expand Down
15 changes: 14 additions & 1 deletion api/src/main/java/ai/djl/util/Hex.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,25 @@ private Hex() {}
* @return the converted hex String
*/
public static String toHexString(byte[] block) {
return toHexString(block, 0, block.length);
}

/**
* Converts a byte array to a hex string.
*
* @param block the bytes to convert
* @param start the start position (inclusive) of the array
* @param end the end position (exclusive) of the array
* @return the converted hex String
*/
public static String toHexString(byte[] block, int start, int end) {
if (block == null) {
return null;
}

StringBuilder buf = new StringBuilder();
for (byte aBlock : block) {
for (int i = start; i < end; ++i) {
byte aBlock = block[i];
int high = ((aBlock & 0xf0) >> 4);
int low = (aBlock & 0x0f);

Expand Down
18 changes: 18 additions & 0 deletions api/src/main/java/ai/djl/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -460,4 +462,20 @@ public static InputStream openUrl(URL url) throws IOException {
}
return new BufferedInputStream(url.openStream());
}

/**
* Returns a hash of a string.
*
* @param input the input string
* @return a 20 bytes hash of the input stream in hex format
*/
public static String hash(String input) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] buf = md.digest(input.getBytes(StandardCharsets.UTF_8));
return Hex.toHexString(buf, 0, 20);
} catch (NoSuchAlgorithmException e) {
throw new AssertionError("SHA256 algorithm not found.", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ai.djl.repository.Repository;
import ai.djl.repository.zoo.DefaultModelZoo;
import ai.djl.util.Progress;
import ai.djl.util.Utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -165,7 +166,7 @@ private synchronized Metadata getMetadata() throws IOException {
}

metadata = new Metadata.MatchAllMetadata();
String hash = md5hash("s3://" + bucket + '/' + prefix);
String hash = Utils.hash("s3://" + bucket + '/' + prefix);
MRL mrl = model(Application.UNDEFINED, DefaultModelZoo.GROUP_ID, hash);
metadata.setRepositoryUri(mrl.toURI());
metadata.setArtifactId(artifactId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ai.djl.repository.Repository;
import ai.djl.repository.zoo.DefaultModelZoo;
import ai.djl.util.Progress;
import ai.djl.util.Utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
Expand Down Expand Up @@ -161,7 +162,7 @@ private synchronized Metadata getMetadata() throws IOException {
}

metadata = new Metadata.MatchAllMetadata();
String hash = md5hash(uri.resolve(prefix).toString());
String hash = Utils.hash(uri.resolve(prefix).toString());
MRL mrl = model(Application.UNDEFINED, DefaultModelZoo.GROUP_ID, hash);
metadata.setRepositoryUri(mrl.toURI());
metadata.setArtifactId(artifactId);
Expand Down

0 comments on commit 6aedd50

Please sign in to comment.