Here you can find the source of computeHashSHA1(final String text)
Parameter | Description |
---|---|
text | a string to compute a hash value for |
Parameter | Description |
---|---|
NoSuchAlgorithmException | if hash algorithm is not available |
public static String computeHashSHA1(final String text) throws NoSuchAlgorithmException
//package com.java2s; //License from project: Apache License import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { /**//from w w w . j a v a2s. c o m * Returns a hash value for a string using SHA-1 algorithm * * @param text a string to compute a hash value for * @return a uppercase hash value for the given text * @throws NoSuchAlgorithmException if hash algorithm is not available */ public static String computeHashSHA1(final String text) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("SHA-1"); md.update(text.getBytes(), 0, text.length()); // get sha-1 bytes byte hashData[] = md.digest(); // create a hex string StringBuilder sb = new StringBuilder(hashData.length * 2); for (int i = 0; i < hashData.length; i++) { int b = (0xFF & hashData[i]); // if it is a single digit, make sure it have 0 in front (proper padding) if (b <= 0xF) sb.append('0'); // add number to string sb.append(Integer.toHexString(b)); } // hex string to uppercase return sb.toString().toUpperCase(); } }