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.redhat.rhn.common.util.SHA256Crypt.java

/**
 * SHA256 and Hexify an array of bytes.  Take the input array, SHA256 encodes it
 * and then turns it into Hex./*  w  w  w . j  av a  2  s  . c  o m*/
 * @param secretBytes you want sha256hexed
 * @return sha256hexed String.
 */
public static String sha256Hex(byte[] secretBytes) {
    String retval = null;
    // add secret
    MessageDigest md;
    md = getSHA256MD();
    md.update(secretBytes);
    // generate the digest
    byte[] digest = md.digest();
    // hexify this puppy
    retval = new String(Hex.encodeHex(digest));
    return retval;
}

From source file:net.bioclipse.dbxp.business.DbxpManager.java

public static String getMD5Sum(String variable) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(variable.getBytes());
    return new BigInteger(1, md.digest()).toString(16);
}

From source file:com.likya.tlos.utils.PasswordService.java

public static synchronized String encrypt(String plaintext) throws Exception {
    MessageDigest md = null;
    try {/*  w ww . ja  va 2  s .  c  o m*/
        md = MessageDigest.getInstance("SHA"); // step 2 //$NON-NLS-1$
    } catch (NoSuchAlgorithmException e) {
        throw new Exception(e.getMessage());
    }
    try {
        md.update(plaintext.getBytes("UTF-8")); // step 3 //$NON-NLS-1$
    } catch (UnsupportedEncodingException e) {
        throw new Exception(e.getMessage());
    }
    byte raw[] = md.digest(); // step 4

    /**
     * Sun uygulamasndan vazgeilip apache uygulamas kullanld
     * 16.03.2011 
     * 
     */
    // String hash = (new BASE64Encoder()).encode(raw); // step 5

    String hash = Base64.encodeBase64String(raw);

    return hash; // step 6
}

From source file:Main.java

static public String md5(String str) {
    MessageDigest algorithm = null;
    try {//from ww  w  . j  av a  2 s  .com
        algorithm = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    if (algorithm != null) {
        algorithm.reset();
        algorithm.update(str.getBytes());
        byte[] bytes = algorithm.digest();
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            if (Integer.toHexString(0xFF & b).length() == 1)
                hexString.append("0").append(Integer.toHexString(0xFF & b));
            else
                hexString.append(Integer.toHexString(0xFF & b));
        }
        return hexString.toString();
    }
    return "";

}

From source file:de.itsvs.cwtrpc.controller.token.DefaultXsrfTokenService.java

protected static String getMd5HexString(byte[] bytes) {
    final MessageDigest md;
    final StringBuilder value;

    md = md5ThreadLocal.get();/*from  w ww.ja  va  2s.c o m*/
    md.reset();
    md.update(bytes);

    value = new StringBuilder();
    for (byte b : md.digest()) {
        final int val = b & 0xff;

        if (val < 16) {
            value.append('0');
        }
        value.append(Integer.toHexString(val));
    }

    return value.toString();
}

From source file:kr.co.sangcomz.facebooklogin.HelloFacebookSampleActivity.java

public static void showHashKey(Context context) {
    try {/*  w  w  w  . ja  v a2  s  . c  o m*/
        PackageInfo info = context.getPackageManager().getPackageInfo("kr.co.sangcomz.facebooklogin",
                PackageManager.GET_SIGNATURES); //Your            package name here
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {
    } catch (NoSuchAlgorithmException e) {
    }
}

From source file:Main.java

public static String SHA256Encrypt(String orignal) {
    MessageDigest md = null;
    try {/*from w  w  w  . j  av a  2  s. c o m*/
        md = MessageDigest.getInstance(ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    if (null != md) {
        byte[] origBytes = orignal.getBytes();
        md.update(origBytes);
        byte[] digestRes = md.digest();
        String digestStr = getDigestStr(digestRes);
        return digestStr;
    }

    return null;
}

From source file:UUIDGenerator.java

protected static synchronized void getInitialUUID() {
    if (baseUUID != null) {
        return;/* w  w w.j a  va 2 s . c o m*/
    }
    if (myRand == null) {
        myRand = new Random();
    }
    long rand = myRand.nextLong();
    String sid;
    try {
        sid = InetAddress.getLocalHost().toString();
    } catch (UnknownHostException e) {
        sid = Thread.currentThread().getName();
    }
    StringBuffer sb = new StringBuffer();
    sb.append(sid);
    sb.append(":");
    sb.append(Long.toString(rand));
    MessageDigest md5 = null;
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        //todo have to be properly handled
    }
    md5.update(sb.toString().getBytes());
    byte[] array = md5.digest();
    StringBuffer sb2 = new StringBuffer();
    for (int j = 0; j < array.length; ++j) {
        int b = array[j] & 0xFF;
        sb2.append(Integer.toHexString(b));
    }
    int begin = myRand.nextInt();
    if (begin < 0)
        begin = begin * -1;
    begin = begin % 8;
    baseUUID = sb2.toString().substring(begin, begin + 18).toUpperCase();
}

From source file:com.pfarrell.crypto.HmacUtil.java

/**
 * calculate an HMAC using the SHA256 algorithm.
 * Given a secret and message, returns a sha256 hash of the message.
 * (no longer uses SHA1)/* w w  w . j a  v a  2 s .  co m*/
 * @param secret string known to both parties
 * @param message to sign
 * @return hexified result
 */
public static String hmac(String secret, byte[] message) {
    Preconditions.checkNotNull(secret);
    Preconditions.checkNotNull(message);
    byte[] gas = null;
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");

        byte[] sbytes = secret.getBytes();
        digest.update(sbytes);
        digest.update(message);
        digest.update(sbytes);
        gas = digest.digest();

    } catch (Exception e) {
        System.out.println("WebUtils.hamac - caught exception: " + e.toString());
    }
    return hexify(gas);
}

From source file:com.mh.commons.utils.Encodes.java

/**
 * //  www.ja v a2s  .  c  o m
 * @param srcStr
 * @param alg
 * @return
 */
private static String getDigest(String srcStr, String alg) {
    Assert.notNull(srcStr);
    Assert.notNull(alg);
    try {
        MessageDigest alga = MessageDigest.getInstance(alg);
        alga.update(srcStr.getBytes());
        byte[] digesta = alga.digest();
        return byte2hex(digesta);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}