Java ID Value Create generateID(String origin)

Here you can find the source of generateID(String origin)

Description

generate ID

License

Apache License

Declaration

public synchronized static String generateID(String origin) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.security.MessageDigest;

import java.util.UUID;

public class Main {
    public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
            "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7",
            "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
            "T", "U", "V", "W", "X", "Y", "Z" };

    public synchronized static String generateID(String origin) {
        java.security.MessageDigest md;
        String pwd = null;/*  w  w  w.  ja va  2  s .c o m*/
        try {
            md = MessageDigest.getInstance("MD5");
            byte[] b = origin.getBytes("UTF-8");
            byte[] hash = md.digest(b);
            String str = byteArrayToHexString(hash);
            return str;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return pwd;
    }

    public synchronized static String generateID() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

    public static String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString().substring(0, 8);
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n = 256 + n;
        int d1 = n / 16;
        int d2 = n % 16;
        return chars[d1] + chars[d2];
    }
}

Related

  1. generateId()
  2. generateId()
  3. generateId()
  4. generateID(final String message)
  5. generateID(int seed)
  6. generateId(String salt)
  7. generateSalt()
  8. generateSalt()
  9. generateSalt()