Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

In this page you can find the example usage for java.security MessageDigest getInstance.

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:Main.java

public final synchronized static byte[] md5(byte[] buff) throws NoSuchAlgorithmException {
    MessageDigest md5Obj = MessageDigest.getInstance("MD5");
    md5Obj.reset();//w  ww  . j a va 2 s  . c  o  m
    return md5Obj.digest(buff);
}

From source file:Main.java

public static byte[] doubleSha256(byte[] bytes) {
    try {//from   www  . j  av  a2 s  . co m
        MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
        return sha256.digest(sha256.digest(bytes));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:auguste.client.command.client.AccountCreate.java

private static String hashPassword(String password) {
    try {// w ww  .  j  av a  2  s .co m
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.reset();
        digest.update(password.getBytes());
        return new String(Hex.encodeHex(digest.digest()));
    } catch (NoSuchAlgorithmException e) {
        // Algorithme indisponible
        System.out.println(e.getMessage());
        return new String();
    }
}

From source file:com.playonlinux.utils.Checksum.java

public static String calculate(File fileToCheck, String algorithm)
        throws NoSuchAlgorithmException, IOException {
    FileInputStream inputStream = new FileInputStream(fileToCheck);
    MessageDigest messageDigest = MessageDigest.getInstance(algorithm);

    return Hex.encodeHexString(getDigest(inputStream, messageDigest));
}

From source file:Main.java

public static String getBinaryHash(File apk, String algo) {
    FileInputStream fis = null;/*from  w w  w .j  a  v a2s .c o m*/
    BufferedInputStream bis = null;
    try {
        MessageDigest md = MessageDigest.getInstance(algo);
        fis = new FileInputStream(apk);
        bis = new BufferedInputStream(fis);

        byte[] dataBytes = new byte[524288];
        int nread = 0;

        while ((nread = bis.read(dataBytes)) != -1)
            md.update(dataBytes, 0, nread);

        byte[] mdbytes = md.digest();
        return toHexString(mdbytes);
    } catch (IOException e) {
        Log.e("FDroid", "Error reading \"" + apk.getAbsolutePath() + "\" to compute " + algo + " hash.");
        return null;
    } catch (NoSuchAlgorithmException e) {
        Log.e("FDroid", "Device does not support " + algo + " MessageDisgest algorithm");
        return null;
    } finally {
        closeQuietly(fis);
    }
}

From source file:MD5Sum.java

/**
 * Create a MD5 checksum for a file//from w  w  w. j  a v a2  s  .  c o  m
 *
 * @param filename
 * @return md5sum
 * @throws Exception
 */
public static byte[] createChecksum(InputStream fis) throws NoSuchAlgorithmException, IOException {
    if (fis == null) {
        throw new FileNotFoundException("InputStream cannot be read");
    }

    byte[] buffer = new byte[1024];
    MessageDigest complete = MessageDigest.getInstance("MD5");
    int numRead;
    do {
        numRead = fis.read(buffer);
        if (numRead > 0) {
            complete.update(buffer, 0, numRead);
        }
    } while (numRead != -1);
    fis.close();
    return complete.digest();
}

From source file:fm.audiobox.core.utils.MD5Checksum.java

/**
 * Calculate checksum of a File using MD5 algorithm
 *//*  w  ww . j av a 2s.c  o  m*/
public static String checkSum(InputStream is) throws NoSuchAlgorithmException {
    String checksum = null;
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();

        // Using MessageDigest update() method to provide input
        byte[] buffer = new byte[8192];
        int numOfBytesRead;
        while ((numOfBytesRead = is.read(buffer)) > 0) {
            md.update(buffer, 0, numOfBytesRead);
        }
        byte[] hash = md.digest();
        checksum = new String(Hex.encodeHex(hash));
    } catch (IOException ex) {
        logger.error(ex.getMessage());
    }

    return checksum;
}

From source file:Main.java

public static void decrypt(String fileIn, String fileOut, byte key[])
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream(fileIn);
    FileOutputStream fos = new FileOutputStream(fileOut);

    // Length is 32 bytes
    //byte key[] = "1010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100001111000011".getBytes("UTF-8");
    MessageDigest sha = MessageDigest.getInstance("SHA-256");
    key = sha.digest(key);//from ww  w.  j  a  v a  2s.c  om
    SecretKeySpec sks = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);

    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while ((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }

    fos.flush();
    fos.close();
    cis.close();
}

From source file:Main.java

public static String imageDigest(BufferedImage img) {

    // write image to byte stream
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {/*from www.j a va2s  .co  m*/
        ImageIO.write(img, "png", os);
        os.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    byte[] data = os.toByteArray();

    // compute md5 hash
    byte[] hash = null;
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(data);
        hash = md.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }

    // convert to string
    String hexString = "";
    for (int i = 0; i < hash.length; i++) {
        hexString += Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1);
    }
    return hexString;
}

From source file:in.bookmylab.Utils.java

public static String hashPassword(String password) {
    String hashed = null;//from   w  w  w.j  a  v a  2 s. com
    try {
        if (!StringUtils.isBlank(password)) {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest((salt + password).getBytes(StandardCharsets.ISO_8859_1));
            //hashed = Base64.getEncoder().encodeToString(hash);
            hashed = Base64.encodeBase64String(hash);
        }
    } catch (NoSuchAlgorithmException ex) {
        log.log(Level.SEVERE, "Could not hash string.", ex);
    }
    return hashed;
}