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());
    
        }
    
    }
    
  • 相关阅读:
    C/C++ 读文件
    算法和数据结构 图表示
    protobuf
    C/C++ jsoncpp
    C/C++ C++11作用域枚举
    腾讯云云函数快速入门实践
    Serverless 与 Flask 框架结合进行 Blog 开发
    从企业微信机器人到小爱同学,用 Serverless 实现生活智能化!
    基于 Serverless 与 Websocket 的聊天工具实现
    云函数 SCF 与对象存储实现 WordCount 算法
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10211016.html
Copyright © 2011-2022 走看看