闲来无事,把我用到过的加密,或主键生成策略写一下
UUID
public static String getUUID(){
return UUID.randomUUID().toString();
}
//没有"-"
public static String compactUUID(){
return getUUID().replace("-","");
}
//自定义的UUID,并非严格系统格式,便于排序以及获取时间搓
public static String dateUUID(){
Random random = new Random();
String uuid=new SimpleDateFormat ("yyyyMMddHHmmssSSSZ").format(new Date());
uuid=uuid.replace("+","1").replace("-","0");
for (int i = 0; i < 10; i++) {
uuid+=random.nextInt(10);
}
return uuid;
}
//自定义的UUID,并非严格系统格式,该UUID可以当时间挫来使用,将时间搓转为36位,然后加入7位0-36的随机码
public static String dateUUID16(){
Random random = new Random();
String ranData="";
for (int i = 0; i < 8; i++) {
ranData+=Integer.toString(random.nextInt(36),36);
}
return Long.toString(new Date().getTime(),36)+ranData;
}
MD5加密
private static String MD5(String s) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(s.getBytes("utf-8"));
return toHex(bytes);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String toHex(byte[] bytes) {
final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
StringBuilder ret = new StringBuilder(bytes.length * 2);
for (int i=0; i<bytes.length; i++) {
ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
}
return ret.toString();
}
base64:
数组类型的适用于图片之类的
public static String base64Encoder(String date){
return base64Encoder(date.getBytes());
}
public static String base64Encoder(byte[] date){
return new String(Base64.getEncoder().encode(date));
}
public static String base64Decoder(byte[] date){
return new String(Base64.getDecoder().decode(date));
}
public static String base64Decoder(String date){
return new String(Base64.getDecoder().decode(date));
}