summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authortechnorama <technorama@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-29 03:20:35 +0000
committertechnorama <technorama@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-29 03:20:35 +0000
commit7752cacd83c4e392951258bdc8b776d3ed87cf19 (patch)
tree5efd210867d276649a6d556abc7eb21187fb95c7 /lib
parent309eb9aa34cdcf2be9062fcf7dadb376cfd1b39c (diff)
* lib/securerandom.rb: new method SecureRandom#uuid
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@21869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/securerandom.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/securerandom.rb b/lib/securerandom.rb
index 3fd63e535f..ee9be9bde4 100644
--- a/lib/securerandom.rb
+++ b/lib/securerandom.rb
@@ -134,4 +134,14 @@ module SecureRandom
Math.ldexp(i64 >> (64-Float::MANT_DIG), -Float::MANT_DIG)
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
end