檢視jks檔案的MD5

清清飞扬發表於2024-04-10

From: https://blog.51cto.com/u_16175512/7903626

package com.lckj.toolbox.util;

import lombok.extern.slf4j.Slf4j;
import lombok.var;

import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.MessageDigest;

@Slf4j
public class JksMD5Util {

    public static void main(String[] args) {
        var md5 = getMD5("E:\\xxxx\\android.jks", "112233");
        log.info("md5 = {}", md5);
    }

    public static String getMD5(String jksFilePath, String jksPassword) {
        String md5Value = null;
        try {
            // 開啟.jks檔案
            FileInputStream fis = new FileInputStream(jksFilePath);

            // 例項化KeyStore物件
            KeyStore keyStore = KeyStore.getInstance("JKS");

            // 載入.jks檔案
            keyStore.load(fis, jksPassword.toCharArray());

            // 計算MD5值
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[8192];
            int length;
            while ((length = fis.read(buffer)) != -1) {
                md.update(buffer, 0, length);
            }
            byte[] digest = md.digest();

            // 轉換為十六進位制字串
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02X", b & 0xff));
            }
            md5Value = sb.toString();

            // 關閉檔案流
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md5Value;
    }

}

  

相關文章