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:com.amazonaws.encryptionsdk.internal.TestIOUtils.java

public static byte[] computeFileDigest(final String fileName) throws IOException {
    try {/*  w ww  .  j av  a  2s. co m*/
        final FileInputStream fis = new FileInputStream(fileName);
        final MessageDigest md = MessageDigest.getInstance("SHA-256");
        final DigestInputStream dis = new DigestInputStream(fis, md);

        final int readLen = 128;
        final byte[] readBytes = new byte[readLen];
        while (dis.read(readBytes) != -1) {
        }
        dis.close();

        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        // shouldn't get here since we hardcode the algorithm.
    }

    return null;
}

From source file:Main.java

public static byte[] calculateMd5(byte[] binaryData) {
    MessageDigest messageDigest = null;
    try {//from w w  w.  j a  v  a 2 s . c  om
        messageDigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("MD5 algorithm not found.");
    }
    messageDigest.update(binaryData);
    return messageDigest.digest();
}

From source file:com.codemage.sql.util.PasswordHasher.java

public String getHashcode(String passwordToHash) {
    String generatedPassword = null;
    try {/*from  w  ww  .  j  ava2 s .c  om*/
        // Create MessageDigest instance for MD5
        MessageDigest md = MessageDigest.getInstance("MD5");
        //Add password bytes to digest
        md.update(passwordToHash.getBytes());
        //Get the hash's bytes 
        byte[] bytes = md.digest();
        //This bytes[] has bytes in decimal format;
        //Convert it to hexadecimal format
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        //Get complete hashed password in hex format
        generatedPassword = sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return generatedPassword;
}

From source file:Main.java

/**
 * Returns an MD-5 digest of the database encryption password.
 * <blockquote>This algorithm performs an MD-5 hash on a UTF-8 representation of the password.</blockquote>
 *
 * @param pass The plain encryption password
 *
 * @return The database encryption password digest
 *
 * @throws NoSuchAlgorithmException If the platform doesn't support MD-5
 *///  w  w  w.  ja  va  2s. com
public static byte[] passToDigest(char[] pass) throws NoSuchAlgorithmException {
    // FIXME: Enhance security for this method
    Charset cs = Charset.forName("UTF-8");
    MessageDigest md = MessageDigest.getInstance("MD5");
    ByteBuffer bbuf = cs.encode(CharBuffer.wrap(pass));
    byte[] bpass = Arrays.copyOf(bbuf.array(), bbuf.remaining());

    return md.digest(bpass);
}

From source file:function.Functions.java

protected static String MD5(String text) {
    String md5Text = "";
    try {//from ww  w  .  j a  v a 2 s . c o  m
        MessageDigest digest = MessageDigest.getInstance("MD5");
        md5Text = new BigInteger(1, digest.digest((text).getBytes())).toString(16);
    } catch (Exception e) {
        System.out.println("Error in call to MD5");
    }

    if (md5Text.length() == 31) {
        md5Text = "0" + md5Text;
    }
    return md5Text;
}

From source file:org.confab.Utilities.java

public static String md5(String pass) {
    String md5 = "";
    try {//from   ww w. j  av a 2  s.  co  m
        MessageDigest m = MessageDigest.getInstance("MD5");
        byte[] data = pass.getBytes();
        m.update(data, 0, data.length);
        BigInteger i = new BigInteger(1, m.digest());
        md5 = String.format("%1$032x", i);
    } catch (NoSuchAlgorithmException e) {
        System.out.println(e);
    }
    return md5;
}

From source file:com.astamuse.asta4d.web.util.SecureIdGenerator.java

public static String createEncryptedURLSafeId() {
    try {//w  ww .jav a2 s  .c  om
        byte[] idBytes = IdGenerator.createIdBytes();

        ByteBuffer bb = ByteBuffer.allocate(idBytes.length + 4);
        bb.put(idBytes);

        // add random salt
        bb.putInt(sr.nextInt());

        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
        return Base64.encodeBase64URLSafeString(crypt.digest(bb.array()));

    } catch (NoSuchAlgorithmException e) {
        // impossible
        throw new RuntimeException(e);
    }
}

From source file:it.cilea.osd.jdyna.utils.HashUtil.java

public static String hashMD5(String passw) {
    String passwHash = "";
    try {//from w  w w .  ja v a  2 s  .  co  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(passw.getBytes());
        byte[] result = md.digest();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            String tmpStr = "0" + Integer.toHexString((0xff & result[i]));
            sb.append(tmpStr.substring(tmpStr.length() - 2));
        }
        passwHash = sb.toString();
    } catch (NoSuchAlgorithmException ecc) {
        log.error("Errore algoritmo " + ecc);
    }
    return passwHash;
}

From source file:hjow.hgtable.util.SecurityUtil.java

/**
 * <p>?? ? .</p>/*www.  j a v a 2 s  . c o m*/
 * 
 * @param text : ?? ?
 * @param algorithm :  
 * @return 
 */
public static String hash(String text, String algorithm) {
    MessageDigest digest = null;
    String methods = algorithm;
    if (methods == null)
        methods = "SHA-256";

    try {
        digest = MessageDigest.getInstance(methods);

        byte[] beforeBytes = text.getBytes("UTF-8");
        byte[] afterBytes = digest.digest(beforeBytes);

        StringBuffer results = new StringBuffer("");

        for (int i = 0; i < afterBytes.length; i++) {
            results.append(Integer.toString(((afterBytes[i] & 0xf0) >> 4), 16));
            results.append(Integer.toString((afterBytes[i] & 0x0f), 16));
        }

        return String.valueOf(results);
    } catch (Throwable e) {

    }
    return null;
}

From source file:Main.java

/** Calculates the MD5 hash of a given string 
 * @author Tom V. http://m2tec.be/blog/2010/02/03/java-md5-hex-0093 
 * @param stringToBeHashed the string to be hashed 
 * @return the MD5 hash as a string /*from www.  ja  v  a2  s.  c o  m*/
 * @throws NoSuchAlgorithmException */
public static String stringToMD5(String stringToBeHashed) throws NoSuchAlgorithmException {
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    StringBuffer md5Hash = new StringBuffer();
    byte[] array = messageDigest.digest(stringToBeHashed.getBytes());

    for (int i = 0; i < array.length; i++) {
        md5Hash.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
    }

    return md5Hash.toString();
}