PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ru/adv/util/UniqId.java

http://mozart.googlecode.com/
Java | 103 lines | 63 code | 9 blank | 31 comment | 2 complexity | ad90a1616e8904682b0f5c0a97abe768 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0
  1. /* =================================================================
  2. Copyright (C) 2009 ADV/web-engineering All rights reserved.
  3. This file is part of Mozart.
  4. Mozart is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Mozart is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Foobar. If not, see <http://www.gnu.org/licenses/>.
  14. Mozart
  15. http://www.mozartcms.ru
  16. ================================================================= */
  17. // -*- java -*-
  18. // $Id: UniqId.java 1106 2009-06-03 07:32:17Z vic $
  19. // $Name: $
  20. package ru.adv.util;
  21. import java.io.BufferedInputStream;
  22. import java.io.FileInputStream;
  23. import java.net.InetAddress;
  24. import java.security.MessageDigest;
  25. import java.security.NoSuchAlgorithmException;
  26. import java.security.SecureRandom;
  27. /**
  28. * ????????? ?????????? ???????????????.
  29. * @version $Revision: 1.6 $
  30. */
  31. public class UniqId {
  32. private UniqId() {
  33. }
  34. private static SecureRandom seeder;
  35. private static ByteBuffer midValue = new ByteBuffer();
  36. static {
  37. byte[] bytes;
  38. // get the internet address
  39. try {
  40. InetAddress inet = InetAddress.getLocalHost();
  41. bytes = inet.getAddress();
  42. }
  43. catch (java.net.UnknownHostException e) {
  44. bytes = new byte[4];
  45. }
  46. midValue.append(bytes);
  47. midValue.append(System.identityHashCode(new Object()));
  48. try {
  49. byte[] buff = new byte[10];
  50. BufferedInputStream is = new BufferedInputStream(new FileInputStream("/dev/urandom"));
  51. Stream.nonBlockRead(is, buff);
  52. is.close();
  53. seeder = new SecureRandom(buff);
  54. }
  55. catch (Exception e) {
  56. seeder = new SecureRandom();
  57. }
  58. seeder.nextInt();
  59. }
  60. private static ByteBuffer getByteBuffer() {
  61. long timeNow = System.currentTimeMillis();
  62. // get int value as unsigned int
  63. int timeLow = (int) (timeNow & 0xFFFFFFFFL);
  64. // get next random value
  65. int node = seeder.nextInt();
  66. return new ByteBuffer(16).append(timeLow).append(midValue).append(node);
  67. }
  68. public static String getUUID() {
  69. return getByteBuffer().toString(16);
  70. }
  71. public static String get() {
  72. try {
  73. MessageDigest md = MessageDigest.getInstance("MD5");
  74. return new ByteBuffer(md.digest(getByteBuffer().getBytes()), false).toString(16);
  75. }
  76. catch (NoSuchAlgorithmException e) {
  77. throw new Error(e.getMessage());
  78. }
  79. }
  80. public static void main(String[] argv) {
  81. long count = 1;
  82. if (argv.length > 0) {
  83. count = Long.parseLong(argv[0]);
  84. }
  85. for (long i = 0; i < count; i++) {
  86. System.out.println(get());
  87. }
  88. }
  89. }