diff options
| author | technorama <technorama@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-01-29 03:20:26 +0000 |
|---|---|---|
| committer | technorama <technorama@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-01-29 03:20:26 +0000 |
| commit | 8be949048b1aa45977504ee10dbace9f8dc6cb31 (patch) | |
| tree | a67d27e8190322f22936007cc279dcb2add21337 /lib | |
| parent | 2b6814d28f77522b826234002bcb20353a6f310c (diff) | |
| download | ruby-8be949048b1aa45977504ee10dbace9f8dc6cb31.tar.gz ruby-8be949048b1aa45977504ee10dbace9f8dc6cb31.tar.xz ruby-8be949048b1aa45977504ee10dbace9f8dc6cb31.zip | |
* lib/securerandom.rb: new method SecureRandom#uuid
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@21868 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/securerandom.rb | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/securerandom.rb b/lib/securerandom.rb index 0de118cb4..0582cee72 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') |
