summaryrefslogtreecommitdiff
path: root/lib/securerandom.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/securerandom.rb')
-rw-r--r--lib/securerandom.rb10
1 files changed, 10 insertions, 0 deletions
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')