SHA1 对字符串求摘要的实现

{测试结果}
对字符串 Hello, World!
求SHA1,结果为:0a0a9f2a6772942557ab5355d76af442f8f65e01

{测试代码}

package sup.orange.learn;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by re-x on 11/4/14.
 */
public class HashTextTest {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        System.out.println(sha1("Hello, World!"));
    }

    static String sha1(String input) throws NoSuchAlgorithmException {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
        byte[] result = messageDigest.digest(input.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    }
}
原文地址:https://www.cnblogs.com/aqing1987/p/4212469.html