diff options
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | lib/securerandom.rb | 10 |
2 files changed, 14 insertions, 0 deletions
@@ -1,3 +1,7 @@ +Thu Jan 29 12:18:54 2009 Technorama Ltd. <oss-ruby@technorama.net> + + * lib/securerandom.rb: new method SecureRandom#uuid + Wed Jan 28 01:16:50 2009 Masatoshi SEKI <m_seki@mva.biglobe.ne.jp> * lib/erb.rb (def_erb_method): pass the trim_mode [Feature #1032] 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 |
