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.sdk.util.PortraitUtil.java

protected static void appendToken(StringBuilder sb, String uuid) throws Exception {

    if (Validator.isNull(uuid)) {
        return;/* w ww . j a  v a2 s .  c o  m*/
    }

    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    digest.update(uuid.getBytes());
    byte[] bytes = digest.digest();
    byte[] encodedBytes = Base64.encodeBase64(bytes);
    String token = new String(encodedBytes);

    sb.append("&img_id_token=");
    sb.append(URLEncoder.encode(token, "UTF8"));
}

From source file:Main.java

static long md5sum(String text) {

    byte[] defaultBytes = text.getBytes();

    try {//from   www  .jav a2 s .c  o  m

        MessageDigest algorithm = MessageDigest.getInstance("MD5");

        algorithm.reset();

        algorithm.update(defaultBytes);

        byte digest[] = algorithm.digest();

        ByteBuffer buffer = ByteBuffer.wrap(digest);

        return buffer.getLong();

    } catch (NoSuchAlgorithmException e) {

        Log.e("udt", "md5 failed: " + e);

        return 0;

    }

}

From source file:ca.ualberta.physics.cssdp.util.HashUtils.java

/**
 * From a password, a number of iterations and a salt, returns the
 * corresponding digest//from  ww w.jav  a2s  .  co m
 * 
 * @param iterationNb
 *            int The number of iterations of the algorithm
 * @param password
 *            String The password to encrypt
 * @param salt
 *            byte[] The salt
 * @return byte[] The digested password
 * @throws NoSuchAlgorithmException
 *             If the algorithm doesn't exist
 */
public static byte[] getHash(int iterationNb, String password, byte[] salt) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        digest.reset();
        digest.update(salt);
        byte[] input = digest.digest(password.getBytes("UTF-8"));
        for (int i = 0; i < iterationNb; i++) {
            digest.reset();
            input = digest.digest(input);
        }
        return input;
    } catch (NoSuchAlgorithmException nsa) {
        throw Throwables.propagate(nsa);
    } catch (UnsupportedEncodingException e) {
        throw Throwables.propagate(e);
    }
}

From source file:cc.recommenders.utils.Fingerprints.java

public static String sha1(final String message) {
    ensureIsNotNull(message);//  w w w. ja v  a  2s .  co  m
    //
    try {
        final MessageDigest digest = createMessageDigest();
        digest.update(message.getBytes());
        return toString(digest);
    } catch (final Exception e) {
        throw Throws.throwUnhandledException(e);
    }
}

From source file:Main.java

public static String getMD5(String str) {
    MessageDigest messageDigest = null;
    try {/*from w  w  w .  j  ava2  s  .c  o  m*/
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
    } catch (UnsupportedEncodingException e) {
    }
    byte[] byteArray = messageDigest.digest();
    StringBuffer md5StrBuff = new StringBuffer();
    for (int i = 0; i < byteArray.length; i++) {
        if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
            md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
        } else {
            md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
        }
    }
    return md5StrBuff.substring(8, 24).toString();
}

From source file:de.micromata.genome.gwiki.auth.PasswordUtils.java

/**
 * Standart asymetrischen password verschlsseln mit SHA-256 fr Genome
 * //from ww  w . j  a  v  a  2  s  .c  o  m
 * @param plaintext Nie <code>null</code>
 * @return password hash
 */

public static String unsaltedSecureHash(String plaintext) {
    Validate.notNull(plaintext, "plaintext not set");
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(plaintext.getBytes(CharEncoding.UTF_8));
        byte raw[] = md.digest();
        String hash = Converter.encodeBase64(raw);
        return hash;
    } catch (Exception ex) {
        /**
         * @logging
         * @reason Beim asymetrischen verschlsseln ist ein Fehler aufgetreten.
         * @action berprfen der Java-Installation
         */
        throw new LoggedRuntimeException(ex, LogLevel.Fatal, GenomeLogCategory.System,
                "Error while executing hashing encryption: " + ex.getMessage());
    }
}

From source file:com.github.robozonky.integrations.zonkoid.ZonkoidConfirmationProvider.java

static String md5(final String secret) throws NoSuchAlgorithmException {
    final MessageDigest mdEnc = MessageDigest.getInstance("MD5");
    mdEnc.update(secret.getBytes(Defaults.CHARSET));
    return new BigInteger(1, mdEnc.digest()).toString(16);
}

From source file:Componentes.EncryptionMD5.java

public static String getStringMessageDigest(String message, String algorithm) {
    byte[] digest = null;
    byte[] buffer = message.getBytes();
    try {/*from   w  w w.ja  v  a2  s.  c  om*/
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
        messageDigest.reset();
        messageDigest.update(buffer);
        digest = messageDigest.digest();
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Error creando Digest");
    }
    return toHexadecimal(digest);
}

From source file:com.agiletec.plugins.jpuserreg.aps.system.services.userreg.util.ShaEncoder.java

public static String encodePassword(String password) throws NoSuchAlgorithmException {

    if (password != null) {

        MessageDigest digest = MessageDigest.getInstance("SHA");

        digest.update(password.getBytes());

        byte bytes[] = digest.digest();

        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < bytes.length; i++) {

            int b = bytes[i] & 0xff;

            if (b < 16) {

                buffer.append("0");

            }/*from  w  ww.j  a v a2 s  . c  om*/

            buffer.append(Integer.toHexString(b));

        }

        password = buffer.toString();

    }

    return password;

}

From source file:Main.java

public static String getMD5Str(String str) {
    MessageDigest messageDigest = null;
    try {/*w w  w . ja va  2s . c  om*/
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    byte[] byteArray = messageDigest.digest();
    StringBuffer md5StrBuff = new StringBuffer();
    for (int i = 0; i < byteArray.length; i++) {
        if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
            md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
        } else {
            md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
        }
    }
    return md5StrBuff.toString();
}