其他分享
首页 > 其他分享> > 比特币相关计算

比特币相关计算

作者:互联网

//计算sha256 hash
public static String sha256String(String data) throws Exception {
        MessageDigest sha256 = null;
        try {
            sha256 = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        assert sha256 != null;
        sha256.update(data.getBytes());
        return byte2HexStr(sha256.digest());
    }
    //字节数组转为16进制
    private static String byte2HexStr(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte byt : bytes) {
            sb.append(Integer.toString((byt & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    }
        //参数是16进制字节码:1a44b9f2
    public static String sha256HexString(String data) throws Exception {
        byte[] hex_arr = hexStr2HexBytes(data);
        return sha256(hex_arr).toLowerCase();
    }
    //16进制转为16进制字节数组
     public static byte[] hexStr2HexBytes(String hexStr) {
        if (null == hexStr || 0 == hexStr.length()) {
            return null;
        }
        hexStr = (hexStr.length() == 1) ? "0" + hexStr : hexStr;
        byte[] arr = new byte[hexStr.length() / 2];
        byte[] tmp = hexStr.getBytes();
        for (int i = 0; i < tmp.length / 2; i++) {
            arr[i] = unitBytes(tmp[i * 2], tmp[i * 2 + 1]);
        }
        return arr;
    }

    private static byte unitBytes(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[]{src0}));
        _b0 = (byte) (_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[]{src1}));
        return (byte) (_b0 ^ _b1);
    }
package mine;

public class Utils {
    //16进制字符串大小端颠倒排序
    public static String hexStrReverse(String str) {
        int length = str.length();
        String[] arr = new String[str.length() / 2];
        int j = 0;
        for (int i = length; i > 1; i = i - 2) {
            arr[j] = str.substring(i - 2, i);
            j++;
        }
        StringBuffer sb = new StringBuffer();

        for (String s : arr) {
            sb.append(s);
        }
        return sb.toString().toLowerCase();
    }

    //字节流转为16进制表示
    public static String encode(byte[] src) {
        String strHex = "";
        StringBuilder sb = new StringBuilder("");
        for (byte b : src) {
            strHex = Integer.toHexString(b & 0xFF);
            sb.append((strHex.length() == 1) ? "0" + strHex : strHex);//每个字节由两个字符表示,位数不够,高位补0
        }
        return sb.toString().trim();
    }

    //字符串转为字节流
    public static byte[] decode(String src) {
        int m = 0, n = 0;
        int byteLen = src.length() / 2;//每两个字符描述一个字节
        byte[] ret = new byte[byteLen];
        for (int i = 0; i < byteLen; i++) {
            m = i * 2 + 1;
            n = m + 1;
            int intVal = Integer.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));
            ret[i] = Byte.valueOf((byte) intVal);
        }
        return ret;
    }

    private static String hexStr = "0123456789ABCDEF";
    private static String[] binaryArray = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};

    //二进制数据转换为二进制字符串
    public static String bytes2BinStr(byte[] bArray) {
        String outStr = "";
        int pos = 0;
        for (byte b : bArray) {
            //高四位
            pos = (b & 0xF0) >> 4;
            outStr += binaryArray[pos];
            //低四位
            pos = b & 0x0F;
            outStr += binaryArray[pos];
        }
        return outStr;
    }

    //二进制数组转换为16进制字符串
    public static String bin2HexStr(byte[] bytes) {
        String result = "";
        String hex = "";
        for (byte aByte : bytes) {
            //字节高四位
            hex = String.valueOf(hexStr.charAt((aByte & 0xF0) >> 4));
            //字节低四位
            hex += String.valueOf(hexStr.charAt((aByte & 0x0F)));
            result += hex;
        }
        return result;
    }

    //16进制转换为二进制字节数组
    public static byte[] hexStr2BinArr(String hexString) {
        int len = hexString.length() / 2;
        byte[] bytes = new byte[len];
        byte high = 0;
        byte low = 0;
        for (int i = 0; i < len; i++) {
            //右移四位得到高位
            high = (byte) ((hexStr.indexOf(hexString.charAt(2 * i))) << 4);
            low = (byte) hexStr.indexOf(hexString.charAt(2 * i + 1));
            bytes[i] = (byte) (high | low);
        }
        return bytes;
    }

    //16进制转换为二进制字符串
    public static String hexStr2BinStr(String hexString) {
        return bytes2BinStr(hexStr2BinArr(hexString));
    }

    //十进制转为16进制字符串
    public static String decimalStr2HexStr(String decimalStr) {
        long number = Long.parseLong(decimalStr);
        return Long.toHexString(number);
    }
    
}

标签:String,比特,int,length,static,计算,相关,byte,hexStr
来源: https://blog.csdn.net/weixin_42248522/article/details/100585232