/src/ru/adv/util/UniqId.java
Java | 103 lines | 63 code | 9 blank | 31 comment | 2 complexity | ad90a1616e8904682b0f5c0a97abe768 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0
- /* =================================================================
- Copyright (C) 2009 ADV/web-engineering All rights reserved.
- This file is part of Mozart.
- Mozart is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- Mozart is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with Foobar. If not, see <http://www.gnu.org/licenses/>.
- Mozart
- http://www.mozartcms.ru
- ================================================================= */
- // -*- java -*-
- // $Id: UniqId.java 1106 2009-06-03 07:32:17Z vic $
- // $Name: $
- package ru.adv.util;
- import java.io.BufferedInputStream;
- import java.io.FileInputStream;
- import java.net.InetAddress;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.security.SecureRandom;
- /**
- * ????????? ?????????? ???????????????.
- * @version $Revision: 1.6 $
- */
- public class UniqId {
- private UniqId() {
- }
- private static SecureRandom seeder;
- private static ByteBuffer midValue = new ByteBuffer();
- static {
- byte[] bytes;
- // get the internet address
- try {
- InetAddress inet = InetAddress.getLocalHost();
- bytes = inet.getAddress();
- }
- catch (java.net.UnknownHostException e) {
- bytes = new byte[4];
- }
- midValue.append(bytes);
- midValue.append(System.identityHashCode(new Object()));
- try {
- byte[] buff = new byte[10];
- BufferedInputStream is = new BufferedInputStream(new FileInputStream("/dev/urandom"));
- Stream.nonBlockRead(is, buff);
- is.close();
- seeder = new SecureRandom(buff);
- }
- catch (Exception e) {
- seeder = new SecureRandom();
- }
- seeder.nextInt();
- }
- private static ByteBuffer getByteBuffer() {
- long timeNow = System.currentTimeMillis();
- // get int value as unsigned int
- int timeLow = (int) (timeNow & 0xFFFFFFFFL);
- // get next random value
- int node = seeder.nextInt();
- return new ByteBuffer(16).append(timeLow).append(midValue).append(node);
- }
- public static String getUUID() {
- return getByteBuffer().toString(16);
- }
- public static String get() {
- try {
- MessageDigest md = MessageDigest.getInstance("MD5");
- return new ByteBuffer(md.digest(getByteBuffer().getBytes()), false).toString(16);
- }
- catch (NoSuchAlgorithmException e) {
- throw new Error(e.getMessage());
- }
- }
- public static void main(String[] argv) {
- long count = 1;
- if (argv.length > 0) {
- count = Long.parseLong(argv[0]);
- }
- for (long i = 0; i < count; i++) {
- System.out.println(get());
- }
- }
- }