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(byte[] input) 

Source Link

Document

Performs a final update on the digest using the specified array of bytes, then completes the digest computation.

Usage

From source file:Main.java

public static byte[] simpleHash256(byte[] msg) throws NoSuchAlgorithmException {
    MessageDigest sha256 = MessageDigest.getInstance(SHA_256);
    byte[] byteHolder1, byteHolder2;
    byteHolder1 = sha256.digest(msg);
    for (int i = 0; i < 100; i++) {
        byteHolder2 = sha256.digest(byteHolder1);
        byteHolder1 = sha256.digest(byteHolder2);
    }//w w w . j  ava  2 s .c  o m
    return byteHolder1;
}

From source file:Main.java

/**
 * SHA256 implementation/* w ww.  j  a va 2  s .c o  m*/
 * @param toHash the cleartext string
 * @return a SHA256 hashed string
 */
public static String sha256(final String toHash) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(toHash.getBytes());
        StringBuilder hexString = new StringBuilder();

        for (byte aHash : hash) {
            String hex = Integer.toHexString(0xff & aHash);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }

        return hexString.toString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static String getMd5Hash(String buffer) {
    MessageDigest md;
    try {//from  w  w w  . j ava  2  s.c  o  m
        md = MessageDigest.getInstance("MD5");
        return bytesToHex(md.digest(buffer.getBytes("UTF-8")));
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException ignore) {
    }
    return "";
}

From source file:Main.java

public static String MD5(String md5) {
    if (md5 == null) {
        return null;
    }//from w ww.ja  va  2 s  .  c om
    try {
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte anArray : array) {
            sb.append(Integer.toHexString((anArray & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
        Log.e("tmessages", e.getMessage());
    }
    return null;
}

From source file:Main.java

/** Returns a 32 character string containing a hash of {@code s}. */
public static String hash(String s) {
    try {/*from   w w  w. j  ava 2  s.  c  o m*/
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] md5bytes = messageDigest.digest(s.getBytes("UTF-8"));
        return bytesToHexString(md5bytes);
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError(e);
    }
}

From source file:Main.java

public static byte[] dataDigest(String in) {
    byte[] sum = null;

    try {//from  ww w .  j a  va2 s . c  o  m
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] bytes = in.getBytes("UTF8");
        sum = md.digest(bytes);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return sum;
}

From source file:controlpac.EncryptHelper.java

public static String Desencriptar(String textoEncriptado) {
    String base64EncryptedString = "";
    try {/*  w  w  w.  jav a 2 s . com*/
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));//Desencodea el texto en base64
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); //Crea un hash con la clave elegida
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");//Crea la clave 
        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key); //Inicia el descifrado
        byte[] plainText = decipher.doFinal(message);//Descifra el texto
        base64EncryptedString = new String(plainText, "UTF-8");
    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:fr.cph.stock.security.Security.java

/**
 * Encode to sha256 the user password//from www . j  a  v a  2s.  c  o  m
 * 
 * @param str
 *            the password to encode
 * @return an encoded string
 * @throws NoSuchAlgorithmException
 *             the NoSuchAlgorithmException
 * @throws UnsupportedEncodingException
 *             the UnsupportedEncodingException
 */
public static String encodeToSha256(final String str)
        throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    byte[] hash = digest.digest(str.getBytes("UTF-8"));
    String encoded = Hex.encodeHexString(hash);
    return encoded;
}

From source file:in.bookmylab.Utils.java

public static String hashPassword(String password) {
    String hashed = null;/*w w  w  .  j ava2  s . c  om*/
    try {
        if (!StringUtils.isBlank(password)) {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest((salt + password).getBytes(StandardCharsets.ISO_8859_1));
            //hashed = Base64.getEncoder().encodeToString(hash);
            hashed = Base64.encodeBase64String(hash);
        }
    } catch (NoSuchAlgorithmException ex) {
        log.log(Level.SEVERE, "Could not hash string.", ex);
    }
    return hashed;
}

From source file:io.ucoin.ucoinj.core.util.crypto.DigestUtils.java

public static String sha1Hex(String message, String encoding) {
    try {//  w  ww  .ja va  2 s  . c  om
        MessageDigest md = getSHA1Instance();
        return encodeHex(md.digest(message.getBytes(encoding)));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}