Java BASE58 以及 md5,sha256,sha1

1package cn.ubibi.wsblog.utils; 2 3import java.io.UnsupportedEncodingException; 4import java.math.BigInteger; 5 6 7 8 9public class Base58 { 10 11 private static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray(); 12 private static final int[] INDEXES = new int[128]; 13 14 static { 15 for (int i = 0; i < INDEXES.length; i++) { 16 INDEXES[i] = -1; 17 } 18 for (int i = 0; i < ALPHABET.length; i++) { 19 INDEXES[ALPHABET[i]] = i; 20 } 21 } 22 23 /** 24 * Encodes the given bytes in base58. No checksum is appended. 25 */ 26 public static String encode(byte[] input) { 27 if (input.length == 0) { 28 return ""; 29 } 30 input = copyOfRange(input, 0, input.length); 31 // Count leading zeroes. 32 int zeroCount = 0; 33 while (zeroCount < input.length && input[zeroCount] == 0) { 34 ++zeroCount; 35 } 36 // The actual encoding. 37 byte[] temp = new byte[input.length * 2]; 38 int j = temp.length; 39 40 int startAt = zeroCount; 41 while (startAt < input.length) { 42 byte mod = divmod58(input, startAt); 43 if (input[startAt] == 0) { 44 ++startAt; 45 } 46 temp[--j] = (byte) ALPHABET[mod]; 47 } 48 49 // Strip extra '1' if there are some after decoding. 50 while (j < temp.length && temp[j] == ALPHABET[0]) { 51 ++j; 52 } 53 // Add as many leading '1' as there were leading zeros. 54 while (--zeroCount >= 0) { 55 temp[--j] = (byte) ALPHABET[0]; 56 } 57 58 byte[] output = copyOfRange(temp, j, temp.length); 59 try { 60 return new String(output, "US-ASCII"); 61 } catch (UnsupportedEncodingException e) { 62 throw new RuntimeException(e); // Cannot happen. 63 } 64 } 65 66 public static byte[] decode(String input) throws IllegalArgumentException { 67 if (input.length() == 0) { 68 return new byte[0]; 69 } 70 byte[] input58 = new byte[input.length()]; 71 // Transform the String to a base58 byte sequence 72 for (int i = 0; i < input.length(); ++i) { 73 char c = input.charAt(i); 74 75 int digit58 = -1; 76 if (c >= 0 && c < 128) { 77 digit58 = INDEXES[c]; 78 } 79 if (digit58 < 0) { 80 throw new IllegalArgumentException("Illegal character " + c + " at " + i); 81 } 82 83 input58[i] = (byte) digit58; 84 } 85 // Count leading zeroes 86 int zeroCount = 0; 87 while (zeroCount < input58.length && input58[zeroCount] == 0) { 88 ++zeroCount; 89 } 90 // The encoding 91 byte[] temp = new byte[input.length()]; 92 int j = temp.length; 93 94 int startAt = zeroCount; 95 while (startAt < input58.length) { 96 byte mod = divmod256(input58, startAt); 97 if (input58[startAt] == 0) { 98 ++startAt; 99 } 100 101 temp[--j] = mod; 102 } 103 // Do no add extra leading zeroes, move j to first non null byte. 104 while (j < temp.length && temp[j] == 0) { 105 ++j; 106 } 107 108 return copyOfRange(temp, j - zeroCount, temp.length); 109 } 110 111 public static BigInteger decodeToBigInteger(String input) throws IllegalArgumentException { 112 return new BigInteger(1, decode(input)); 113 } 114 115 // 116 // number -> number / 58, returns number % 58 117 // 118 private static byte divmod58(byte[] number, int startAt) { 119 int remainder = 0; 120 for (int i = startAt; i < number.length; i++) { 121 int digit256 = (int) number[i] & 0xFF; 122 int temp = remainder * 256 + digit256; 123 124 number[i] = (byte) (temp / 58); 125 126 remainder = temp % 58; 127 } 128 129 return (byte) remainder; 130 } 131 132 // 133 // number -> number / 256, returns number % 256 134 // 135 private static byte divmod256(byte[] number58, int startAt) { 136 int remainder = 0; 137 for (int i = startAt; i < number58.length; i++) { 138 int digit58 = (int) number58[i] & 0xFF; 139 int temp = remainder * 58 + digit58; 140 141 number58[i] = (byte) (temp / 256); 142 143 remainder = temp % 256; 144 } 145 146 return (byte) remainder; 147 } 148 149 private static byte[] copyOfRange(byte[] source, int from, int to) { 150 byte[] range = new byte[to - from]; 151 System.arraycopy(source, from, range, 0, range.length); 152 153 return range; 154 } 155 156 157} 158 159package cn.ubibi.wsblog.utils; 160 161import org.slf4j.Logger; 162import org.slf4j.LoggerFactory; 163 164import java.security.MessageDigest; 165import java.util.Base64; 166 167public class CryptoUtils { 168 169 private static final Logger LOGGER = LoggerFactory.getLogger(CryptoUtils.class); 170 171 private static final String ALGORITHM_MD5 = "MD5"; 172 private static final String ALGORITHM_SHA256 = "SHA-256"; 173 private static final String ALGORITHM_SHA1 = "SHA-1"; 174 private static final String CHAT_SET_UTF8 = "UTF-8"; 175 private static final String ENCODE_STRING_HEX = "hex"; 176 private static final String ENCODE_STRING_BASE64 = "base64"; 177 private static final String ENCODE_STRING_BASE58 = "base58"; 178 179 180 //32个字符 181 public static String encrypt_md5_hex(String str) { 182 return encrypt_hash_function(str, ALGORITHM_MD5, CHAT_SET_UTF8, ENCODE_STRING_HEX); 183 } 184 185 //24个字符 186 public static String encrypt_md5_base64(String str) { 187 return encrypt_hash_function(str, ALGORITHM_MD5, CHAT_SET_UTF8, ENCODE_STRING_BASE64); 188 } 189 190 //22个字符 191 public static String encrypt_md5_base58(String str) { 192 return encrypt_hash_function(str, ALGORITHM_MD5, CHAT_SET_UTF8, ENCODE_STRING_BASE58); 193 } 194 195 //40个字符 196 public static String encrypt_sha1_hex(String str) { 197 return encrypt_hash_function(str, ALGORITHM_SHA1, CHAT_SET_UTF8, ENCODE_STRING_HEX); 198 } 199 200 201 //28个字符 202 public static String encrypt_sha1_base64(String str) { 203 return encrypt_hash_function(str, ALGORITHM_SHA1, CHAT_SET_UTF8, ENCODE_STRING_BASE64); 204 } 205 206 //28个字符 207 public static String encrypt_sha1_base58(String str) { 208 return encrypt_hash_function(str, ALGORITHM_SHA1, CHAT_SET_UTF8, ENCODE_STRING_BASE58); 209 } 210 211 212 //64个字符 213 public static String encrypt_sha256_hex(String str) { 214 return encrypt_hash_function(str, ALGORITHM_SHA256, CHAT_SET_UTF8, ENCODE_STRING_HEX); 215 } 216 217 //44个字符 218 public static String encrypt_sha256_base64(String str) { 219 return encrypt_hash_function(str, ALGORITHM_SHA256, CHAT_SET_UTF8, ENCODE_STRING_BASE64); 220 } 221 222 //44个字符 223 public static String encrypt_sha256_base58(String str) { 224 return encrypt_hash_function(str, ALGORITHM_SHA256, CHAT_SET_UTF8, ENCODE_STRING_BASE58); 225 } 226 227 228 private static String encrypt_hash_function(String str, String algorithm, String chatset, String encodeMethod) { 229 MessageDigest messageDigest; 230 String encodeStr = ""; 231 try { 232 messageDigest = MessageDigest.getInstance(algorithm); 233 messageDigest.update(str.getBytes(chatset)); 234 235 byte[] digest_bytes = messageDigest.digest(); 236 237 if (ENCODE_STRING_BASE64.equals(encodeMethod)) { 238 encodeStr = byte2Base64(digest_bytes); 239 } else if (ENCODE_STRING_BASE58.equals(encodeMethod)) { 240 encodeStr = Base58.encode(digest_bytes); 241 } else { 242 encodeStr = byte2Hex(digest_bytes); 243 } 244 245 } catch (Exception e) { 246 LOGGER.error("", e); 247 } 248 return encodeStr; 249 } 250 251 252 private static String byte2Base64(byte[] bytes) { 253 return Base64.getEncoder().encodeToString(bytes); 254 } 255 256 257 private static String byte2Hex(byte[] bytes) { 258 StringBuffer stringBuffer = new StringBuffer(); 259 String temp; 260 for (int i = 0; i < bytes.length; i++) { 261 temp = Integer.toHexString(bytes[i] & 0xFF); 262 if (temp.length() == 1) { 263 stringBuffer.append("0"); 264 } 265 stringBuffer.append(temp); 266 } 267 return stringBuffer.toString(); 268 } 269 270 271 public static void main(String[] args) { 272 String x = encrypt_sha256_base58("12345"); 273 System.out.println(x); 274 System.out.println(x.length()); 275 } 276 277}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Java BASE58 以及 md5,sha256,sha1 - HelloWorld