Help Center/ Video On Demand/ API Reference/ Appendix/ Generating an MD5 Value
Updated on 2024-03-01 GMT+08:00

Generating an MD5 Value

Media Upload and Update

When calling the APIs for uploading media files to VOD and video update, you can use video_md5 to configure the MD5 value of the media file. OBS will verify the MD5 value. For details, see Setting Object Properties.

The MD5 value is calculated using the standard MD5 hash algorithm and then encoded using Base64. The sample code is as follows:

 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
import org.apache.commons.codec.binary.Base64;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class VodDemoObsCheckMd5 {
    public String md5Generate4ObsCheck(String fileUrl)
        throws IOException, NoSuchAlgorithmException {
        String md5Content = null;

        if ((fileUrl != null) && (fileUrl.length() != 0)) {
            File file = new File(fileUrl);

            if (!file.exists()) {
                System.out.println("The file does not exist.");
            } else if (file.isDirectory()) {
                System.out.println(file.getCanonicalPath() + "is a directory and cannot be calculated.);
            } else {
                FileInputStream fis = new FileInputStream(file);
                DigestInputStream dis = new DigestInputStream(fis,
                        MessageDigest.getInstance("MD5"));
                byte[] buffer = new byte[8192];

                while (dis.read(buffer) > 0) {
                }

                md5Content = new String(Base64.encodeBase64(
                            dis.getMessageDigest().digest()));
                fis.getChannel().position(0L);
                System.out.println("The MD5 value of the file is "+ md5Content);
                fis.close();
            }
        } else {
            System.out.println("The file name is missing.");
        }

        return md5Content;
    }
}

Verification

VOD uses the MD5 values of media files to check whether duplicate media files exist when you call the API for verifying the upload. The MD5 value is generated based on the 1,024 bytes of the media file and calculated using the MD5 algorithm. The sample code is as follows:
 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
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import org.apache.commons.codec.digest.DigestUtils;

public class VodDemoDuplicateCheckMd5 {
    public static String computeMd5ByFile(String fileUrl) {
        String md5Content = null;
        Path targetFile = Paths.get(fileUrl);
        try (SeekableByteChannel channel = Files.newByteChannel(targetFile, StandardOpenOption.READ)) {
            ByteBuffer byteBuffer = ByteBuffer.allocate(1025);
            channel.read(byteBuffer);
            byteBuffer.flip();
            byte[] data = new byte[byteBuffer.limit()];
            byteBuffer.get(data);
            md5Content = DigestUtils.md5Hex(data);
        } catch (IOException e) {
            throw new RuntimeException(String.format("Read file %s failed.", fileUrl));
        }
        return md5Content;
    }
}