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:com.liferay.mobile.android.util.PortraitUtil.java

protected static void appendToken(StringBuilder sb, String uuid) {
    if (Validator.isNull(uuid)) {
        return;//from w w w.java 2s  .  co  m
    }

    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.update(uuid.getBytes());

        byte[] bytes = digest.digest();
        String token = null;

        try {
            token = Base64.encodeToString(bytes, Base64.NO_WRAP);
        } catch (RuntimeException re) {
            if ("Stub!".equals(re.getMessage())) {
                token = org.apache.commons.codec.binary.Base64.encodeBase64String(bytes);
            }
        }

        if (token != null) {
            sb.append("&img_id_token=");
            sb.append(URLEncoder.encode(token, "UTF8"));
        }
    } catch (Exception e) {
        Log.e(_CLASS_NAME, "Couldn't generate portrait image token", e);
    }
}

From source file:de.innovationgate.utils.UIDGenerator.java

/**
 * Generates an MD5 hash from the given string
 * @param hashString//from   w w w. j  av  a  2s.  com
 */
public static String generateMD5Hash(String hashString) {
    MessageDigest md5;
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        return null;
    }

    md5.update(hashString.getBytes());

    byte[] array = md5.digest();
    StringBuffer sb = new StringBuffer();
    for (int j = 0; j < array.length; ++j) {
        int b = array[j] & 0xFF;
        if (b < 0x10)
            sb.append('0');
        sb.append(Integer.toHexString(b));
    }
    return sb.toString();
}

From source file:com.aqnote.shared.cryptology.digest.MD.java

public final static String md5(byte[] src) {
    if (src == null)
        return "";
    try {//from   ww w  .j a v  a  2s  .  co  m
        // MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        MessageDigest messageDigest = MessageDigest.getInstance(OID_MD5, JCE_PROVIDER);

        messageDigest.update(src);
        return new String(ByteUtil.toHexBytes(messageDigest.digest()));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:com.ec2box.manage.util.EncryptionUtil.java

/**
 * return hash value of string//  w w w .  j  a  v  a 2 s.com
 *
 * @param str  unhashed string
 * @param salt salt for hash
 * @return hash value of string
 */
public static String hash(String str, String salt) {
    String hash = null;
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        if (StringUtils.isNotEmpty(salt)) {
            md.update(Base64.decodeBase64(salt.getBytes()));
        }
        md.update(str.getBytes("UTF-8"));
        hash = new String(Base64.encodeBase64(md.digest()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return hash;
}

From source file:com.keybox.manage.util.EncryptionUtil.java

/**
 * return hash value of string/*  www. j a  v  a 2s. c om*/
 *
 * @param str  unhashed string
 * @param salt salt for hash
 * @return hash value of string
 */
public static String hash(String str, String salt) {
    String hash = null;
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        if (StringUtils.isNotEmpty(salt)) {
            md.update(Base64.decodeBase64(salt.getBytes()));
        }
        md.update(str.getBytes("UTF-8"));
        hash = new String(Base64.encodeBase64(md.digest()));
    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    return hash;
}

From source file:jatoo.properties.FileProperties.java

private static final Key generateKey(String passphrase) throws NoSuchAlgorithmException {

    MessageDigest digest = MessageDigest.getInstance("SHA");
    digest.update(passphrase.getBytes());

    return new SecretKeySpec(digest.digest(), 0, 16, "AES");
}

From source file:com.doculibre.constellio.solr.handler.component.SearchLogComponent.java

private static String digest(byte[] content) {
    String digestString;//  www .j a va 2  s.c o m
    try {
        MessageDigest shaDigester = MessageDigest.getInstance("SHA");
        shaDigester.update(content);
        byte[] shaDigest = shaDigester.digest();
        digestString = new String(Base64.encodeBase64(shaDigest));
        digestString = escape(digestString);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    return digestString;
}

From source file:Main.java

public static String generateShortUuid() {
    UUID uuid = UUID.randomUUID();
    MessageDigest md = null;
    try {/*  www.  j ava2  s  .c o m*/
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return uuid.toString();
    }

    md.update(uuid.toString().getBytes());
    byte[] digest = md.digest();

    return Base64.encodeToString(digest, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING).substring(0, 20);
}

From source file:web.org.perfmon4j.console.app.spring.security.Perfmon4jUserConsoleLoginService.java

public static String generateMD5Hash(String str) {
    try {/*from  ww  w. j a v  a  2  s.  co  m*/
        MessageDigest d = MessageDigest.getInstance("MD5");
        d.update(str.getBytes("UTF-8"));
        return Hex.encodeHexString(d.digest());
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("UTF-8 not supported");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Unable to create MD5 hash", e);
    }
}

From source file:com.corngo.base.support.utils.security.Digests.java

/**
 * , ?md5sha1.//from w w w .  j  a v a2  s  .co m
 */
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
    try {
        MessageDigest digest = MessageDigest.getInstance(algorithm);

        if (salt != null) {
            digest.update(salt);
        }

        byte[] result = digest.digest(input);

        for (int i = 1; i < iterations; i++) {
            digest.reset();
            result = digest.digest(result);
        }
        return result;
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
        return null;
    }
}