All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.founder.sdk.utils.SHACoder Maven / Gradle / Ivy

There is a newer version: 3.6.1.9
Show newest version
package com.founder.sdk.utils;

import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import java.security.MessageDigest;

public class SHACoder {
    /**
     * SHA-1消息摘要算法
     */
    public static String encodeSHA(byte[] data) throws Exception {
        // 初始化MessageDigest,SHA即SHA-1的簡稱
        MessageDigest md = MessageDigest.getInstance("SHA");
        // 執行摘要方法
        byte[] digest = md.digest(data);
        return new HexBinaryAdapter().marshal(digest);
    }

    /**
     * SHA-256消息摘要算法
     */
    public static String encodeSHA256(byte[] data) throws Exception {
        // 初始化MessageDigest,SHA即SHA-1的簡稱
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        // 執行摘要方法
        byte[] digest = md.digest(data);
        return new HexBinaryAdapter().marshal(digest);
    }

    /**
     * SHA-384消息摘要算法
     */
    public static String encodeSHA384(byte[] data) throws Exception {
        // 初始化MessageDigest,SHA即SHA-1的簡稱
        MessageDigest md = MessageDigest.getInstance("SHA-384");
        // 執行摘要方法
        byte[] digest = md.digest(data);
        return new HexBinaryAdapter().marshal(digest);
    }

    /**
     * SHA-512消息摘要算法
     */
    public static String encodeSHA512(byte[] data) throws Exception {
        // 初始化MessageDigest,SHA即SHA-1的簡稱
        MessageDigest md = MessageDigest.getInstance("SHA-512");
        // 執行摘要方法
        byte[] digest = md.digest(data);
        return new HexBinaryAdapter().marshal(digest);
    }

    public static void main(String[] args) throws Exception {
        String testString="asd`12asd31";
        System.out.println(SHACoder.encodeSHA(testString.getBytes()));
        System.out.println(SHACoder.encodeSHA256(testString.getBytes()));
        System.out.println(SHACoder.encodeSHA384(testString.getBytes()));
        System.out.println(SHACoder.encodeSHA512(testString.getBytes()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy