Object Storage Service (OSS) SDK for Android provides methods that can be used to verify data integrity and ensure data security when you upload, download, and copy objects.

Due to the complex environment of mobile networks, an error may occur when data is transferred between the client and server. OSS SDK for Android provides the CRC-64 and MD5 verification methods to ensure end-to-end data integrity.

CRC-64

If CRC-64 is enabled, OSS automatically checks data integrity after OSS reads data from a stream.

The following sample code provides an example on how to enable CRC-64:

GetObjectRequest request = new GetObjectRequest(OSSTestConfig.ANDROID_TEST_BUCKET, testFile);
// Enable CRC-64. 
request.setCRC64(OSSRequest.CRC64Config.YES);
//....
try{
    GetObjectResult result = oss.getObject(request);
    InputStream in = result.getObjectContent();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new byte[BUFFER_SIZE];
    int len;
    while ((len = in.read(buffer)) > -1) {
            output.write(buffer, 0, len);
    }
    output.flush();
    in.close();
}catch(ClientException e){
    //...
}catch(InconsistentException e){
    //....
}

MD5 verification

To check whether the object that you uploaded by performing multipart upload is the same as the local file, you can upload the parts with the Content-MD5 value specified in the upload request. The OSS server helps you with the MD5 verification. The upload is successful only if the MD5 hash of the object received by the OSS server is the same as the Content-MD5 value. This method ensures the consistency of the uploaded object.

The following sample code provides an example on how to enable MD5 verification:

// Specify the name of the bucket and the full path of the object. In this example, the name of the bucket is examplebucket and the full path of the object is exampledir/exampleobject.txt. 
UploadPartRequest uploadPart = new UploadPartRequest("examplebucket", "exampledir/exampleobject.txt", uploadId, currentIndex); 
// Specify the part content. 
uploadPart.setPartContent(partData);  
// Specify the MD5 hash. 
uploadPart.setMd5Digest(BinaryUtil.calculateBase64Md5(data));