Java BASE58 以及 md5,sha256,sha1

package cn.ubibi.wsblog.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

public class Base58 {

    private static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray();
    private static final int[] INDEXES = new int[128];

    static {
        for (int i = 0; i < INDEXES.length; i++) {
            INDEXES[i] = -1;
        }
        for (int i = 0; i < ALPHABET.length; i++) {
            INDEXES[ALPHABET[i]] = i;
        }
    }

    /**
     * Encodes the given bytes in base58. No checksum is appended.
     */
    public static String encode(byte[] input) {
        if (input.length == 0) {
            return "";
        }
        input = copyOfRange(input, 0, input.length);
        // Count leading zeroes.
        int zeroCount = 0;
        while (zeroCount < input.length && input[zeroCount] == 0) {
            ++zeroCount;
        }
        // The actual encoding.
        byte[] temp = new byte[input.length * 2];
        int j = temp.length;

        int startAt = zeroCount;
        while (startAt < input.length) {
            byte mod = divmod58(input, startAt);
            if (input[startAt] == 0) {
                ++startAt;
            }
            temp[--j] = (byte) ALPHABET[mod];
        }

        // Strip extra ‘1‘ if there are some after decoding.
        while (j < temp.length && temp[j] == ALPHABET[0]) {
            ++j;
        }
        // Add as many leading ‘1‘ as there were leading zeros.
        while (--zeroCount >= 0) {
            temp[--j] = (byte) ALPHABET[0];
        }

        byte[] output = copyOfRange(temp, j, temp.length);
        try {
            return new String(output, "US-ASCII");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);  // Cannot happen.
        }
    }

    public static byte[] decode(String input) throws IllegalArgumentException {
        if (input.length() == 0) {
            return new byte[0];
        }
        byte[] input58 = new byte[input.length()];
        // Transform the String to a base58 byte sequence
        for (int i = 0; i < input.length(); ++i) {
            char c = input.charAt(i);

            int digit58 = -1;
            if (c >= 0 && c < 128) {
                digit58 = INDEXES[c];
            }
            if (digit58 < 0) {
                throw new IllegalArgumentException("Illegal character " + c + " at " + i);
            }

            input58[i] = (byte) digit58;
        }
        // Count leading zeroes
        int zeroCount = 0;
        while (zeroCount < input58.length && input58[zeroCount] == 0) {
            ++zeroCount;
        }
        // The encoding
        byte[] temp = new byte[input.length()];
        int j = temp.length;

        int startAt = zeroCount;
        while (startAt < input58.length) {
            byte mod = divmod256(input58, startAt);
            if (input58[startAt] == 0) {
                ++startAt;
            }

            temp[--j] = mod;
        }
        // Do no add extra leading zeroes, move j to first non null byte.
        while (j < temp.length && temp[j] == 0) {
            ++j;
        }

        return copyOfRange(temp, j - zeroCount, temp.length);
    }

    public static BigInteger decodeToBigInteger(String input) throws IllegalArgumentException {
        return new BigInteger(1, decode(input));
    }

    //
    // number -> number / 58, returns number % 58
    //
    private static byte divmod58(byte[] number, int startAt) {
        int remainder = 0;
        for (int i = startAt; i < number.length; i++) {
            int digit256 = (int) number[i] & 0xFF;
            int temp = remainder * 256 + digit256;

            number[i] = (byte) (temp / 58);

            remainder = temp % 58;
        }

        return (byte) remainder;
    }

    //
    // number -> number / 256, returns number % 256
    //
    private static byte divmod256(byte[] number58, int startAt) {
        int remainder = 0;
        for (int i = startAt; i < number58.length; i++) {
            int digit58 = (int) number58[i] & 0xFF;
            int temp = remainder * 58 + digit58;

            number58[i] = (byte) (temp / 256);

            remainder = temp % 256;
        }

        return (byte) remainder;
    }

    private static byte[] copyOfRange(byte[] source, int from, int to) {
        byte[] range = new byte[to - from];
        System.arraycopy(source, from, range, 0, range.length);

        return range;
    }

}

  

package cn.ubibi.wsblog.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;
import java.util.Base64;

public class CryptoUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(CryptoUtils.class);

    private static final String ALGORITHM_MD5 = "MD5";
    private static final String ALGORITHM_SHA256 = "SHA-256";
    private static final String ALGORITHM_SHA1 = "SHA-1";
    private static final String CHAT_SET_UTF8 = "UTF-8";
    private static final String ENCODE_STRING_HEX = "hex";
    private static final String ENCODE_STRING_BASE64 = "base64";
    private static final String ENCODE_STRING_BASE58 = "base58";

    //32个字符
    public static String encrypt_md5_hex(String str) {
        return encrypt_hash_function(str, ALGORITHM_MD5, CHAT_SET_UTF8, ENCODE_STRING_HEX);
    }

    //24个字符
    public static String encrypt_md5_base64(String str) {
        return encrypt_hash_function(str, ALGORITHM_MD5, CHAT_SET_UTF8, ENCODE_STRING_BASE64);
    }

    //22个字符
    public static String encrypt_md5_base58(String str) {
        return encrypt_hash_function(str, ALGORITHM_MD5, CHAT_SET_UTF8, ENCODE_STRING_BASE58);
    }

    //40个字符
    public static String encrypt_sha1_hex(String str) {
        return encrypt_hash_function(str, ALGORITHM_SHA1, CHAT_SET_UTF8, ENCODE_STRING_HEX);
    }

    //28个字符
    public static String encrypt_sha1_base64(String str) {
        return encrypt_hash_function(str, ALGORITHM_SHA1, CHAT_SET_UTF8, ENCODE_STRING_BASE64);
    }

    //28个字符
    public static String encrypt_sha1_base58(String str) {
        return encrypt_hash_function(str, ALGORITHM_SHA1, CHAT_SET_UTF8, ENCODE_STRING_BASE58);
    }

    //64个字符
    public static String encrypt_sha256_hex(String str) {
        return encrypt_hash_function(str, ALGORITHM_SHA256, CHAT_SET_UTF8, ENCODE_STRING_HEX);
    }

    //44个字符
    public static String encrypt_sha256_base64(String str) {
        return encrypt_hash_function(str, ALGORITHM_SHA256, CHAT_SET_UTF8, ENCODE_STRING_BASE64);
    }

    //44个字符
    public static String encrypt_sha256_base58(String str) {
        return encrypt_hash_function(str, ALGORITHM_SHA256, CHAT_SET_UTF8, ENCODE_STRING_BASE58);
    }

    private static String encrypt_hash_function(String str, String algorithm, String chatset, String encodeMethod) {
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance(algorithm);
            messageDigest.update(str.getBytes(chatset));

            byte[] digest_bytes = messageDigest.digest();

            if (ENCODE_STRING_BASE64.equals(encodeMethod)) {
                encodeStr = byte2Base64(digest_bytes);
            } else if (ENCODE_STRING_BASE58.equals(encodeMethod)) {
                encodeStr = Base58.encode(digest_bytes);
            } else {
                encodeStr = byte2Hex(digest_bytes);
            }

        } catch (Exception e) {
            LOGGER.error("", e);
        }
        return encodeStr;
    }

    private static String byte2Base64(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }

    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) {
        String x = encrypt_sha256_base58("12345");
        System.out.println(x);
        System.out.println(x.length());
    }

}

  

原文地址:https://www.cnblogs.com/lhp2012/p/9387614.html

时间: 2024-08-13 19:23:29

Java BASE58 以及 md5,sha256,sha1的相关文章

.net实现md5加密 sha1加密 sha256加密 sha384加密 sha512加密 des加密解密

写项目时,后台一直用md5加密,一天群里人问,除了MD5还有其它的加密方法吗?当时只知道还有个SHA,但怎么实现什么的都不清楚,于是当网上找了下,把几种常见的加密方法都整理了下,用winform写了个程序,如图: 关键代码 using System.Security;using System.Security.Cryptography;using System.Web;using System.IO; //MD5 不区分大小写的        //type 类型,16位还是32位,16位就是取3

Java Base64、AES、SHA1、MD5加密算法(转载)

1 package com.example.decript; 2 3 import java.io.UnsupportedEncodingException; 4 import java.security.InvalidKeyException; 5 import java.security.MessageDigest; 6 import java.security.NoSuchAlgorithmException; 7 import java.security.SecureRandom; 8

(转)3DES、AES、RC6、TEA、RSA、MD5、SHA1、SHA256加密源码大聚齐

原贴地址:http://www.amobbs.com/thread-5466438-1-1.html DES---研究过加密的朋友十分熟悉,老牌的加密方法了.这是一个可逆的对称加密算 法,也是应用最广泛的密钥系统.好像是从1977年美国政府开始采用的.大家都看过U-571吧,DES的思路就是参照二战时期盟军缴获的德军恩格玛加密 机,不过DES比那个要NB的多多了.到现在为止,除了差分分析法和线性分析法外只有暴力穷举法了.前两种方法不是密码学家或数学家都不懂呵,不过穷举 DES,以现有我们大家都可

MD5,SHA1,SHA256,SHA512等常用加密算法

using System; using System.IO; using System.Data; using System.Text; using System.Diagnostics; using System.Security; using System.Security.Cryptography; /* * .Net框架由于拥有CLR提供的丰富库支持,只需很少的代码即可实现先前使用C等旧式语言很难实现的加密算法.本类实现一些常用机密算法,供参考.其中MD5算法返回Int的ToString

获取android应用签名证书(打包APK用到的那个文件)的SHA1,MD5,SHA256值

转载自:http://www.cnblogs.com/goldeneast/archive/2013/09/09/3309129.html http://developer.baidu.com/map/sdkandev-1.htm#.E7.AE.80.E4.BB.8B3(这个URL页面,最后面,百度地图也提供了2种获取方法) 今天,看到(百度地图android SDK 2.1.3以及之后的版本)的申请KEY中 安全码需要用到:签名证书SHA1的值. 1.使用CMD(命令行窗口),进入签名文件所在

字符串以及文件的hashlib的md5和sha1等的运用

hashlib的md5和sha1等的运用 import hashlib print(hashlib.algorithms_available) print(hashlib.algorithms_guaranteed) #MD5 import hashlib hash_object = hashlib.md5(b'Hello World') print(hash_object.hexdigest()) # import hashlib mystring = input('Enter String

密码学应用(DES,AES, MD5, SHA1, RSA, Salt, Pkcs8)

目录 一.数据加密标准 - Data Encryption Standard(DES) 二.高级加密标准 - Advanced Encryption Standard(AES) 三.消息摘要算法第五版 - Message-Digest Algorithm 5(MD5) 四.安全哈希算法 - Secure Hash Algorithm(SHA1) 五.公钥加密算法(RSA) 六.干扰项 - 盐(Salt) 七.RSA密钥格式Pkcs8 八.源码下载 数据加密标准 - Data Encryption

摘要算法测试MD5或SHA-1

package com.fenghao.other; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * * <P>摘要工具类使用MD5或SHA-1摘要算法</P> * @ClassName: MessageDigestUtils * @author 冯浩 2016年11月29日 下午3:15:54 * @see TODO */ public class M

MD5和sha1加密算法

在很多电子商务和社区应用中,我们都要存放很多的客户的资料,其中包括了很多的隐私信息和客户不愿被别人看到的信息,当然好有客户执行各种操作的密码,此时就需要对客户的信息进行加密再存储,目前有两种比较好的加密算法:MD5和sha1. 这两种加密算法都属于散列加密技术.所谓散列加密就是无论输入的字符串是什么,有多大,加密后都将变成唯一的定长的加密串. 首先介绍一下MD5,MD5的全称是Message-Digest Algorithm 5,在90年代初由MIT的计算机科学实验室和RSA Data Secu