Java Hash Calculate getHash(String algorithm, int i)

Here you can find the source of getHash(String algorithm, int i)

Description

get Hash

License

Apache License

Declaration

public static String getHash(String algorithm, int i) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String getHash(String algorithm, int i) throws IOException {
        return getHash(algorithm, String.valueOf(i));
    }//from w w w . j  a v a 2  s. c o  m

    public static String getHash(String algorithm, String string) throws IOException {
        MessageDigest messageDigest = null;

        try {
            messageDigest = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException nsae) {
            throw new IOException(nsae);
        }

        messageDigest.update(string.getBytes("UTF-8"));
        byte[] messageDigestBytes = messageDigest.digest();
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < messageDigestBytes.length; i++) {
            stringBuilder.append(Integer.toString((messageDigestBytes[i] & 0xff) + 0x100, 16).substring(1));
        }

        return stringBuilder.toString().toLowerCase();
    }

    private static String getHash(String algorithm, File file) throws IOException {
        MessageDigest messageDigest = null;

        try {
            messageDigest = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException nsae) {
            throw new IOException(nsae);
        }

        InputStream inputStream = null;

        byte[] dataBytes = new byte[1024];
        int bytesRead = 0;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            inputStream = new BufferedInputStream(new FileInputStream(file));

            while ((bytesRead = inputStream.read(dataBytes)) != -1) {
                messageDigest.update(dataBytes, 0, bytesRead);
            }

            byte[] messageDigestBytes = messageDigest.digest();

            for (int i = 0; i < messageDigestBytes.length; i++) {
                stringBuilder.append(Integer.toString((messageDigestBytes[i] & 0xff) + 0x100, 16).substring(1));
            }

            return stringBuilder.toString().toLowerCase();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Throwable throwable) {
                    // DO NOTHING
                }

                inputStream = null;
            }
        }
    }
}

Related

  1. getHash(final byte[] data, final String hashAlgorithm)
  2. getHash(final String text, final Charset charset, final MessageDigest algorithm)
  3. getHash(InputStream in, String algorithm)
  4. getHash(InputStream is, String algorithm)
  5. getHash(int iterationNb, String password, byte[] salt)
  6. getHash(String credentials)
  7. getHash(String Data)
  8. getHash(String fileName, String hashType)
  9. getHash(String givenStr)