Example usage for java.security MessageDigest getInstance

List of usage examples for java.security MessageDigest getInstance

Introduction

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

Prototype

public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a MessageDigest object that implements the specified digest algorithm.

Usage

From source file:Main.java

private static String sha256Hex(final String filePath) throws NoSuchAlgorithmException, IOException {
    final InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
    final MessageDigest md = MessageDigest.getInstance("SHA-256");

    final byte[] dataBytes = new byte[1024];

    int nread;/*w  ww  .j  a v  a 2 s  .  c  o m*/
    while ((nread = fis.read(dataBytes)) != -1)
        md.update(dataBytes, 0, nread);
    final byte[] mdbytes = md.digest();

    final StringBuilder sb = new StringBuilder();
    for (final byte b : mdbytes)
        sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));

    return sb.toString();
}

From source file:Main.java

public static String GetMd5(final String s) {
    try {/* www . jav  a 2s.  c  o m*/
        final MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(s.trim().getBytes());
        final byte[] messageDigset = digest.digest();
        return Bytes2Hex(messageDigset);
    } catch (final NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return s;
}

From source file:Main.java

/**
 * encode By MD5/*from w  w w. ja  v a  2  s .com*/
 * 
 * @param str
 * @return String
 */
public static String md5(String str) {
    if (str == null) {
        return null;
    }
    try {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(str.getBytes());
        return new String(encodeHex(messageDigest.digest()));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:MD5.java

/**
 * MD5 BASE64 checksum for the specified input string.
 * //from  w  ww . j  av a  2  s.c o m
 * @param input -
 *          Specified input string
 * @return String - MD5 BASE64 sum
 */
public static String checkMD5(String input) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(input.getBytes());
        byte[] enc = md.digest();
        String md5Sum = new sun.misc.BASE64Encoder().encode(enc);
        return md5Sum;
    } catch (NoSuchAlgorithmException nsae) {
        System.out.println(nsae.getMessage());
        return null;
    }
}

From source file:com.bstore.services.test.TestMD5.java

private static String generateToken() {
    MessageDigest md;//w  w w  .j a  va2 s .  c o  m
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("com.bstore.services.test.TestMD5.generateToken():" + e);
        throw new RuntimeException(e);
    }

    StringBuffer hexString = new StringBuffer();
    byte[] data = md.digest(RandomStringUtils.randomAlphabetic(10).getBytes());
    for (int i = 0; i < data.length; i++) {
        hexString.append(Integer.toHexString((data[i] >> 4) & 0x0F));
        hexString.append(Integer.toHexString(data[i] & 0x0F));
    }
    System.out.println("com.bstore.services.test.TestMD5.generateToken():: " + hexString.toString());
    return hexString.toString();
}

From source file:Main.java

public static String generateDeviceUUID(Context context) {
    String serial = android.os.Build.SERIAL;
    String androidID = Settings.Secure.ANDROID_ID;
    String deviceUUID = serial + androidID;

    /*/*from  w w  w .ja va  2 s .co  m*/
     * SHA-1
     */
    MessageDigest digest;
    byte[] result;
    try {
        digest = MessageDigest.getInstance("SHA-1");
        result = digest.digest(deviceUUID.getBytes("UTF-8"));
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    StringBuilder sb = new StringBuilder();
    for (byte b : result) {
        sb.append(String.format("%02X", b));
    }

    return sb.toString();
}

From source file:Main.java

public static String makeSHA1HashBase64(byte[] bytes) {
    try {/*from  w  ww. j a va  2s.  c om*/
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(bytes, 0, bytes.length);
        byte[] sha1hash = md.digest();
        return Base64.encodeToString(sha1hash, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String generateHash(String pText) throws Exception {
    String hashValue;// w  w  w.  ja  va 2  s  .co  m
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.reset();
    md.update(pText.getBytes("ASCII"));
    hashValue = encodeHex(md.digest());
    return hashValue;
}

From source file:de.kaojo.security.util.SecureHashingAlgorithmHelper.java

public static String hashSHA256(String input) throws NoSuchAlgorithmException {

    MessageDigest md = MessageDigest.getInstance(ALGORITHM);

    md.update(input.getBytes());/*  w w w  .j av a2 s.c om*/
    byte[] digest = md.digest();

    byte[] encode = Base64.encodeBase64(digest);
    return new String(encode);
}

From source file:Main.java

public static String getMD5EncryptedString(byte[] _encTarget) {
    String encTarget = "";
    try {/*from w  w w . j  a  v  a2 s. co m*/
        encTarget = new String(_encTarget, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        System.out.println("error converting byte[] to string");
    }
    MessageDigest mdEnc = null;
    try {
        mdEnc = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception while encrypting to md5");
    } // Encryption algorithm
    mdEnc.update(encTarget.getBytes(), 0, encTarget.length());
    String md5 = new BigInteger(1, mdEnc.digest()).toString(16);
    while (md5.length() < 32) {
        md5 = "0" + md5;
    }
    return md5;
}