对象存储服务 OBS-Header中携带签名:Java中Content-MD5的计算方法示例

时间:2024-10-21 15:06:41

Java中Content-MD5的计算方法示例

您可以选择在构造StringToSign时加入Content-MD5的头域和头域值,以下为计算Content-MD5头域值的代码示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.security.MessageDigest;
import sun.misc.BASE64Encoder;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

public class Md5{
     public static void main(String[] args) {
          try {
                 String exampleString = "blog";
                 MessageDigest messageDigest = MessageDigest.getInstance("MD5"); 
                 BASE64Encoder encoder = new BASE64Encoder(); 
                 //计算字符串的MD5值,然后将MD5值进行Base64编码
                 String contentMd5 = encoder.encode(messageDigest.digest(exampleString.getBytes("utf-8")));
                 System.out.println("Content-MD5:" + contentMd5);    
          } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) 
          {
                 e.printStackTrace();
          }
     }
}
support.huaweicloud.com/api-obs/obs_04_0010.html