Skip to content

Commit

Permalink
Add option to pass file SHA-1 hash for upload integrity (#502)
Browse files Browse the repository at this point in the history
* Add option to pass file SHA-1 hash for upload integrity

Added a sha1 attribute to FileUploadParams to enable sending the
expected hash of the file for verification that the file was not
corrupted in transit.

Fixes #328

* Add empty check for hash
  • Loading branch information
Matt Willer authored and carycheng committed Jan 4, 2018
1 parent 55bd11f commit 42d4272
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/box/sdk/BoxFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ public BoxFile.Info uploadFile(FileUploadParams uploadParams) {
fieldJSON.add("content_modified_at", BoxDateFormat.format(uploadParams.getModified()));
}

if (uploadParams.getSHA1() != null && !uploadParams.getSHA1().isEmpty()) {
request.setContentSHA1(uploadParams.getSHA1());
}

request.putField("attributes", fieldJSON.toString());

if (uploadParams.getSize() > 0) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/box/sdk/FileUploadParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class FileUploadParams {
private Date modified;
private long size;
private ProgressListener listener;
private String sha1;

/**
* Constructs a new FileUploadParams with default parameters.
Expand Down Expand Up @@ -126,4 +127,22 @@ public FileUploadParams setProgressListener(ProgressListener listener) {
this.listener = listener;
return this;
}

/**
* Set the SHA-1 hash of the file to ensure it is not corrupted during the upload.
* @param sha1 the SHA-1 hash of the file.
* @return this FileUploadParams for chaining.
*/
public FileUploadParams setSHA1(String sha1) {
this.sha1 = sha1;
return this;
}

/**
* Gets the file's SHA-1 hash.
* @return the file hash.
*/
public String getSHA1() {
return this.sha1;
}
}
43 changes: 43 additions & 0 deletions src/test/java/com/box/sdk/BoxFolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,49 @@ public void uploadFileWithCreatedAndModifiedDatesSucceeds() {
uploadedFile.delete();
}

@Test
@Category(UnitTest.class)
public void testUploadFileWithSHA1SetsCorrectHeader() {

final String sha1 = "1f09d30c707d53f3d16c530dd73d70a6ce7596a9";

BoxAPIConnection api = new BoxAPIConnection("");
api.setRequestInterceptor(new RequestInterceptor() {
@Override
public BoxAPIResponse onRequest(BoxAPIRequest request) {
Assert.assertEquals(
"https://upload.box.com/api/2.0/files/content",
request.getUrl().toString());

List<BoxAPIRequest.RequestHeader> headers = request.getHeaders();

boolean foundHeader = false;

for (BoxAPIRequest.RequestHeader header : headers) {

if (header.getKey() == "Content-MD5" && header.getValue() == sha1) {
foundHeader = true;
}
}

assertTrue(foundHeader);

return new BoxJSONResponse() {
@Override
public String getJSON() {
return "{\"entries\":[{\"id\": \"0\"}]}";
}
};
}
});

FileUploadParams uploadParams = new FileUploadParams();
uploadParams.setSHA1(sha1);

BoxFolder folder = new BoxFolder(api, "0");
folder.uploadFile(uploadParams);
}

@Test
@Category(IntegrationTest.class)
public void updateFolderInfoSucceeds() {
Expand Down

0 comments on commit 42d4272

Please sign in to comment.