Java BASE58 以及 md5,sha256,sha1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 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()); } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步