sha-256算法进行加密java

利用Apache的工具类实现加密,使用commons-codec包中的DigestUtils算法工具类(入参支持字符串、字节数组、文件流等)

依赖包

<dependency>
 <groupId>commons-codec</groupId>
 <artifactId>commons-codec</artifactId>
 <version>${common-codec.version}</version>
</dependency>

代码

import java.security.MessageDigest;
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
import org.apache.commons.codec.digest.DigestUtils;
 
public abstract class SHACoder {
 
    /**
     * SHA加密
     * 
     * @param data 待加密数据
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static byte[] encodeSHA(String data) throws Exception {
 
        // 执行消息摘要
        return DigestUtils.sha(data);
    }
 
    /**
     * SHAHex加密
     * 
     * @param data 待加密数据
     * @return String 消息摘要
     * @throws Exception
     */
    public static String encodeSHAHex(String data) throws Exception {
 
        // 执行消息摘要
        return DigestUtils.shaHex(data);
    }
 
    /**
     * SHA-224加密
     * 
     * @param data
     *            待加密数据
     * @return byte[] 消息摘要
     * 
     * @throws Exception
     */
    public static byte[] encodeSHA224(byte[] data) throws Exception {
        // 加入BouncyCastleProvider支持
        Security.addProvider(new BouncyCastleProvider());
 
        // 初始化MessageDigest
        MessageDigest md = MessageDigest.getInstance("SHA-224");
 
        // 执行消息摘要
        return md.digest(data);
    }
 
    /**
     * SHA-224加密
     * 
     * @param data
     *            待加密数据
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static String encodeSHA224Hex(byte[] data) throws Exception {
 
        // 执行消息摘要
        byte[] b = encodeSHA224(data);
 
        // 做十六进制编码处理
        return new String(Hex.encode(b));
 
    }
 
    /**
     * SHA256加密
     * 
     * @param data 待加密数据
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static byte[] encodeSHA256(String data) throws Exception {
 
        // 执行消息摘要
        return DigestUtils.sha256(data);
    }
 
    /**
     * SHA256Hex加密
     * 
     * @param data 待加密数据
     * @return String 消息摘要
     * @throws Exception
     */
    public static String encodeSHA256Hex(String data) throws Exception {
 
        // 执行消息摘要
        return DigestUtils.sha256Hex(data);
    }
 
    /**
     * SHA384加密
     * 
     * @param data 待加密数据
     * @return byte[] 消息摘要 
     * @throws Exception
     */
    public static byte[] encodeSHA384(String data) throws Exception {
 
        // 执行消息摘要
        return DigestUtils.sha384(data);
    }
 
    /**
     * SHA384Hex加密
     * 
     * @param data 待加密数据
     * @return String 消息摘要 
     * @throws Exception
     */
    public static String encodeSHA384Hex(String data) throws Exception {
 
        // 执行消息摘要
        return DigestUtils.sha384Hex(data);
    }
 
    /**
     * SHA512Hex加密
     * 
     * @param data 待加密数据
     * @return byte[] 消息摘要
     * @throws Exception
     */
    public static byte[] encodeSHA512(String data) throws Exception {
 
        // 执行消息摘要
        return DigestUtils.sha512(data);
    }
 
    /**
     * SHA512Hex加密
     * 
     * @param data 待加密数据
     * @return String 消息摘要
     * @throws Exception
     */
    public static String encodeSHA512Hex(String data) throws Exception {
 
        // 执行消息摘要
        return DigestUtils.sha512Hex(data);
    }
 
}

转自:https://blog.csdn.net/dh753655485/article/details/106947206/

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容