From 7dd1a098a6db52ff628cb504303726ccddf945df Mon Sep 17 00:00:00 2001 From: technorama Date: Thu, 29 Jan 2009 03:20:26 +0000 Subject: [PATCH] * lib/securerandom.rb: new method SecureRandom#uuid git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21868 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 4 ++++ lib/securerandom.rb | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/ChangeLog b/ChangeLog index 5440f4e3c4..e58116f99a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Thu Jan 29 12:18:54 2009 Technorama Ltd. + + * lib/securerandom.rb: new method SecureRandom#uuid + Thu Jan 29 11:22:19 2009 NAKAMURA Usaku * ext/socket/raddrinfo.c (inspect_sockaddr): if defined AF_INET6, diff --git a/lib/securerandom.rb b/lib/securerandom.rb index 0de118cb44..0582cee722 100644 --- a/lib/securerandom.rb +++ b/lib/securerandom.rb @@ -167,6 +167,16 @@ module SecureRandom end end + # SecureRandom.uuid generates a v4 random UUID. + def self.uuid + str = self.random_bytes(16) + str[6] = (str[6] & 0x0f) | 0x40 + str[8] = (str[8] & 0x3f) | 0x80 + + ary = str.unpack("NnnnnN") + "%08x-%04x-%04x-%04x-%04x%08x" % ary + end + # Following code is based on David Garamond's GUID library for Ruby. def self.lastWin32ErrorMessage # :nodoc: get_last_error = Win32API.new("kernel32", "GetLastError", '', 'L')