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 void update(byte[] input, int offset, int len) 

Source Link

Document

Updates the digest using the specified array of bytes, starting at the specified offset.

Usage

From source file:customerproject.customerutilities.Utilities.java

public static String SHA256(String email, String password)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {

    String text = email + "." + password;
    MessageDigest md = DigestUtils.getSha256Digest();

    byte[] sha256hash = new byte[32];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha256hash = md.digest();//from w  ww  .j  a  v  a  2  s.c  o m

    //return Base64.encodeBase64String(sha256hash);
    return Base64.encodeBase64URLSafeString(sha256hash);
    //String hex = convertToHex(sha256hash);

}

From source file:Main.java

public static String sha1(String text) {
    MessageDigest md;
    try {// w  w  w .jav a  2  s  .  co  m
        md = MessageDigest.getInstance("SHA-1");
        byte[] sha1hash = new byte[40];
        md.update(text.getBytes("UTF-8"), 0, text.length());
        sha1hash = md.digest();
        return convertToHex(sha1hash);
    } catch (Exception e) {
    }

    return "";
}

From source file:org.confab.Utilities.java

public static String md5(String pass) {
    String md5 = "";
    try {/*from w  w w.  j a  va  2s  . c  o 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:it.zero11.acme.utils.JWKUtils.java

private static byte[] SHA256(String text) {
    try {//  w w  w .j  av a 2s .  c  o  m
        MessageDigest md;
        md = MessageDigest.getInstance("SHA-256");
        md.update(text.getBytes("UTF-8"), 0, text.length());
        return md.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Return a md5 string based on a given string 
 *//*from w w  w  .j  a v a  2s .  c  om*/
public static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md;
    md = MessageDigest.getInstance("MD5");
    byte[] md5hash = new byte[32];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    md5hash = md.digest();
    return convertToHex(md5hash);
}

From source file:customerproject.customerutilities.Utilities.java

public static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {

    MessageDigest md;
    md = MessageDigest.getInstance("MD5");
    byte[] md5hash = new byte[32];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    md5hash = md.digest();//from w  w  w  .  j  a va 2  s  .c o  m
    return convertToHex(md5hash);
}

From source file:br.com.gerenciapessoal.service.CadastroUsuarioService.java

@SuppressWarnings("null")
public static String md5(String input) {
    String md5 = null;//from w  w w  .  j  av  a2s .c om
    if (!StringUtils.isNotBlank(input)) {
        return null;
    }
    try {
        //Create MessageDigest object for MD5           
        MessageDigest digest = MessageDigest.getInstance("MD5");
        //Update input string in message digest           
        digest.update(input.getBytes(), 0, input.length());
        //Converts message digest value in base 16 (hex)            
        md5 = new BigInteger(1, digest.digest()).toString(16);
    } catch (NoSuchAlgorithmException e) {
    }
    return md5.trim();
}

From source file:Main.java

/**
 * Generates SHA256 hash of the password which is used as key
 *
 * @param password used to generated key
 * @return SHA256 of the password//w w  w.  ja  va 2s.  c  o  m
 */
private static SecretKeySpec generateKey(final String password)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    final MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM);
    byte[] bytes = password.getBytes("UTF-8");
    digest.update(bytes, 0, bytes.length);
    byte[] key = digest.digest();

    log("SHA-256 key ", key);

    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
    return secretKeySpec;
}

From source file:Main.java

public static byte[] getDigestFromURL(URL u) throws Exception {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    InputStream in = u.openStream();
    byte[] data = new byte[1024];
    int bytesRead = -1;
    while ((bytesRead = in.read(data)) >= 0) {
        md5.update(data, 0, bytesRead);
    }/* ww w .  ja  v  a 2  s.c  o m*/
    return md5.digest();
}

From source file:io.sidecar.security.SecurityUtils.java

/**
 * Generates a String representation of an MD5 Checksum from a given String input.
 *
 * @param input - A non-null String./*from www .jav  a2 s .  co m*/
 * @return an MD5 checksum as a Hex encoded String.
 */
public static String md5(String input) {
    checkNotNull(input);
    try {
        //Create MessageDigest object for MD5
        MessageDigest digest = MessageDigest.getInstance(MD5_ALGORITHM);

        //Update input string in message digest
        digest.update(input.getBytes(), 0, input.length());

        //Converts message digest value in base 16 (hex)
        return String.copyValueOf(Hex.encodeHex(digest.digest()));
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    }
}