Base58简介

Base58采用的字符集合为“123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ”,从这不难看出,Base58是纯数字与字母组成而且去掉了容易引起视觉混淆的字符(0:数字零,O:大写O,I:大写i,l:小写L)。9个数字+49个字母=58个。由于没有特殊字符所以在采用鼠标双击或移动设备选择时可以自动识别全选。

Base58本身就是URLSafe。Base64的URFSafe模式虽然已经对URL支持的比较好,但UUID中还是包含“-或_”。

目前流行的比特币,采用的就是Base58Check编码,是在Base58基础上又增加了安全效验机制。

三、Base58编码器程序

由于Base58最近才兴起,Java与Apache Commons中并不包含编码器。

package org.noahx.uuid.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger; /**
* Created with IntelliJ IDEA.
* User: noah
* Date: 8/2/13
* Time: 10:36 AM
* To change this template use File | Settings | File Templates.
*/
public class Base58 { public 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;
} }

UUID生成程序

这个生成UUID程序包含了Base64(URLSafe)与Base58两种编码。

package org.noahx.uuid.util;

import org.apache.commons.codec.binary.Base64;

import java.nio.ByteBuffer;
import java.util.UUID; public abstract class UuidUtils { public static String uuid() {
UUID uuid = UUID.randomUUID();
return uuid.toString();
} public static String base64Uuid() {
UUID uuid = UUID.randomUUID();
return base64Uuid(uuid);
} protected static String base64Uuid(UUID uuid) { ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits()); return Base64.encodeBase64URLSafeString(bb.array());
} public static String encodeBase64Uuid(String uuidString) {
UUID uuid = UUID.fromString(uuidString);
return base64Uuid(uuid);
} public static String decodeBase64Uuid(String compressedUuid) { byte[] byUuid = Base64.decodeBase64(compressedUuid); ByteBuffer bb = ByteBuffer.wrap(byUuid);
UUID uuid = new UUID(bb.getLong(), bb.getLong());
return uuid.toString();
} public static String base58Uuid() {
UUID uuid = UUID.randomUUID();
return base58Uuid(uuid);
} protected static String base58Uuid(UUID uuid) { ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits()); return Base58.encode(bb.array());
} public static String encodeBase58Uuid(String uuidString) {
UUID uuid = UUID.fromString(uuidString);
return base58Uuid(uuid);
} public static String decodeBase58Uuid(String base58uuid) {
byte[] byUuid = Base58.decode(base58uuid);
ByteBuffer bb = ByteBuffer.wrap(byUuid);
UUID uuid = new UUID(bb.getLong(), bb.getLong());
return uuid.toString();
}
}

生成UUID的效果

1、Base64的效果

M0ISICCxQi6sP-KIq3kFOw
11YozyYYTvKmuUXpRDvoJA
KlZnS-MuT2m3d-the2chxg
8J3SC10AQzqZr6Im8V2xYA
ES1UiFTGTHqn6ADU5YW0aw
1usa208oT1q7FitKbQHH5Q
53aDQZxKTGyqmKCzDnBwYQ
SVVjViEoQXayWB9_JknKqQ
fP6znJIAT1uGMN9HW5o8cw
YR-2-kKmSOubhGr2LpFCgQ

可以看到有-与_字符。大家可以双击上面包含-的UUID,得到只选中部分的效果。

2、Base58的效果

MqJqC2rtZLkuHys6ed2Eai
QrS5w2t5etpRY3zTR1BAEJ
Qd6wcFFVz2ZSQb3voGGj8P
75bJdWMcEh6NhT51D5Uyju
2L7kTgsktxMBKLkfAo2iWC
UX2Twhbt1kstRziqc7iwCR
9tZNKCeR93taLHU6PVy8hN
HSn6JMibca4nG9URWokpwg
8eL4SNz2a4puEW8fD4njsG
GThFxPsdVUoZMfmKoEHwQX

Base58与Base64(URLSafe)一样也只需21或22个字符就可以标示128位的UUID数据。基本一样的长度,看上去更舒服,当然以后就采用Base58来生成UUID。配合Hibernate的UUID生成器

生成Base58格式的UUID(Hibernate Base64格式的UUID续)的更多相关文章

  1. EF+LINQ事物处理 C# 使用NLog记录日志入门操作 ASP.NET MVC多语言 仿微软网站效果(转) 详解C#特性和反射(一) c# API接受图片文件以Base64格式上传图片 .NET读取json数据并绑定到对象

    EF+LINQ事物处理   在使用EF的情况下,怎么进行事务的处理,来减少数据操作时的失误,比如重复插入数据等等这些问题,这都是经常会遇到的一些问题 但是如果是我有多个站点,然后存在同类型的角色去操作 ...

  2. 怎么在Vue中使用Base64格式的背景

    问题发生于一次mock数据,生成了base64格式的东西,因为编码包在一个变量中,不知道怎么直接在 :style 中引入. 解答1:格式background-image: url(此处是我们mock生 ...

  3. 微信小程序 base64格式图片的显示及保存

    当我们拿到如下base64格式的图片(如下图)时, base64格式的图片数据: 如何显示 ? 使用image标签,src属性添加data:image/png;base64, (注意:若imgData ...

  4. uniapp微信小程序保存base64格式图片的方法

    uniapp保存base64格式图片的方法首先第一要先获取用户的权限 saveAlbum(){//获取权限保存相册 uni.getSetting({//获取用户的当前设置 success:(res)= ...

  5. html5 图片转为base64格式异步上传

    因为有这个需求(移动端),所以就研究了一下,发现还挺不错的.这个主要是用了html5的API,不需要其他的JS插件,不过只有支持html5的浏览器才行,就现在而言应该大部份都支持的.<!DOCT ...

  6. 在线生成CSS样式和兼容的字体格式

    http://www.fontsquirrel.com/tools/webfont-generator 在线生成CSS样式和兼容的字体格式.

  7. OpenSSL 1.0.0生成p12、jks、crt等格式证书的命令个过程(转)

    OpenSSL 1.0.0生成p12.jks.crt等格式证书的命令个过程   此生成的证书可用于浏览器.java.tomcat.c++等.在此备忘!     1.创建根证私钥命令:openssl g ...

  8. 将图片转换为base64 格式

    1.页面上的图片,转换成base64格式,可以通过canvas 的 toDataURL 例子:给定图片的url 将图片转换为base64 var imageSrc = "../images/ ...

  9. base64格式图片转换为FormData对象进行上传

    原理:理由ArrayBuffer.Blob和FormData var base64String = /*base64图片串*/; //这里对base64串进行操作,去掉url头,并转换为byte va ...

随机推荐

  1. css link和@import区别用法

    这里link与@import介绍的是html引入css的语法单词.两者均是引入css到html的单词. 1.link语法结构<link rel="stylesheet" ty ...

  2. 【HDOJ】5632 Rikka with Array

    1. 题目描述$A[i]$表示二级制表示的$i$的数字之和.求$1 \le i < j \le n$并且$A[i]>A[j]$的$(i,j)$的总对数. 2. 基本思路$n \le 10^ ...

  3. Java 包装类 自动装箱和拆箱

    包装类(Wrapper Class) 包装类是针对于原生数据类型的包装. 因为有8个原生数据类型,所以对应有8个包装类. 所有的包装类(8个)都位于java.lang下. Java中的8个包装类分别是 ...

  4. Save output to a text file from Mac terminal

      Simply with output redirection: system_profiler > file.txt Basically, this will take the output ...

  5. qt创建android项目后需要加入的参数

    默认用qtcreator5.2.0创建了一个quick项目,却报如下错误: error:cstdlib.h no such file or directory 解决方法: 打开项目文件untitled ...

  6. Oracle :一次数据库连接,返回多个结果集

    1. 一次数据库连接,返回多个结果集 1.1 建立包规范 create or replace package QX_GDJTJ is -- Author : xxx -- Created : 2012 ...

  7. MySQL安装之“测试”

    将MySQL安装完成之后还需要对其进行测试,判断MySQL是否安装成功,MySQL其可视化与我们之前使用过的SQLserver不同.MySQL其中测试方法有两种:一.使用MySQL命令进行测试:二.安 ...

  8. 【LeetCode 160】Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. Shell获取Aix/linux/unix机器上db2和os的信息并上传到指定服务器

    (之前写过一篇类似的文章,当时传输文件用的是ftp,因为项目觉得ftp不够安全所以这次换成了scp,同时对脚本的一些地方也做了一些调整) 其实做这个东西还是因为项目的需求,需要获取某些机器(目前主要是 ...

  10. <转>Python 参数知识(变量前加星号的意义)

    csdn上的牛人就是多,加油 —————————————————————————— 过量的参数 在运行时知道一个函数有什么参数,通常是不可能的.另一个情况是一个函数能操作很多对象.更有甚者,调用自身的 ...