Example usage for java.security MessageDigest update

List of usage examples for java.security MessageDigest update

Introduction

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

Prototype

public final void update(ByteBuffer input) 

Source Link

Document

Update the digest using the specified ByteBuffer.

Usage

From source file:Main.java

private static byte[] generateKey(String strSrc) {

    MessageDigest digest;

    byte[] keyBytes = new byte[32];
    try {/*  w  w  w .jav a  2s .co m*/
        digest = MessageDigest.getInstance("SHA-256");
        digest.update(strSrc.getBytes("UTF-8"));
        System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return keyBytes;
}

From source file:com.sammyun.util.MD5.java

/**
 * @param str//  w  w  w  .  ja  v  a2s .c o m
 * @return
 * @throws NoSuchAlgorithmException
 */
public static String crypt(String str) throws NoSuchAlgorithmException {
    if (str == null || str.length() == 0) {
        throw new IllegalArgumentException("String to encript cannot be null or zero length");
    }

    StringBuffer hexString = new StringBuffer();

    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(str.getBytes());
    byte[] hash = md.digest();

    for (byte aHash : hash) {
        if ((OXFF & aHash) < OX10) {
            hexString.append("0").append(Integer.toHexString((OXFF & aHash)));
        } else {
            hexString.append(Integer.toHexString(OXFF & aHash));
        }
    }

    return hexString.toString();
}

From source file:ezbake.warehaus.WarehausUtils.java

public static Text getKey(String uri) {
    try {//w  w  w.  ja v  a 2  s.c om
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(uri.getBytes());
        byte[] hash = messageDigest.digest();
        BigInteger bigInt = new BigInteger(1, hash);
        String hashtext = bigInt.toString(16);
        return new Text(hashtext + ":" + uri);
    } catch (NoSuchAlgorithmException e) {
        // We hopefully should never end up here
        logger.error("NoSuchAlgorithmException thrown while attempting " + "to hash key.", e);
        throw new RuntimeException(e);
    }
}

From source file:br.com.manish.ahy.kernel.util.HashUtil.java

public static String getHash(String text) {
    String ret = null;//from  ww  w  .j  ava  2s . c  o m
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(text.getBytes());
        ret = getHex(md.digest());
    } catch (NoSuchAlgorithmException e) {
        log.error(e);
        throw new OopsException(e, "Hash Error.");
    }
    return ret;
}

From source file:com.smallfe.clerk.util.DigestUtil.java

public static String getSHA256(String message) {
    try {/*from ww  w  . j a v  a 2  s . com*/
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(message.getBytes());

        byte byteData[] = md.digest();

        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(DigestUtil.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:org.nebulaframework.util.hashing.SHA1Generator.java

/**
 * Generates SHA1 Hash for the given {@code byte[]} and returns
 * the hash code as a {@code byte[]}.//www  .ja  v a  2s .c  o m
 * 
 * @param source source value
 * @return SHA-1 hash for value
 * 
 * @see #generate(String)
 * @see #generateAsString(byte[])
 * 
 * @throws IllegalArgumentException if {@code source} is {@code null}
 */
public static byte[] generate(byte[] source) throws IllegalArgumentException {

    // Check for nulls
    Assert.notNull(source);

    try {
        MessageDigest digest = MessageDigest.getInstance("SHA");
        digest.update(source);
        return digest.digest();
    } catch (NoSuchAlgorithmException e) {
        log.fatal("Cannot load hashing algorithm", e);
        throw new RuntimeException(e);
    }
}

From source file:cn.ucloud.sdk.utils.EncoderUtils.java

private static String encode(String algorithm, String str) {
    String result = null;//  w w  w  .  j ava2  s.c om
    if (str == null) {
        return result;
    }
    try {
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        messageDigest.update(str.getBytes("UTF-8"));
        result = getFormattedText(messageDigest.digest());
    } catch (Exception e) {
        LogUtils.exception(logger, e);
    }
    return result;
}

From source file:Main.java

private static byte[] computeHash(String x) {
    java.security.MessageDigest d = null;
    try {/*from w  w w  .  j  a va  2  s. co m*/
        d = java.security.MessageDigest.getInstance("SHA-1");
        d.reset();
        d.update(x.getBytes());
        return d.digest();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

public static byte[] computeSHA1(ByteBuffer convertme, int offset, int len) {
    int oldp = convertme.position();
    int oldl = convertme.limit();
    try {/*from  w w  w . j a  v a  2s  . c  o m*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        convertme.position(offset);
        convertme.limit(len);
        md.update(convertme);
        return md.digest();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        convertme.limit(oldl);
        convertme.position(oldp);
    }
    return new byte[20];
}

From source file:com.ikanow.infinit.e.api.authentication.PasswordEncryption.java

/**
 *  Encrypt the password/*from w w w.j  a va  2s  .c o m*/
 * @throws NoSuchAlgorithmException 
 * @throws UnsupportedEncodingException 
 */
public static String encrypt(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(password.getBytes("UTF-8"));
    return Base64.encodeBase64String(md.digest());
}