guid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/CHANGES +2 -0
  2. data/FAQ +82 -0
  3. data/README +70 -0
  4. data/Rakefile +70 -0
  5. data/install.rb +1298 -0
  6. data/lib/guid.rb +153 -0
  7. metadata +62 -0
@@ -0,0 +1,153 @@
1
+ #
2
+ # Guid - Ruby library for portable GUID/UUID generation.
3
+ #
4
+ # Copyright (c) 2004 David Garamond <davegaramond at icqmail com>
5
+ #
6
+ # This library is free software; you can redistribute it and/or modify it
7
+ # under the same terms as Ruby itself.
8
+ #
9
+
10
+ if RUBY_PLATFORM =~ /win/i
11
+ module Guid_Win32_
12
+ require 'Win32API'
13
+
14
+ PROV_RSA_FULL = 1
15
+ CRYPT_VERIFYCONTEXT = 0xF0000000
16
+ FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
17
+ FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000
18
+
19
+ CryptAcquireContext = Win32API.new("advapi32", "CryptAcquireContext",
20
+ 'PPPII', 'L')
21
+ CryptGenRandom = Win32API.new("advapi32", "CryptGenRandom",
22
+ 'LIP', 'L')
23
+ CryptReleaseContext = Win32API.new("advapi32", "CryptReleaseContext",
24
+ 'LI', 'L')
25
+ GetLastError = Win32API.new("kernel32", "GetLastError", '', 'L')
26
+ FormatMessageA = Win32API.new("kernel32", "FormatMessageA",
27
+ 'LPLLPLPPPPPPPP', 'L')
28
+
29
+ def lastErrorMessage
30
+ code = GetLastError.call
31
+ msg = "\0" * 1024
32
+ len = FormatMessageA.call(FORMAT_MESSAGE_IGNORE_INSERTS +
33
+ FORMAT_MESSAGE_FROM_SYSTEM, 0,
34
+ code, 0, msg, 1024, nil, nil,
35
+ nil, nil, nil, nil, nil, nil)
36
+ msg[0, len].tr("\r", '').chomp
37
+ end
38
+
39
+ def initialize
40
+ hProvStr = " " * 4
41
+ if CryptAcquireContext.call(hProvStr, nil, nil, PROV_RSA_FULL,
42
+ CRYPT_VERIFYCONTEXT) == 0
43
+ raise SystemCallError, "CryptAcquireContext failed: #{lastErrorMessage}"
44
+ end
45
+ hProv, = hProvStr.unpack('L')
46
+ @bytes = " " * 16
47
+ if CryptGenRandom.call(hProv, 16, @bytes) == 0
48
+ raise SystemCallError, "CryptGenRandom failed: #{lastErrorMessage}"
49
+ end
50
+ if CryptReleaseContext.call(hProv, 0) == 0
51
+ raise SystemCallError, "CryptReleaseContext failed: #{lastErrorMessage}"
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ module Guid_Unix_
58
+ @@random_device = nil
59
+
60
+ def initialize
61
+ if !@@random_device
62
+ if File.exists? "/dev/urandom"
63
+ @@random_device = File.open "/dev/urandom", "r"
64
+ elsif File.exists? "/dev/random"
65
+ @@random_device = File.open "/dev/random", "r"
66
+ else
67
+ raise RuntimeError, "Can't find random device"
68
+ end
69
+ end
70
+
71
+ @bytes = @@random_device.read(16)
72
+ end
73
+ end
74
+
75
+ class Guid
76
+ if RUBY_PLATFORM =~ /win/
77
+ include Guid_Win32_
78
+ else
79
+ include Guid_Unix_
80
+ end
81
+
82
+ def hexdigest
83
+ @bytes.unpack("h*")[0]
84
+ end
85
+
86
+ def to_s
87
+ @bytes.unpack("h8 h4 h4 h4 h12").join "-"
88
+ end
89
+
90
+ def inspect
91
+ to_s
92
+ end
93
+
94
+ def raw
95
+ @bytes
96
+ end
97
+
98
+ def self.from_s(s)
99
+ raise ArgumentError, "Invalid GUID hexstring" unless
100
+ s =~ /\A[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}\z/i
101
+ guid = Guid.allocate
102
+ guid.instance_eval { @bytes = [s.gsub(/[^0-9a-f]+/i, '')].pack "h*" }
103
+ guid
104
+ end
105
+
106
+ def self.from_raw(bytes)
107
+ raise ArgumentError, "Invalid GUID raw bytes, length must be 16 bytes" unless
108
+ bytes.length == 16
109
+ guid = Guid.allocate
110
+ guid.instance_eval { @bytes = bytes }
111
+ guid
112
+ end
113
+
114
+ def ==(other)
115
+ @bytes == other.raw
116
+ end
117
+ end
118
+
119
+ if __FILE__ == $0
120
+ require 'test/unit'
121
+
122
+ class GuidTest < Test::Unit::TestCase
123
+ def test_new
124
+ g = Guid.new
125
+
126
+ # different representations of guid: hexdigest, hex+dashes, raw bytes
127
+ assert_equal(0, g.to_s =~ /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/)
128
+ assert_equal(16, g.raw.length)
129
+ assert_equal(0, g.hexdigest =~ /\A[0-9a-f]{32}\z/)
130
+ assert_equal(g.hexdigest, g.to_s.gsub(/-/, ''))
131
+
132
+ # must be different each time we produce (this is just a simple test)
133
+ g2 = Guid.new
134
+ assert_equal(true, g != g2)
135
+ assert_equal(true, g.to_s != g2.to_s)
136
+ assert_equal(true, g.raw != g2.raw)
137
+ assert_equal(true, g.hexdigest != g2.hexdigest)
138
+ assert_equal(1000, (1..1000).select { |i| g != Guid.new }.length)
139
+ end
140
+
141
+ def test_from_s
142
+ g = Guid.new
143
+ g2 = Guid.from_s(g.to_s)
144
+ assert_equal(g, g2)
145
+ end
146
+
147
+ def test_from_raw
148
+ g = Guid.new
149
+ g2 = Guid.from_raw(g.raw)
150
+ assert_equal(g, g2)
151
+ end
152
+ end
153
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Takita, Ross Hale, & David Garamond
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-15 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Guid is a Ruby library for portable GUID/UUID generation. At the moment it can be used on Windows (except first release of Win95 and older) and on Unix. This is a fork of David Garamond's ruby-uuid library.
17
+ email: opensource@pivotallabs.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - CHANGES
25
+ files:
26
+ - FAQ
27
+ - CHANGES
28
+ - Rakefile
29
+ - README
30
+ - install.rb
31
+ - lib/guid.rb
32
+ has_rdoc: true
33
+ homepage: http://pivotallabs.com
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --main
37
+ - README
38
+ - --inline-source
39
+ - --line-numbers
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project: pivotalrb
57
+ rubygems_version: 1.0.1
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: Guid is a Ruby library for portable GUID/UUID generation. At the moment it can be used on Windows (except first release of Win95 and older) and on Unix. This is a fork of David Garamond's ruby-uuid library.
61
+ test_files: []
62
+