uuidtools 2.1.1 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG +15 -0
- data/LICENSE.txt +202 -0
- data/README.md +37 -0
- data/Rakefile +10 -23
- data/lib/uuidtools.rb +363 -172
- data/lib/uuidtools/version.rb +16 -21
- data/spec/spec_helper.rb +2 -0
- data/spec/uuidtools/mac_address_spec.rb +442 -3
- data/spec/uuidtools/utility_spec.rb +5 -5
- data/spec/uuidtools/uuid_creation_spec.rb +50 -41
- data/spec/uuidtools/uuid_parsing_spec.rb +63 -43
- data/tasks/benchmark.rake +46 -27
- data/tasks/gem.rake +31 -18
- data/tasks/rspec.rake +55 -0
- data/tasks/yard.rake +22 -0
- data/uuidtools.gemspec +42 -0
- metadata +84 -68
- data/LICENSE +0 -20
- data/README +0 -13
- data/tasks/clobber.rake +0 -2
- data/tasks/rdoc.rake +0 -29
- data/tasks/rubyforge.rake +0 -89
- data/tasks/spec.rake +0 -64
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a5cf7d8f6def3ddb0c325cfe8d2dec0404b73996c69f559888d74dad4f6519f1
|
4
|
+
data.tar.gz: 4ba7a0931d37d6f14f664e230d38610995d281167ab1c0e0ed2ea6355c93a060
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f9ac42163867349a27f20100c859be1103dec0b5c402f7630371d8debadb8a760b399f4f4369a941111987ca53ab68775947431f21ce29c7d267efb2a88fcc2c
|
7
|
+
data.tar.gz: 20b1d43a62100484f52844a919ba4683ca774da910e572acb865d599af5b6ad795180418746039b7a9b2f24942350a00414ca3309c6987dbf93085330017842c
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
== UUIDTools 2.2.0
|
2
|
+
* drop support for ruby < 2.3
|
3
|
+
* use Integer vs Fixnum
|
4
|
+
* performance improvements
|
5
|
+
== UUIDTools 2.1.5
|
6
|
+
* fixed issue with ip command vs ifconfig
|
7
|
+
* dumped RubyForge related cruft
|
8
|
+
* updated to modern RSpec syntax
|
9
|
+
== UUIDTools 2.1.4
|
10
|
+
* various OS-specific improvements to obtaining MAC address
|
11
|
+
== UUIDTools 2.1.3
|
12
|
+
* changes to build system
|
13
|
+
== UUIDTools 2.1.2
|
14
|
+
* fixed issue with frozen objects
|
15
|
+
* fixed issue with running specs in Ruby 1.9.2
|
1
16
|
== UUIDTools 2.1.1
|
2
17
|
* fixed issue with Ruby 1.9 compatibility
|
3
18
|
== UUIDTools 2.1.0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
[![Build Status](https://secure.travis-ci.org/sporkmonger/uuidtools.svg)](http://travis-ci.org/sporkmonger/uuidtools)
|
2
|
+
|
3
|
+
# UUIDTools
|
4
|
+
|
5
|
+
<dl>
|
6
|
+
<dt>Homepage</dt><dd><a href="https://github.com/sporkmonger/uuidtools">sporkmonger/uuidtools</a></dd>
|
7
|
+
<dt>Author</dt><dd><a href="mailto:bob@sporkmonger.com">Bob Aman</a></dd>
|
8
|
+
<dt>Copyright</dt><dd>Copyright © 2005-2014 Bob Aman</dd>
|
9
|
+
<dt>License</dt><dd>Apache 2.0</dd>
|
10
|
+
</dl>
|
11
|
+
|
12
|
+
# Description
|
13
|
+
|
14
|
+
UUIDTools was designed to be a simple library for generating any
|
15
|
+
of the various types of UUIDs. It conforms to RFC 4122 whenever
|
16
|
+
possible.
|
17
|
+
|
18
|
+
# Reference
|
19
|
+
|
20
|
+
- {UUIDTools::UUID}
|
21
|
+
|
22
|
+
# Example usage
|
23
|
+
|
24
|
+
require "uuidtools"
|
25
|
+
|
26
|
+
UUIDTools::UUID.md5_create(UUIDTools::UUID_DNS_NAMESPACE, "www.widgets.com")
|
27
|
+
# => #<UUID:0x287576 UUID:3d813cbb-47fb-32ba-91df-831e1593ac29>
|
28
|
+
UUIDTools::UUID.sha1_create(UUIDTools::UUID_DNS_NAMESPACE, "www.widgets.com")
|
29
|
+
# => #<UUID:0x2a0116 UUID:21f7f8de-8051-5b89-8680-0195ef798b6a>
|
30
|
+
UUIDTools::UUID.timestamp_create
|
31
|
+
# => #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8>
|
32
|
+
UUIDTools::UUID.random_create
|
33
|
+
# => #<UUID:0x19013a UUID:984265dc-4200-4f02-ae70-fe4f48964159>
|
34
|
+
|
35
|
+
# Install
|
36
|
+
|
37
|
+
$ sudo gem install uuidtools
|
data/Rakefile
CHANGED
@@ -1,29 +1,21 @@
|
|
1
|
-
lib_dir = File.expand_path(File.join(File.dirname(__FILE__), "lib"))
|
2
|
-
$:.unshift(lib_dir)
|
3
|
-
$:.uniq!
|
4
|
-
|
5
1
|
require 'rubygems'
|
6
2
|
require 'rake'
|
7
|
-
require 'rake/testtask'
|
8
|
-
require 'rake/rdoctask'
|
9
|
-
require 'rake/packagetask'
|
10
|
-
require 'rake/gempackagetask'
|
11
|
-
require 'spec/rake/spectask'
|
12
3
|
|
13
|
-
require File.join(File.dirname(__FILE__), 'lib
|
4
|
+
require File.join(File.dirname(__FILE__), 'lib', 'uuidtools', 'version')
|
14
5
|
|
15
6
|
PKG_DISPLAY_NAME = 'UUIDTools'
|
16
7
|
PKG_NAME = PKG_DISPLAY_NAME.downcase
|
17
|
-
PKG_VERSION =
|
8
|
+
PKG_VERSION = UUIDTools::VERSION::STRING
|
18
9
|
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
19
10
|
|
20
11
|
RELEASE_NAME = "REL #{PKG_VERSION}"
|
21
12
|
|
22
|
-
|
23
|
-
|
24
|
-
RUBY_FORGE_PATH = "/var/www/gforge-projects/#{RUBY_FORGE_PROJECT}"
|
25
|
-
RUBY_FORGE_URL = "http://#{RUBY_FORGE_PROJECT}.rubyforge.org/"
|
13
|
+
GIT_HUB_USER = "sporkmonger"
|
14
|
+
GIT_HUB_URL = "https://github.com/#{GIT_HUB_USER}/#{PKG_NAME}"
|
26
15
|
|
16
|
+
PKG_AUTHOR = "Bob Aman"
|
17
|
+
PKG_AUTHOR_EMAIL = "bob@sporkmonger.com"
|
18
|
+
PKG_HOMEPAGE = GIT_HUB_URL
|
27
19
|
PKG_SUMMARY = "UUID generator"
|
28
20
|
PKG_DESCRIPTION = <<-TEXT
|
29
21
|
A simple universally unique ID generation library.
|
@@ -33,14 +25,9 @@ PKG_FILES = FileList[
|
|
33
25
|
"lib/**/*", "spec/**/*", "vendor/**/*",
|
34
26
|
"tasks/**/*", "website/**/*",
|
35
27
|
"[A-Z]*", "Rakefile"
|
36
|
-
].exclude(/database\.yml/).exclude(/[_\.]git$/)
|
37
|
-
|
38
|
-
|
39
|
-
if RCOV_ENABLED
|
40
|
-
task :default => "spec:verify"
|
41
|
-
else
|
42
|
-
task :default => "spec"
|
43
|
-
end
|
28
|
+
].exclude(/database\.yml/).exclude(/[_\.]git$/).exclude(/Gemfile/).exclude(/Gemfile\.lock/)
|
29
|
+
|
30
|
+
task :default => "spec"
|
44
31
|
|
45
32
|
WINDOWS = (RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/) rescue false
|
46
33
|
SUDO = WINDOWS ? '' : ('sudo' unless ENV['SUDOLESS'])
|
data/lib/uuidtools.rb
CHANGED
@@ -1,26 +1,21 @@
|
|
1
|
+
# encoding:utf-8
|
1
2
|
#--
|
2
|
-
#
|
3
|
+
# Copyright (C) 2005-2014 Bob Aman
|
3
4
|
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
-
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
-
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
-
# the following conditions:
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
11
8
|
#
|
12
|
-
#
|
13
|
-
# included in all copies or substantial portions of the Software.
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14
10
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
22
16
|
#++
|
23
17
|
|
18
|
+
|
24
19
|
$:.unshift(File.dirname(__FILE__))
|
25
20
|
|
26
21
|
require 'uri'
|
@@ -38,30 +33,50 @@ rescue LoadError
|
|
38
33
|
end
|
39
34
|
|
40
35
|
module UUIDTools
|
41
|
-
|
42
|
-
#
|
36
|
+
##
|
43
37
|
# UUIDTools was designed to be a simple library for generating any
|
44
38
|
# of the various types of UUIDs. It conforms to RFC 4122 whenever
|
45
39
|
# possible.
|
46
40
|
#
|
47
|
-
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
41
|
+
# @example
|
42
|
+
# UUID.md5_create(UUID_DNS_NAMESPACE, "www.widgets.com")
|
43
|
+
# # => #<UUID:0x287576 UUID:3d813cbb-47fb-32ba-91df-831e1593ac29>
|
44
|
+
# UUID.sha1_create(UUID_DNS_NAMESPACE, "www.widgets.com")
|
45
|
+
# # => #<UUID:0x2a0116 UUID:21f7f8de-8051-5b89-8680-0195ef798b6a>
|
46
|
+
# UUID.timestamp_create
|
47
|
+
# # => #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8>
|
48
|
+
# UUID.random_create
|
49
|
+
# # => #<UUID:0x19013a UUID:984265dc-4200-4f02-ae70-fe4f48964159>
|
56
50
|
class UUID
|
57
51
|
include Comparable
|
58
52
|
|
53
|
+
##
|
54
|
+
# @api private
|
59
55
|
@@last_timestamp = nil
|
56
|
+
|
57
|
+
##
|
58
|
+
# @api private
|
60
59
|
@@last_node_id = nil
|
60
|
+
|
61
|
+
##
|
62
|
+
# @api private
|
61
63
|
@@last_clock_sequence = nil
|
64
|
+
|
65
|
+
##
|
66
|
+
# @api private
|
62
67
|
@@state_file = nil
|
68
|
+
|
69
|
+
##
|
70
|
+
# @api private
|
63
71
|
@@mutex = Mutex.new
|
64
72
|
|
73
|
+
##
|
74
|
+
# Creates a new UUID structure from its component values.
|
75
|
+
# @see UUID.md5_create
|
76
|
+
# @see UUID.sha1_create
|
77
|
+
# @see UUID.timestamp_create
|
78
|
+
# @see UUID.random_create
|
79
|
+
# @api private
|
65
80
|
def initialize(time_low, time_mid, time_hi_and_version,
|
66
81
|
clock_seq_hi_and_reserved, clock_seq_low, nodes)
|
67
82
|
unless time_low >= 0 && time_low < 4294967296
|
@@ -110,22 +125,38 @@ module UUIDTools
|
|
110
125
|
@nodes = nodes
|
111
126
|
end
|
112
127
|
|
128
|
+
##
|
129
|
+
# Returns the value of attribute `time_low`
|
113
130
|
attr_accessor :time_low
|
131
|
+
|
132
|
+
##
|
133
|
+
# Returns the value of attribute `time_mid`
|
114
134
|
attr_accessor :time_mid
|
135
|
+
|
136
|
+
##
|
137
|
+
# Returns the value of attribute `time_hi_and_version`
|
115
138
|
attr_accessor :time_hi_and_version
|
139
|
+
|
140
|
+
##
|
141
|
+
# Returns the value of attribute `clock_seq_hi_and_reserved`
|
116
142
|
attr_accessor :clock_seq_hi_and_reserved
|
143
|
+
|
144
|
+
##
|
145
|
+
# Returns the value of attribute `clock_seq_low`
|
117
146
|
attr_accessor :clock_seq_low
|
147
|
+
|
148
|
+
##
|
149
|
+
# Returns the value of attribute `nodes`
|
118
150
|
attr_accessor :nodes
|
119
151
|
|
152
|
+
##
|
120
153
|
# Parses a UUID from a string.
|
121
154
|
def self.parse(uuid_string)
|
122
155
|
unless uuid_string.kind_of? String
|
123
156
|
raise TypeError,
|
124
157
|
"Expected String, got #{uuid_string.class.name} instead."
|
125
158
|
end
|
126
|
-
uuid_components = uuid_string.downcase.scan(
|
127
|
-
Regexp.new("^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-" +
|
128
|
-
"([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{12})$")).first
|
159
|
+
uuid_components = uuid_string.downcase.scan(UUIDTools::UUID_REGEXP).first
|
129
160
|
raise ArgumentError, "Invalid UUID format." if uuid_components.nil?
|
130
161
|
time_low = uuid_components[0].to_i(16)
|
131
162
|
time_mid = uuid_components[1].to_i(16)
|
@@ -133,52 +164,114 @@ module UUIDTools
|
|
133
164
|
clock_seq_hi_and_reserved = uuid_components[3].to_i(16)
|
134
165
|
clock_seq_low = uuid_components[4].to_i(16)
|
135
166
|
nodes = []
|
136
|
-
|
167
|
+
6.times do |i|
|
137
168
|
nodes << uuid_components[5][(i * 2)..(i * 2) + 1].to_i(16)
|
138
169
|
end
|
139
170
|
return self.new(time_low, time_mid, time_hi_and_version,
|
140
171
|
clock_seq_hi_and_reserved, clock_seq_low, nodes)
|
141
172
|
end
|
142
173
|
|
174
|
+
##
|
143
175
|
# Parses a UUID from a raw byte string.
|
144
176
|
def self.parse_raw(raw_string)
|
145
177
|
unless raw_string.kind_of? String
|
146
178
|
raise TypeError,
|
147
179
|
"Expected String, got #{raw_string.class.name} instead."
|
148
180
|
end
|
149
|
-
integer = self.convert_byte_string_to_int(raw_string)
|
150
181
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
182
|
+
if raw_string.respond_to?(:force_encoding)
|
183
|
+
raw_string.force_encoding(Encoding::ASCII_8BIT)
|
184
|
+
end
|
185
|
+
|
186
|
+
raw_length = raw_string.length
|
187
|
+
if raw_length < 16
|
188
|
+
# Option A: Enforce raw_string be 16 characters (More strict)
|
189
|
+
#raise ArgumentError,
|
190
|
+
# "Expected 16 bytes, got #{raw_string.length} instead."
|
191
|
+
|
192
|
+
# Option B: Pad raw_string to 16 characters (Compatible with existing behavior)
|
193
|
+
raw_string = raw_string.rjust(16, "\0")
|
194
|
+
elsif raw_length > 16
|
195
|
+
# NOTE: As per "Option B" above, existing behavior would use the lower
|
196
|
+
# 128-bits of an overly long raw_string instead of using the upper 128-bits.
|
197
|
+
start_index = raw_length - 16
|
198
|
+
raw_string = raw_string[start_index...raw_length]
|
199
|
+
end
|
200
|
+
|
201
|
+
raw_bytes = []
|
202
|
+
if raw_string[0].respond_to? :ord
|
203
|
+
for i in 0...raw_string.size
|
204
|
+
raw_bytes << raw_string[i].ord
|
205
|
+
end
|
206
|
+
else
|
207
|
+
raw_bytes = raw_string
|
208
|
+
end
|
209
|
+
|
210
|
+
time_low = ((raw_bytes[0] << 24) +
|
211
|
+
(raw_bytes[1] << 16) +
|
212
|
+
(raw_bytes[2] << 8) +
|
213
|
+
raw_bytes[3])
|
214
|
+
time_mid = ((raw_bytes[4] << 8) +
|
215
|
+
raw_bytes[5])
|
216
|
+
time_hi_and_version = ((raw_bytes[6] << 8) +
|
217
|
+
raw_bytes[7])
|
218
|
+
clock_seq_hi_and_reserved = raw_bytes[8]
|
219
|
+
clock_seq_low = raw_bytes[9]
|
156
220
|
nodes = []
|
157
|
-
for i in
|
158
|
-
nodes <<
|
221
|
+
for i in 10...16
|
222
|
+
nodes << raw_bytes[i]
|
159
223
|
end
|
224
|
+
|
160
225
|
return self.new(time_low, time_mid, time_hi_and_version,
|
161
|
-
|
226
|
+
clock_seq_hi_and_reserved, clock_seq_low, nodes)
|
162
227
|
end
|
163
228
|
|
229
|
+
##
|
164
230
|
# Parses a UUID from an Integer.
|
165
231
|
def self.parse_int(uuid_int)
|
166
232
|
unless uuid_int.kind_of?(Integer)
|
167
233
|
raise ArgumentError,
|
168
234
|
"Expected Integer, got #{uuid_int.class.name} instead."
|
169
235
|
end
|
170
|
-
|
236
|
+
|
237
|
+
time_low = (uuid_int >> 96) & 0xFFFFFFFF
|
238
|
+
time_mid = (uuid_int >> 80) & 0xFFFF
|
239
|
+
time_hi_and_version = (uuid_int >> 64) & 0xFFFF
|
240
|
+
clock_seq_hi_and_reserved = (uuid_int >> 56) & 0xFF
|
241
|
+
clock_seq_low = (uuid_int >> 48) & 0xFF
|
242
|
+
nodes = []
|
243
|
+
for i in 0..5
|
244
|
+
nodes << ((uuid_int >> (40 - (i * 8))) & 0xFF)
|
245
|
+
end
|
246
|
+
|
247
|
+
return self.new(time_low, time_mid, time_hi_and_version,
|
248
|
+
clock_seq_hi_and_reserved, clock_seq_low, nodes)
|
171
249
|
end
|
172
250
|
|
251
|
+
##
|
173
252
|
# Parse a UUID from a hexdigest String.
|
174
|
-
def self.parse_hexdigest(
|
175
|
-
unless
|
253
|
+
def self.parse_hexdigest(uuid_hex)
|
254
|
+
unless uuid_hex.kind_of?(String)
|
176
255
|
raise ArgumentError,
|
177
|
-
"Expected String, got #{
|
256
|
+
"Expected String, got #{uuid_hex.class.name} instead."
|
178
257
|
end
|
179
|
-
|
258
|
+
|
259
|
+
time_low = uuid_hex[0...8].to_i(16)
|
260
|
+
time_mid = uuid_hex[8...12].to_i(16)
|
261
|
+
time_hi_and_version = uuid_hex[12...16].to_i(16)
|
262
|
+
clock_seq_hi_and_reserved = uuid_hex[16...18].to_i(16)
|
263
|
+
clock_seq_low = uuid_hex[18...20].to_i(16)
|
264
|
+
nodes_string = uuid_hex[20...32]
|
265
|
+
nodes = []
|
266
|
+
for i in 0..5
|
267
|
+
nodes << nodes_string[(i * 2)..(i * 2) + 1].to_i(16)
|
268
|
+
end
|
269
|
+
|
270
|
+
return self.new(time_low, time_mid, time_hi_and_version,
|
271
|
+
clock_seq_hi_and_reserved, clock_seq_low, nodes)
|
180
272
|
end
|
181
273
|
|
274
|
+
##
|
182
275
|
# Creates a UUID from a random value.
|
183
276
|
def self.random_create()
|
184
277
|
new_uuid = self.parse_raw(SecureRandom.random_bytes(16))
|
@@ -189,6 +282,7 @@ module UUIDTools
|
|
189
282
|
return new_uuid
|
190
283
|
end
|
191
284
|
|
285
|
+
##
|
192
286
|
# Creates a UUID from a timestamp.
|
193
287
|
def self.timestamp_create(timestamp=nil)
|
194
288
|
# We need a lock here to prevent two threads from ever
|
@@ -213,7 +307,7 @@ module UUIDTools
|
|
213
307
|
nodes = SecureRandom.random_bytes(6).unpack("C*")
|
214
308
|
nodes[0] |= 0b00000001
|
215
309
|
end
|
216
|
-
|
310
|
+
6.times do |i|
|
217
311
|
node_id += (nodes[i] << (40 - (i * 8)))
|
218
312
|
end
|
219
313
|
clock_sequence = @@last_clock_sequence
|
@@ -248,16 +342,19 @@ module UUIDTools
|
|
248
342
|
end
|
249
343
|
end
|
250
344
|
|
345
|
+
##
|
251
346
|
# Creates a UUID using the MD5 hash. (Version 3)
|
252
347
|
def self.md5_create(namespace, name)
|
253
348
|
return self.create_from_hash(Digest::MD5, namespace, name)
|
254
349
|
end
|
255
350
|
|
351
|
+
##
|
256
352
|
# Creates a UUID using the SHA1 hash. (Version 5)
|
257
353
|
def self.sha1_create(namespace, name)
|
258
354
|
return self.create_from_hash(Digest::SHA1, namespace, name)
|
259
355
|
end
|
260
356
|
|
357
|
+
##
|
261
358
|
# This method applies only to version 1 UUIDs.
|
262
359
|
# Checks if the node ID was generated from a random number
|
263
360
|
# or from an IEEE 802 address (MAC address).
|
@@ -269,6 +366,7 @@ module UUIDTools
|
|
269
366
|
return ((self.nodes.first & 0x01) == 1)
|
270
367
|
end
|
271
368
|
|
369
|
+
##
|
272
370
|
# Returns true if this UUID is the
|
273
371
|
# nil UUID (00000000-0000-0000-0000-000000000000).
|
274
372
|
def nil_uuid?
|
@@ -283,6 +381,7 @@ module UUIDTools
|
|
283
381
|
return true
|
284
382
|
end
|
285
383
|
|
384
|
+
##
|
286
385
|
# Returns the UUID version type.
|
287
386
|
# Possible values:
|
288
387
|
# 1 - Time-based with unique or random host identifier
|
@@ -294,6 +393,7 @@ module UUIDTools
|
|
294
393
|
return (time_hi_and_version >> 12)
|
295
394
|
end
|
296
395
|
|
396
|
+
##
|
297
397
|
# Returns the UUID variant.
|
298
398
|
# Possible values:
|
299
399
|
# 0b000 - Reserved, NCS backward compatibility.
|
@@ -313,6 +413,7 @@ module UUIDTools
|
|
313
413
|
return (result >> 6)
|
314
414
|
end
|
315
415
|
|
416
|
+
##
|
316
417
|
# Returns true if this UUID is valid.
|
317
418
|
def valid?
|
318
419
|
if [0b000, 0b100, 0b110, 0b111].include?(self.variant) &&
|
@@ -323,6 +424,7 @@ module UUIDTools
|
|
323
424
|
end
|
324
425
|
end
|
325
426
|
|
427
|
+
##
|
326
428
|
# Returns the IEEE 802 address used to generate this UUID or
|
327
429
|
# nil if a MAC address was not used.
|
328
430
|
def mac_address
|
@@ -333,6 +435,7 @@ module UUIDTools
|
|
333
435
|
end).join(":")
|
334
436
|
end
|
335
437
|
|
438
|
+
##
|
336
439
|
# Returns the timestamp used to generate this UUID
|
337
440
|
def timestamp
|
338
441
|
return nil if self.version != 1
|
@@ -345,8 +448,10 @@ module UUIDTools
|
|
345
448
|
(gmt_timestamp_100_nanoseconds - 0x01B21DD213814000) / 10000000.0)
|
346
449
|
end
|
347
450
|
|
451
|
+
##
|
348
452
|
# Compares two UUIDs lexically
|
349
453
|
def <=>(other_uuid)
|
454
|
+
return nil unless other_uuid.is_a?(UUIDTools::UUID)
|
350
455
|
check = self.time_low <=> other_uuid.time_low
|
351
456
|
return check if check != 0
|
352
457
|
check = self.time_mid <=> other_uuid.time_mid
|
@@ -358,7 +463,7 @@ module UUIDTools
|
|
358
463
|
return check if check != 0
|
359
464
|
check = self.clock_seq_low <=> other_uuid.clock_seq_low
|
360
465
|
return check if check != 0
|
361
|
-
|
466
|
+
6.times do |i|
|
362
467
|
if (self.nodes[i] < other_uuid.nodes[i])
|
363
468
|
return -1
|
364
469
|
end
|
@@ -369,169 +474,224 @@ module UUIDTools
|
|
369
474
|
return 0
|
370
475
|
end
|
371
476
|
|
477
|
+
##
|
372
478
|
# Returns a representation of the object's state
|
373
479
|
def inspect
|
374
480
|
return "#<UUID:0x#{self.object_id.to_s(16)} UUID:#{self.to_s}>"
|
375
481
|
end
|
376
482
|
|
483
|
+
##
|
377
484
|
# Returns the hex digest of the UUID object.
|
378
485
|
def hexdigest
|
379
|
-
|
486
|
+
(self.frozen? ?
|
487
|
+
generate_hexdigest : (@hexdigest ||= generate_hexdigest)
|
488
|
+
).dup
|
380
489
|
end
|
381
490
|
|
491
|
+
##
|
382
492
|
# Returns the raw bytes that represent this UUID.
|
383
493
|
def raw
|
384
|
-
|
494
|
+
(self.frozen? ? generate_raw : (@raw ||= generate_raw)).dup
|
385
495
|
end
|
386
496
|
|
497
|
+
##
|
387
498
|
# Returns a string representation for this UUID.
|
388
499
|
def to_s
|
389
|
-
|
390
|
-
@time_hi_and_version, @clock_seq_hi_and_reserved, @clock_seq_low);
|
391
|
-
for i in 0..5
|
392
|
-
result << sprintf("%2.2x", @nodes[i])
|
393
|
-
end
|
394
|
-
return result.downcase
|
500
|
+
(self.frozen? ? generate_s : (@string ||= generate_s)).dup
|
395
501
|
end
|
396
502
|
alias_method :to_str, :to_s
|
397
503
|
|
504
|
+
##
|
398
505
|
# Returns an integer representation for this UUID.
|
399
506
|
def to_i
|
400
|
-
@integer ||=
|
507
|
+
self.frozen? ? generate_i : (@integer ||= generate_i)
|
508
|
+
end
|
509
|
+
|
510
|
+
##
|
511
|
+
# Returns a URI string for this UUID.
|
512
|
+
def to_uri
|
513
|
+
return "urn:uuid:#{self.to_s}"
|
514
|
+
end
|
515
|
+
|
516
|
+
##
|
517
|
+
# Returns an integer hash value.
|
518
|
+
def hash
|
519
|
+
self.frozen? ? generate_hash : (@hash ||= generate_hash)
|
520
|
+
end
|
521
|
+
|
522
|
+
protected
|
523
|
+
##
|
524
|
+
# Generates the hex digest of the UUID object.
|
525
|
+
#
|
526
|
+
# @api private
|
527
|
+
def generate_hexdigest
|
528
|
+
return self.to_i.to_s(16).rjust(32, "0")
|
529
|
+
end
|
530
|
+
|
531
|
+
# Generates an integer hash value.
|
532
|
+
#
|
533
|
+
# @api private
|
534
|
+
def generate_hash
|
535
|
+
return self.to_i % 0x3fffffff
|
536
|
+
end
|
537
|
+
|
538
|
+
##
|
539
|
+
# Generates an integer representation for this UUID.
|
540
|
+
#
|
541
|
+
# @api private
|
542
|
+
def generate_i
|
543
|
+
return (begin
|
401
544
|
bytes = (time_low << 96) + (time_mid << 80) +
|
402
545
|
(time_hi_and_version << 64) + (clock_seq_hi_and_reserved << 56) +
|
403
546
|
(clock_seq_low << 48)
|
404
|
-
|
547
|
+
6.times do |i|
|
405
548
|
bytes += (nodes[i] << (40 - (i * 8)))
|
406
549
|
end
|
407
550
|
bytes
|
408
551
|
end)
|
409
552
|
end
|
410
553
|
|
411
|
-
|
412
|
-
|
413
|
-
|
554
|
+
##
|
555
|
+
# Generates a string representation for this UUID.
|
556
|
+
#
|
557
|
+
# @api private
|
558
|
+
def generate_s
|
559
|
+
result = sprintf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", @time_low, @time_mid,
|
560
|
+
@time_hi_and_version, @clock_seq_hi_and_reserved, @clock_seq_low);
|
561
|
+
6.times do |i|
|
562
|
+
result << sprintf("%2.2x", @nodes[i])
|
563
|
+
end
|
564
|
+
return result.downcase
|
414
565
|
end
|
415
566
|
|
416
|
-
|
417
|
-
|
418
|
-
|
567
|
+
##
|
568
|
+
# Generates the raw bytes that represent this UUID.
|
569
|
+
#
|
570
|
+
# @api private
|
571
|
+
def generate_raw
|
572
|
+
return self.class.convert_int_to_byte_string(self.to_i, 16)
|
419
573
|
end
|
420
574
|
|
575
|
+
public
|
576
|
+
##
|
421
577
|
# Returns true if this UUID is exactly equal to the other UUID.
|
422
578
|
def eql?(other)
|
423
579
|
return self == other
|
424
580
|
end
|
425
581
|
|
582
|
+
#
|
583
|
+
# Determine what OS we're running on. Helps decide how to find the MAC
|
584
|
+
#
|
585
|
+
def self.os_class
|
586
|
+
require 'rbconfig'
|
587
|
+
os_platform = RbConfig::CONFIG['target_os']
|
588
|
+
os_class = nil
|
589
|
+
if (os_platform =~ /win/i && !(os_platform =~ /darwin/i)) ||
|
590
|
+
os_platform =~ /w32/i
|
591
|
+
os_class = :windows
|
592
|
+
elsif os_platform =~ /solaris/i
|
593
|
+
os_class = :solaris
|
594
|
+
elsif os_platform =~ /netbsd/i
|
595
|
+
os_class = :netbsd
|
596
|
+
elsif os_platform =~ /openbsd/i
|
597
|
+
os_class = :openbsd
|
598
|
+
end
|
599
|
+
end
|
600
|
+
|
601
|
+
# making these class variables helps with testing
|
602
|
+
@ifconfig_command = "ifconfig"
|
603
|
+
@ifconfig_path_default = "/sbin/ifconfig"
|
604
|
+
@ip_command = "ip"
|
605
|
+
@ip_path_default = "/sbin/ip"
|
606
|
+
|
607
|
+
class << self
|
608
|
+
attr_accessor :ifconfig_command, :ifconfig_path_default
|
609
|
+
attr_accessor :ip_command, :ip_path_default
|
610
|
+
end
|
611
|
+
|
612
|
+
#
|
613
|
+
# Find the path of the ifconfig(8) command if it is present
|
614
|
+
#
|
615
|
+
def self.ifconfig_path
|
616
|
+
path = `which #{UUID.ifconfig_command} 2>/dev/null`.strip
|
617
|
+
path = UUID.ifconfig_path_default if (path == "" && File.exist?(UUID.ifconfig_path_default))
|
618
|
+
return (path === "" ? nil : path)
|
619
|
+
end
|
620
|
+
|
621
|
+
#
|
622
|
+
# Find the path of the ip(8) command if it is present
|
623
|
+
#
|
624
|
+
def self.ip_path
|
625
|
+
path = `which #{UUID.ip_command} 2>/dev/null`.strip
|
626
|
+
path = UUID.ip_path_default if (path == "" && File.exist?(UUID.ip_path_default))
|
627
|
+
return (path === "" ? nil : path)
|
628
|
+
end
|
629
|
+
|
630
|
+
#
|
631
|
+
# Call the ifconfig or ip command that is found
|
632
|
+
#
|
633
|
+
def self.ifconfig(all=nil)
|
634
|
+
# find the path of the ifconfig command
|
635
|
+
ifconfig_path = UUID.ifconfig_path
|
636
|
+
|
637
|
+
# if it does not exist, try the ip command
|
638
|
+
if ifconfig_path == nil
|
639
|
+
ifconfig_path = "#{UUID.ip_path} addr list"
|
640
|
+
# all makes no sense when using ip(1)
|
641
|
+
all = nil
|
642
|
+
end
|
643
|
+
|
644
|
+
all_switch = all == nil ? "" : "-a"
|
645
|
+
return `#{ifconfig_path} #{all_switch}` if not ifconfig_path == nil
|
646
|
+
end
|
647
|
+
|
648
|
+
# Match and return the first Mac address found
|
649
|
+
def self.first_mac(instring)
|
650
|
+
mac_regexps = [
|
651
|
+
Regexp.new("address:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
|
652
|
+
Regexp.new("addr:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
|
653
|
+
Regexp.new("ether:? (#{(["[0-9a-fA-F]{1,2}"] * 6).join(":")})"),
|
654
|
+
Regexp.new("HWaddr:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
|
655
|
+
Regexp.new("link/ether? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
|
656
|
+
Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
|
657
|
+
Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join("-")})")
|
658
|
+
]
|
659
|
+
parse_mac = lambda do |output|
|
660
|
+
(mac_regexps.map do |regexp|
|
661
|
+
result = output[regexp, 1]
|
662
|
+
result.downcase.gsub(/-/, ":") if result != nil
|
663
|
+
end).compact.first
|
664
|
+
end
|
665
|
+
|
666
|
+
mac = parse_mac.call(instring)
|
667
|
+
if mac
|
668
|
+
# expand octets that were compressed (solaris)
|
669
|
+
return (mac.split(':').map do |octet|
|
670
|
+
(octet.length == 1 ? "0#{octet}" : octet)
|
671
|
+
end).join(':')
|
672
|
+
else
|
673
|
+
return nil
|
674
|
+
end
|
675
|
+
end
|
676
|
+
|
677
|
+
##
|
426
678
|
# Returns the MAC address of the current computer's network card.
|
427
679
|
# Returns nil if a MAC address could not be found.
|
428
|
-
def self.mac_address
|
680
|
+
def self.mac_address
|
429
681
|
if !defined?(@@mac_address)
|
430
682
|
require 'rbconfig'
|
431
|
-
|
432
|
-
os_class =
|
433
|
-
|
434
|
-
os_platform =~ /w32/i
|
435
|
-
os_class = :windows
|
436
|
-
elsif os_platform =~ /solaris/i
|
437
|
-
os_class = :solaris
|
438
|
-
elsif os_platform =~ /netbsd/i
|
439
|
-
os_class = :netbsd
|
440
|
-
elsif os_platform =~ /openbsd/i
|
441
|
-
os_class = :openbsd
|
442
|
-
end
|
443
|
-
mac_regexps = [
|
444
|
-
Regexp.new("address:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
|
445
|
-
Regexp.new("addr:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
|
446
|
-
Regexp.new("ether:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
|
447
|
-
Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
|
448
|
-
Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join("-")})")
|
449
|
-
]
|
450
|
-
parse_mac = lambda do |output|
|
451
|
-
(mac_regexps.map do |regexp|
|
452
|
-
result = output[regexp, 1]
|
453
|
-
result.downcase.gsub(/-/, ":") if result != nil
|
454
|
-
end).compact.first
|
455
|
-
end
|
683
|
+
|
684
|
+
os_class = UUID.os_class
|
685
|
+
|
456
686
|
if os_class == :windows
|
457
|
-
script_in_path = true
|
458
|
-
else
|
459
|
-
script_in_path = Kernel.system("which ifconfig 2>&1 > /dev/null")
|
460
|
-
end
|
461
|
-
if os_class == :solaris
|
462
|
-
begin
|
463
|
-
ifconfig_output =
|
464
|
-
(script_in_path ? `ifconfig -a` : `/sbin/ifconfig -a`)
|
465
|
-
ip_addresses = ifconfig_output.scan(
|
466
|
-
/inet\s?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
|
467
|
-
ip = ip_addresses.find {|addr| addr[0] != '127.0.0.1'}[0]
|
468
|
-
@@mac_address = `/usr/sbin/arp #{ip}`.split(' ')[3]
|
469
|
-
rescue Exception
|
470
|
-
end
|
471
|
-
if @@mac_address == "" || @@mac_address == nil
|
472
|
-
begin
|
473
|
-
ifconfig_output =
|
474
|
-
(script_in_path ?
|
475
|
-
`ifconfig -a` : `/sbin/ifconfig -a`).split(' ')
|
476
|
-
index = ifconfig_output.index("inet") + 1
|
477
|
-
ip = ifconfig_output[index]
|
478
|
-
@@mac_address = `arp #{ip}`.split(' ')[3]
|
479
|
-
rescue Exception
|
480
|
-
end
|
481
|
-
end
|
482
|
-
elsif os_class == :windows
|
483
687
|
begin
|
484
|
-
@@mac_address =
|
485
|
-
rescue
|
486
|
-
end
|
487
|
-
else
|
488
|
-
begin
|
489
|
-
if os_class == :netbsd
|
490
|
-
@@mac_address = parse_mac.call(
|
491
|
-
script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
|
492
|
-
)
|
493
|
-
elsif os_class == :openbsd
|
494
|
-
@@mac_address = parse_mac.call(
|
495
|
-
script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
|
496
|
-
)
|
497
|
-
elsif File.exists?('/sbin/ifconfig')
|
498
|
-
@@mac_address = parse_mac.call(
|
499
|
-
script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`
|
500
|
-
)
|
501
|
-
if @@mac_address == nil
|
502
|
-
@@mac_address = parse_mac.call(
|
503
|
-
script_in_path ?
|
504
|
-
`ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
|
505
|
-
)
|
506
|
-
end
|
507
|
-
if @@mac_address == nil
|
508
|
-
@@mac_address = parse_mac.call(
|
509
|
-
script_in_path ?
|
510
|
-
`ifconfig | grep HWaddr | cut -c39- 2>&1` :
|
511
|
-
`/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`
|
512
|
-
)
|
513
|
-
end
|
514
|
-
else
|
515
|
-
@@mac_address = parse_mac.call(
|
516
|
-
script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`
|
517
|
-
)
|
518
|
-
if @@mac_address == nil
|
519
|
-
@@mac_address = parse_mac.call(
|
520
|
-
script_in_path ?
|
521
|
-
`ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
|
522
|
-
)
|
523
|
-
end
|
524
|
-
if @@mac_address == nil
|
525
|
-
@@mac_address = parse_mac.call(
|
526
|
-
script_in_path ?
|
527
|
-
`ifconfig | grep HWaddr | cut -c39- 2>&1` :
|
528
|
-
`/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`
|
529
|
-
)
|
530
|
-
end
|
531
|
-
end
|
688
|
+
@@mac_address = UUID.first_mac `ipconfig /all`
|
532
689
|
rescue
|
533
690
|
end
|
691
|
+
else # linux, bsd, macos, solaris
|
692
|
+
@@mac_address = UUID.first_mac(UUID.ifconfig(:all))
|
534
693
|
end
|
694
|
+
|
535
695
|
if @@mac_address != nil
|
536
696
|
if @@mac_address.respond_to?(:to_str)
|
537
697
|
@@mac_address = @@mac_address.to_str
|
@@ -552,6 +712,7 @@ module UUIDTools
|
|
552
712
|
return @@mac_address
|
553
713
|
end
|
554
714
|
|
715
|
+
##
|
555
716
|
# Allows users to set the MAC address manually in cases where the MAC
|
556
717
|
# address cannot be obtained programatically.
|
557
718
|
def self.mac_address=(new_mac_address)
|
@@ -561,8 +722,12 @@ module UUIDTools
|
|
561
722
|
# The following methods are not part of the public API,
|
562
723
|
# and generally should not be called directly.
|
563
724
|
|
725
|
+
|
726
|
+
##
|
564
727
|
# Creates a new UUID from a SHA1 or MD5 hash
|
565
|
-
|
728
|
+
#
|
729
|
+
# @api private
|
730
|
+
def self.create_from_hash(hash_class, namespace, name)
|
566
731
|
if hash_class == Digest::MD5
|
567
732
|
version = 3
|
568
733
|
elsif hash_class == Digest::SHA1
|
@@ -585,34 +750,60 @@ module UUIDTools
|
|
585
750
|
return new_uuid
|
586
751
|
end
|
587
752
|
|
588
|
-
|
753
|
+
##
|
754
|
+
# @api private
|
755
|
+
def self.convert_int_to_byte_string(integer, size)
|
589
756
|
byte_string = ""
|
590
757
|
if byte_string.respond_to?(:force_encoding)
|
591
758
|
byte_string.force_encoding(Encoding::ASCII_8BIT)
|
592
759
|
end
|
593
|
-
|
760
|
+
size.times do |i|
|
594
761
|
byte_string << ((integer >> (((size - 1) - i) * 8)) & 0xFF)
|
595
762
|
end
|
596
763
|
return byte_string
|
597
764
|
end
|
598
765
|
|
599
|
-
|
766
|
+
##
|
767
|
+
# @api private
|
768
|
+
def self.convert_byte_string_to_int(byte_string)
|
600
769
|
if byte_string.respond_to?(:force_encoding)
|
601
770
|
byte_string.force_encoding(Encoding::ASCII_8BIT)
|
602
771
|
end
|
772
|
+
|
603
773
|
integer = 0
|
604
774
|
size = byte_string.size
|
605
|
-
|
606
|
-
|
607
|
-
byte_string[i].ord
|
608
|
-
|
775
|
+
if byte_string[0].respond_to? :ord
|
776
|
+
for i in 0...size
|
777
|
+
integer += (byte_string[i].ord << (((size - 1) - i) * 8))
|
778
|
+
end
|
779
|
+
else
|
780
|
+
for i in 0...size
|
781
|
+
integer += (byte_string[i] << (((size - 1) - i) * 8))
|
782
|
+
end
|
609
783
|
end
|
784
|
+
|
610
785
|
return integer
|
611
786
|
end
|
612
787
|
end
|
613
788
|
|
789
|
+
##
|
790
|
+
# Constant Regexp that matches a UUID and captures its components.
|
791
|
+
UUID_REGEXP = Regexp.new("^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-" +
|
792
|
+
"([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{12})$")
|
793
|
+
|
794
|
+
##
|
795
|
+
# Constant that represents the DNS namespace.
|
614
796
|
UUID_DNS_NAMESPACE = UUID.parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
|
797
|
+
|
798
|
+
##
|
799
|
+
# Constant that represents the URL namespace.
|
615
800
|
UUID_URL_NAMESPACE = UUID.parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
|
801
|
+
|
802
|
+
##
|
803
|
+
# Constant that represents the OID namespace.
|
616
804
|
UUID_OID_NAMESPACE = UUID.parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
|
805
|
+
|
806
|
+
##
|
807
|
+
# Constant that represents the X500 namespace.
|
617
808
|
UUID_X500_NAMESPACE = UUID.parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
|
618
809
|
end
|