zoukankan      html  css  js  c++  java
  • JAVA-产生唯一32位GUID

    import java.net.*;
    import java.util.*;
    import java.security.*;
    import org.apache.log4j.Logger;
    
    /**
     * 產生唯一GUID
     */
    public class RandomGUID extends Object {
    
        public String valueBeforeMD5 = "";
        public String valueAfterMD5 = "";
        private static Random myRand;
        private static SecureRandom mySecureRand;
    
        static Logger logger = Logger.getLogger(RandomGUID.class);
    
        static {
            mySecureRand = new SecureRandom();
            long secureInitializer = mySecureRand.nextLong();
            myRand = new Random(secureInitializer);
        }
    
        /**
         * 生成32位GUID码
         * @return
         */
        static public String generatorGUID() {
            RandomGUID myGUID = new RandomGUID();
            return myGUID.toString();
        }
    
        public RandomGUID() {
            getRandomGUID(false);
        }
    
        public RandomGUID(boolean secure) {
            getRandomGUID(secure);
        }
    
        private void getRandomGUID(boolean secure) {
            MessageDigest md5 = null;
            StringBuffer sbValueBeforeMD5 = new StringBuffer();
    
            try {
                md5 = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e) {
                logger.error("Error: " + e);
            }
    
            try {
                InetAddress id = InetAddress.getLocalHost();
                long time = System.currentTimeMillis();
                long rand = 0;
    
                if (secure) {
                    rand = mySecureRand.nextLong();
                } else {
                    rand = myRand.nextLong();
                }
    
                sbValueBeforeMD5.append(id.toString());
                sbValueBeforeMD5.append(":");
                sbValueBeforeMD5.append(Long.toString(time));
                sbValueBeforeMD5.append(":");
                sbValueBeforeMD5.append(Long.toString(rand));
    
                valueBeforeMD5 = sbValueBeforeMD5.toString();
                md5.update(valueBeforeMD5.getBytes());
    
                byte[] array = md5.digest();
                StringBuffer sb = new StringBuffer();
                for (int j = 0; j < array.length; ++j) {
                    int b = array[j] & 0xFF;
                    if (b < 0x10)
                        sb.append('0');
                    sb.append(Integer.toHexString(b));
                }
    
                valueAfterMD5 = sb.toString();
    
            } catch (UnknownHostException e) {
                logger.error("Error:" + e);
            }
        }
    
        public String toString() {
            String raw = valueAfterMD5.toUpperCase();
            StringBuffer sb = new StringBuffer();
            sb.append(raw.substring(0, 8));
            sb.append(raw.substring(8, 12));
            sb.append(raw.substring(12, 16));
            sb.append(raw.substring(16, 20));
            sb.append(raw.substring(20));
            return sb.toString();
        }
    
        /*
         * test of class
         */
        public static void main(String[] args) {
    
            RandomGUID myGUID = new RandomGUID(true);
    
            logger.error("Seeding String=" + myGUID.valueBeforeMD5);
            logger.error("rawGUID=" + myGUID.valueAfterMD5);
            logger.error("RandomGUID=" + myGUID.toString());
    
        }
    
    }
    
  • 相关阅读:
    转:基于科大讯飞语音API语音识别开发详解
    转:Netty系列之Netty高性能之道
    转:hadoop知识整理
    转:nginx防DDOS攻击的简单配置
    转:Google论文之一----Bigtable学习翻译
    POJ 2112 Optimal Milking(最大流+二分)
    HDU 4647 Another Graph Game(贪心)
    HDU 4671 Partition(定理题)
    HDU 4648 Magic Pen 6
    HDU 4649 Professor Tian(DP)
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10211016.html
Copyright © 2011-2022 走看看