Java Code Examples for java.security.MessageDigest.digest()

The following are Jave code examples for showing how to use digest() of the java.security.MessageDigest class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: WeatherSystem   File: MD5Util.java   View Source Code Vote up 6 votes
/**
    * 生成md5
    * 
    * @param message
    * @return
    */
   public static String getMD5(String message) {
String md5str = "";
try {
    // 1 创建一个提供信息摘要算法的对象,初始化为md5算法对象
    MessageDigest md = MessageDigest.getInstance("MD5");

    // 2 将消息变成byte数组
    byte[] input = message.getBytes();

    // 3 计算后获得字节数组,这就是那128位了
    byte[] buff = md.digest(input);

    // 4 把数组每一字节(一个字节占八位)换成16进制连成md5字符串
    md5str = bytesToHex(buff);

} catch (Exception e) {
    e.printStackTrace();
}
return md5str;
   }
 
Example 2
Project: jdk8u-jdk   File: CheckCertId.java   View Source Code Vote up 6 votes
public static void main(String[] args) throws Exception {

        X509CertImpl cert = loadCert(CERT_FILENAME);

        /* Compute the hash in the same way as CertId constructor */
        MessageDigest hash = MessageDigest.getInstance("SHA1");
        hash.update(cert.getSubjectX500Principal().getEncoded());
        byte[] expectedHash = hash.digest();

        CertId certId = new CertId(cert, null);
        byte[] receivedHash = certId.getIssuerNameHash();

        if (! Arrays.equals(expectedHash, receivedHash)) {
            throw new
                Exception("Bad hash value for issuer name in CertId object");
        }
    }
 
Example 3
Project: NovelReader   File: MD5Utils.java   View Source Code Vote up 6 votes
public static String strToMd5By32(String str){
    String reStr = null;
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] bytes = md5.digest(str.getBytes());
        StringBuffer stringBuffer = new StringBuffer();
        for (byte b : bytes){
            int bt = b&0xff;
            if (bt < 16){
                stringBuffer.append(0);
            }
            stringBuffer.append(Integer.toHexString(bt));
        }
        reStr = stringBuffer.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return reStr;
}
 
Example 4
Project: pay   File: Sha1Util.java   View Source Code Vote up 6 votes
public static String getSha1(String str) {
	if (str == null || str.length() == 0) {
		return null;
	}
	char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
			'a', 'b', 'c', 'd', 'e', 'f' };

	try {
		MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
		mdTemp.update(str.getBytes("GBK"));

		byte[] md = mdTemp.digest();
		int j = md.length;
		char buf[] = new char[j * 2];
		int k = 0;
		for (int i = 0; i < j; i++) {
			byte byte0 = md[i];
			buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
			buf[k++] = hexDigits[byte0 & 0xf];
		}
		return new String(buf);
	} catch (Exception e) {
		return null;
	}
}
 
Example 5
Project: CreeperHostGui   File: Callbacks.java   View Source Code Vote up 6 votes
public static String getPlayerHash(UUID uuid)
{
    if (hashCache.containsKey(uuid))
        return hashCache.get(uuid);

    String playerHash;
    try
    {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hash = digest.digest(uuid.toString().getBytes(Charset.forName("UTF-8")));
        playerHash = (new HexBinaryAdapter()).marshal(hash);
    }
    catch (NoSuchAlgorithmException e)
    {
        e.printStackTrace();
        return null;
    }
    hashCache.put(uuid, playerHash);
    return playerHash;
}
 
Example 6
Project: Websocket-Smart-Card-Signer   File: SignUtils.java   View Source Code Vote up 6 votes
public static byte[] calculateHASH(String digestOID, byte[] data) throws Exception{
    String digestName = "";
    
    try{
        if(Security.getProvider("BC") == null)
            Security.addProvider(new BouncyCastleProvider());
        
        if(digestOID.equals(CMSSignedDataGenerator.DIGEST_MD5))
            digestName = "MD5";
        if(digestOID.equals(CMSSignedDataGenerator.DIGEST_SHA1))
            digestName = "SHA-1";
        if(digestOID.equals(CMSSignedDataGenerator.DIGEST_SHA256))
            digestName = "SHA-256";
        if(digestOID.equals(CMSSignedDataGenerator.DIGEST_SHA384))
            digestName = "SHA-384";
        if(digestOID.equals(CMSSignedDataGenerator.DIGEST_SHA512))
            digestName = "SHA-512";
        
        if(digestName.equals(""))
            throw new Exception("Unsupported digestOID");
        
        MessageDigest md = MessageDigest.getInstance(digestName, "BC");
        md.update(data);
        
        byte[] hash = md.digest();

        return hash;
    }catch(Exception e){
        throw new Exception("Error on the generation for the Hash "+digestName+":\n"+e.getMessage());
    }
}
 
Example 7
Project: bumblebee   File: Md5Util.java   View Source Code Vote up 6 votes
/**
 * 将明文参数进行MD5加密
 *
 * @param str
 *            明文
 * @return str 密文
 */
public static String MD5(String str) {
    if (StringUtils.isEmpty(str) || StringUtils.isBlank(str)) {
        return null;
    }

    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F' };
    try {
        byte[] btInput = str.getBytes();
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        mdInst.update(btInput);
        byte[] md = mdInst.digest();
        int j = md.length;
        char strArr[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            strArr[k++] = hexDigits[byte0 >>> 4 & 0xf];
            strArr[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(strArr);
    } catch (Exception e) {
        return null;
    }
}
 
Example 8
Project: incubator-netbeans   File: PathRegistry.java   View Source Code Vote up 6 votes
@NonNull
private static byte[] toDigest(
    @NonNull final ClassPath cp,
    @NonNull final MessageDigest md) {
    final StringBuilder sb = new StringBuilder();
    for (ClassPath.Flag flag : cp.getFlags()) {
        sb.append(flag.name()).
            append(File.pathSeparatorChar);
    }
    for (ClassPath.Entry e : cp.entries()) {
        sb.append(e.getURL().toExternalForm()).
            append(File.pathSeparatorChar);
    }
    try {
        return md.digest(sb.toString().getBytes());
    } finally {
        md.reset();
    }
}
 
Example 9
Project: PasswordNotebook   File: EncryptUtil.java   View Source Code Vote up 6 votes
public static String getMD5Value(byte[] inputBytes) {
    MessageDigest messageDigest;
    try {
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(inputBytes);
        byte[] digest = messageDigest.digest();
        BigInteger bigInt = new BigInteger(1, digest);
        String hashtext = bigInt.toString(16);
        // Now we need to zero pad it if you actually want the full 32 chars.
        while (hashtext.length() < 32) {
            hashtext = "0" + hashtext;
        }
        return hashtext;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 10
Project: openjdk-jdk10   File: TestSameValue.java   View Source Code Vote up 5 votes
@Override
public byte[] updateDigest(byte[] data, MessageDigest md) {
    byte[] mainPart = new byte[data.length - LASTNBYTES];
    for (int i = 0; i < mainPart.length; i++) {
        mainPart[i] = data[i];
    }
    for (int j = 0; j < LASTNBYTES; j++) {
        REMAIN[j] = data[data.length - LASTNBYTES + j];
    }
    md.update(ByteBuffer.wrap(mainPart));
    return md.digest(REMAIN);
}
 
Example 11
Project: CleanBT   File: Utils.java   View Source Code Vote up 5 votes
static byte[] SHA_1(byte[] input) {
    try {
        MessageDigest sha_1 = MessageDigest.getInstance("SHA-1");
        sha_1.update(input);
        return sha_1.digest();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 12
Project: jdk8u-jdk   File: PKCS12KeyStore.java   View Source Code Vote up 5 votes
private byte[] generateHash(byte[] data) throws IOException
{
    byte[] digest = null;

    try {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(data);
        digest = md.digest();
    } catch (Exception e) {
        throw new IOException("generateHash failed: " + e, e);
    }
    return digest;
}
 
Example 13
Project: lastpass-java   File: SimpleSha256Managed.java   View Source Code Vote up 5 votes
@Override
public byte[] computeHash(byte[] bytes) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("SHA-256 not found", e);
    }
    md.update(bytes);
    byte[] digest = md.digest();
    return digest;
}
 
Example 14
Project: springreplugin   File: SecurityUtil.java   View Source Code Vote up 5 votes
/** 计算给定 byte [] 串的 MD5 */
public static byte[] MD5(byte[] input) {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    if (md != null) {
        md.update(input);
        return md.digest();
    } else {
        return null;
    }
}
 
Example 15
Project: yadaframework   File: YadaUtil.java   View Source Code Vote up 5 votes
/**
 * Create a MD5 hash of a string (from http://snippets.dzone.com/posts/show/3686)
 * @param clear the source text
 * @return
 * @throws NoSuchAlgorithmException
 */
public String md5Hash(String clear) throws NoSuchAlgorithmException {
	MessageDigest m = MessageDigest.getInstance("MD5");
	byte[] data = clear.getBytes(); 
	m.update(data,0,data.length);
	BigInteger i = new BigInteger(1,m.digest());
	return String.format("%1$032X", i);
}
 
Example 16
Project: ovh_mail_redirections_manager_for_android   File: OvhApi.java   View Source Code Vote up 5 votes
public static String HashSHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest md;
    md = MessageDigest.getInstance("SHA-1");
    byte[] sha1hash = new byte[40];
    md.update(text.getBytes("iso-8859-1"), 0, text.length());
    sha1hash = md.digest();
    StringBuffer sb = new StringBuffer();
    for (byte aSha1hash : sha1hash) {
        sb.append(Integer.toString((aSha1hash & 0xff) + 0x100, 16).substring(1));
    }
    return sb.toString();
}
 
Example 17
Project: jdk8u-jdk   File: InitialToken.java   View Source Code Vote up 4 votes
private byte[] computeChannelBinding(ChannelBinding channelBinding)
    throws GSSException {

    InetAddress initiatorAddress = channelBinding.getInitiatorAddress();
    InetAddress acceptorAddress = channelBinding.getAcceptorAddress();
    int size = 5*4;

    int initiatorAddressType = getAddrType(initiatorAddress);
    int acceptorAddressType = getAddrType(acceptorAddress);

    byte[] initiatorAddressBytes = null;
    if (initiatorAddress != null) {
        initiatorAddressBytes = getAddrBytes(initiatorAddress);
        size += initiatorAddressBytes.length;
    }

    byte[] acceptorAddressBytes = null;
    if (acceptorAddress != null) {
        acceptorAddressBytes = getAddrBytes(acceptorAddress);
        size += acceptorAddressBytes.length;
    }

    byte[] appDataBytes = channelBinding.getApplicationData();
    if (appDataBytes != null) {
        size += appDataBytes.length;
    }

    byte[] data = new byte[size];

    int pos = 0;

    writeLittleEndian(initiatorAddressType, data, pos);
    pos += 4;

    if (initiatorAddressBytes != null) {
        writeLittleEndian(initiatorAddressBytes.length, data, pos);
        pos += 4;
        System.arraycopy(initiatorAddressBytes, 0,
                         data, pos, initiatorAddressBytes.length);
        pos += initiatorAddressBytes.length;
    } else {
        // Write length 0
        pos += 4;
    }

    writeLittleEndian(acceptorAddressType, data, pos);
    pos += 4;

    if (acceptorAddressBytes != null) {
        writeLittleEndian(acceptorAddressBytes.length, data, pos);
        pos += 4;
        System.arraycopy(acceptorAddressBytes, 0,
                         data, pos, acceptorAddressBytes.length);
        pos += acceptorAddressBytes.length;
    } else {
        // Write length 0
        pos += 4;
    }

    if (appDataBytes != null) {
        writeLittleEndian(appDataBytes.length, data, pos);
        pos += 4;
        System.arraycopy(appDataBytes, 0, data, pos,
                         appDataBytes.length);
        pos += appDataBytes.length;
    } else {
        // Write 0
        pos += 4;
    }

    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        return md5.digest(data);
    } catch (NoSuchAlgorithmException e) {
            throw new GSSException(GSSException.FAILURE, -1,
                                   "Could not get MD5 Message Digest - "
                                   + e.getMessage());
    }
}
 
Example 18
Project: openjdk-jdk10   File: TestSameValue.java   View Source Code Vote up 4 votes
@Override
public byte[] updateDigest(byte[] data, MessageDigest md) {
    md.update(ByteBuffer.wrap(data));
    return md.digest();
}
 
Example 19
Project: jdk8u-jdk   File: TestSameValue.java   View Source Code Vote up 4 votes
@Override
public byte[] updateDigest(byte[] data, MessageDigest md) {
    md.update(ByteBuffer.wrap(data));
    return md.digest();
}
 
Example 20
Project: nat-network-stream   File: Util.java   View Source Code Vote up 4 votes
public static String getMD5(String val) throws NoSuchAlgorithmException {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    md5.update(val.getBytes());
    byte[] m = md5.digest();//加密
    return getString(m);
}