LoginSignup
1

More than 3 years have passed since last update.

Javaでファイルのハッシュ値を返却するサンプルプログラム

Last updated at Posted at 2020-04-02

javaでファイルのハッシュ値を返却するサンプルプログラムです。

SampleMain.java
package test;

public class SampleMain {

    public static void main(String[] args) {
        System.out.println(SampleHash
                .getfileHash("glassfish-5.0.1.zip", SampleHash.SHA_512));
    }
}
SampleHash.java
package test;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SampleHash {

    /** MD2アルゴリズム */
    public static final String MD2 = "MD2";
    /** MD5アルゴリズム */
    public static final String MD5 = "MD5";
    /** SHA-1アルゴリズム */
    public static final String SHA_1 = "SHA-1";
    /** SHA-256アルゴリズム */
    public static final String SHA_256 = "SHA-256";
    /** SHA-512アルゴリズム */
    public static final String SHA_512 = "SHA-512";

    /**
     * ファイルのハッシュ値(文字列)を返す
     * @param filePath ファイルパス
     * @param algorithmName アルゴリズム
     * @return ハッシュ値(文字列)
     */
    public static String getfileHash(String filePath, String algorithmName) {

        Path path = Paths.get(filePath);

        byte[] hash = null;

        // アルゴリズム取得
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance(algorithmName);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        try (
                // 入力ストリームの生成
                DigestInputStream dis = new DigestInputStream(
                        new BufferedInputStream(Files.newInputStream(path)), md)) {

            // ファイルの読み込み
            while (dis.read() != -1) {
            }

            // ハッシュ値の計算
            hash = md.digest();

        } catch (IOException e) {
            e.printStackTrace();
        }

        // ハッシュ値(byte)を文字列に変換し返却
        StringBuilder sb = new StringBuilder();
        for (byte b : hash) {
            String hex = String.format("%02x", b);
            sb.append(hex);
        }
        return sb.toString();
    }
}

java.security.DigestInputStreamjava.security.MessageDigestを使用してハッシュ値を取得しています。
取得したハッシュ値(byte)を文字列に変換して返却しています。


以上

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1