计算字节数组的SHA-1 [英] Compute SHA-1 of byte array

查看:123
本文介绍了计算字节数组的SHA-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来获取带有Java字节数组的SHA-1校验和作为消息。

I'm looking for a way of getting an SHA-1 checksum with a Java byte array as the message.

我应该使用第三方工具还是有什么内置的JVM可以提供帮助吗?

Should I use a third party tool or is there something built in to the JVM that can help?

推荐答案

怎么样:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

public static String SHAsum(byte[] convertme) throws NoSuchAlgorithmException{
    MessageDigest md = MessageDigest.getInstance("SHA-1"); 
    return byteArray2Hex(md.digest(convertme));
}

private static String byteArray2Hex(final byte[] hash) {
    Formatter formatter = new Formatter();
    for (byte b : hash) {
        formatter.format("%02x", b);
    }
    return formatter.toString();
}

这篇关于计算字节数组的SHA-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆