Example usage for java.security MessageDigest digest

List of usage examples for java.security MessageDigest digest

Introduction

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

Prototype

public byte[] digest() 

Source Link

Document

Completes the hash computation by performing final operations such as padding.

Usage

From source file:Main.java

/**
 * Gets the key hash of application package's certificate signature.
 * @since   0.1.1/*from  ww w . java 2  s.  c o m*/
 * @param   aContext The context from which the package manager is retrieved to get the signature's information.
 * @return   The list of signatures embedded to the application package.
 */
public static List<String> getKeyHash(Context aContext) {
    try {
        PackageInfo info = aContext.getPackageManager().getPackageInfo(getPackageName(aContext),
                PackageManager.GET_SIGNATURES);

        List<String> keyHashList = new ArrayList<String>(info.signatures.length);

        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            keyHashList.add(Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
        return keyHashList;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.continusec.client.Util.java

/**
 * Calculate the Merkle Tree Leaf Hash for an object (HASH(chr(0) || b)).
 * @param b the input to the leaf hash//  ww  w .  j ava2s.com
 * @return the leaf hash.
 */
public static final byte[] leafMerkleTreeHash(byte[] b) {
    MessageDigest d = DigestUtils.getSha256Digest();
    d.update((byte) 0);
    d.update(b);
    return d.digest();
}

From source file:Main.java

public static byte[] SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md;
    md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes(), 0, text.length());
    // "iso-8859-1"
    sha1hash = md.digest();
    return (sha1hash);
}

From source file:Main.java

/**
 * Digests the given message with the given algorithm
 *
 * @param originalMessage the original message
 * @param algorithm the algorithm//ww w .j a  v a2  s  .c o m
 * @return the byte [ ]
 * @throws NoSuchAlgorithmException if the given algorithm does not exist in JCA
 */
public static byte[] digestMessage(byte[] originalMessage, String algorithm) throws NoSuchAlgorithmException {
    MessageDigest messageDigest;
    messageDigest = MessageDigest.getInstance(algorithm);

    messageDigest.update(originalMessage, 0, originalMessage.length);
    return messageDigest.digest();
}

From source file:annis.utils.Utils.java

/** Hashes a string using SHA-256. */
public static String calculateSHAHash(String s) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(s.getBytes("UTF-8"));
    byte[] digest = md.digest();

    StringBuilder hashVal = new StringBuilder();
    for (byte b : digest) {
        hashVal.append(String.format("%02x", b));
    }/*from  w  w w  . j  a  v  a  2  s.c  o  m*/

    return hashVal.toString();
}

From source file:com.jopss.logico.negocio.util.CriptoUtils.java

public static String md5Encrypt(String texto) {
    String encripted = null;//from  w w  w. j  a  v  a  2s .co m
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(texto.getBytes());
        BigInteger hash = new BigInteger(1, md.digest());
        encripted = hash.toString(16);
    } catch (NoSuchAlgorithmException e) {
        log.error(e);
    }
    return encripted;
}

From source file:Main.java

public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    text = "l;jfa@#" + text + "fads@)*3 ";
    MessageDigest md;
    md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    return convertToHex(sha1hash);
}

From source file:StringUtil.java

/**
 * Encrypt password by using SHA-256 algorithm, encryptedPassword length is 32 bits
 * @param clearTextPassword/*from  w  w  w .jav  a  2s . co m*/
 * @return
 * @throws NoSuchAlgorithmException
 * reference http://java.sun.com/j2se/1.4.2/docs/api/java/security/MessageDigest.html
 */
public static String getEncryptedPassword(String clearTextPassword) {

    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(clearTextPassword.getBytes());
        return new sun.misc.BASE64Encoder().encode(md.digest());
    } catch (NoSuchAlgorithmException e) {
        //_log.error("Failed to encrypt password.", e);
    }
    return "";
}

From source file:com.olayinka.file.transfer.Utils.java

public static String hash(String string) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = null;
    md = MessageDigest.getInstance("SHA-256");
    md.update(string.getBytes());/*from ww  w . j  a  v  a2s  .  c o m*/
    byte[] digest = md.digest();
    return String.valueOf(Hex.encodeHex(digest));
}

From source file:ke.co.tawi.babblesms.server.utils.security.SecurityUtil.java

/**
 * Return the MD5 hahs of a String. It will work correctly for most 
 * strings. A word which does not work correctly is "michael" (check 
 * against online MD5 hash tools).   //from  www .  jav  a2  s  .co  m
 *
 * @param toHash plain text string to encryption
 * @return an md5 hashed string
 */
public static String getMD5Hash(String toHash) {
    String md5Hash = "";

    try {
        MessageDigest md = MessageDigest.getInstance("MD5");

        md.update(toHash.getBytes(), 0, toHash.length());

        md5Hash = new BigInteger(1, md.digest()).toString(16);

    } catch (NoSuchAlgorithmException e) {
        logger.error("NoSuchAlgorithmException while getting MD5 hash of '" + toHash + "'");
        logger.error(ExceptionUtils.getStackTrace(e));
    }

    return md5Hash;
}