Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

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

public class Main {
    private static String sha256Hex(final String filePath) throws NoSuchAlgorithmException, IOException {
        final InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
        final MessageDigest md = MessageDigest.getInstance("SHA-256");

        final byte[] dataBytes = new byte[1024];

        int nread;
        while ((nread = fis.read(dataBytes)) != -1)
            md.update(dataBytes, 0, nread);
        final byte[] mdbytes = md.digest();

        final StringBuilder sb = new StringBuilder();
        for (final byte b : mdbytes)
            sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));

        return sb.toString();
    }
}