码迷,mamicode.com
首页 > 其他好文 > 详细

SHA1 对字符串求摘要的实现

时间:2015-01-09 10:26:39      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

{测试结果}
对字符串 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();
    }
}

 

SHA1 对字符串求摘要的实现

标签:

原文地址:http://www.cnblogs.com/aqing1987/p/4212469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!