Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.nio.file.*;
- import java.util.Scanner;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- public class SHA256Sum {
- public static void main(String[] args) {
- if (args.length == 0) {
- try {
- printSHA256SystemIn();
- } catch (NoSuchAlgorithmException e) {
- System.err.println("ERROR: cannot find SHA-256 algorithm.");
- return;
- }
- } else {
- for (String filePath : args) {
- try {
- printSHA256(filePath);
- } catch (NoSuchAlgorithmException e) {
- System.err.println("ERROR: cannot find SHA-256 algorithm.");
- return;
- } catch (FileNotFoundException e) {
- System.err.println("ERROR: " + filePath + ": file not found.");
- return;
- } catch (IOException e) {
- System.err.println("ERROR: " + filePath + ": cannot read from file.");
- return;
- }
- }
- }
- }
- public static void printSHA256(String filePath) throws NoSuchAlgorithmException, IOException {
- MessageDigest md = MessageDigest.getInstance("SHA-256");
- md.update(Files.readAllBytes(Paths.get(filePath)));
- for (byte b : md.digest()) {
- System.out.print(String.format("%02x", b).toUpperCase());
- }
- System.out.println(" *" + filePath);
- }
- public static void printSHA256SystemIn() throws NoSuchAlgorithmException {
- MessageDigest md = MessageDigest.getInstance("SHA-256");
- Scanner input = new Scanner(System.in);
- while (input.hasNextLine()) {
- md.update(input.nextLine().getBytes());
- }
- for (byte b : md.digest()) {
- System.out.print(String.format("%02x", b).toUpperCase());
- }
- System.out.println(" *-");
- input.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement