hakusho 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 413f789eff55357b7cdd8780d423b0a8f7c59853
4
+ data.tar.gz: 422b2240659bbc27b93678c1aeb58d2d3e673355
5
+ SHA512:
6
+ metadata.gz: bc3301a98f0aa9f00880f0751e75ba20a986752c51340f648ee2bf07ed3aba145f11ea7b51e2c3ef9a428753ac165f0cc915841aca5dfa85f3c2a7ef0dbc5260
7
+ data.tar.gz: ccc6b93a5d1f039548e603397a5c0e5daf6aa77422dc27e2604d247bdad25324c89e1499c703c59fc83177bb664261d79b82163aca1d3574ea916c194cbcae40
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc/
3
+ /_yardoc/
4
+ /Gemfile.lock
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :benchmark do
6
+ # the champions
7
+ gem "ruby-uuid"
8
+ gem "uuidtools"
9
+ gem "uuid4r"
10
+
11
+ # the ref
12
+ gem "benchmark-ips"
13
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Blake Imsland
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,144 @@
1
+ # Hakusho
2
+
3
+ A fast uuid generation and parsing library. Currently supports UUID versions 3, 4, and 5.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "hakusho"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hakusho
20
+
21
+ ## Usage
22
+
23
+ ### Parsing
24
+
25
+ ```ruby
26
+ uuid = Hakusho.parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
27
+ ```
28
+
29
+ ### Generation
30
+
31
+ #### Version 3 (MD5)
32
+
33
+ ```ruby
34
+ ns = Hakusho.parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
35
+ uuid = Hakusho.create_md5(ns, "this is a pretty rad stirng")
36
+ ```
37
+
38
+ ### Version 4 (Random)
39
+
40
+ ```ruby
41
+ uuid = Hakusho.create_random
42
+ ```
43
+
44
+ ### Version 5 (SHA1)
45
+
46
+ ```ruby
47
+ ns = Hakusho.parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
48
+ uuid = Hakusho.create_sha1(ns, "this is a pretty rad stirng")
49
+ ```
50
+
51
+ ## Benchmarks
52
+
53
+ These were created by running `bin/benchmark`.
54
+
55
+ ### UUID Version 3
56
+
57
+ ```
58
+ Calculating -------------------------------------
59
+ hakusho 7.672k i/100ms
60
+ uuidtools 1.056k i/100ms
61
+ ruby-uuid 1.709k i/100ms
62
+ uuid4r 2.045k i/100ms
63
+ -------------------------------------------------
64
+ hakusho 86.471k (± 3.3%) i/s - 437.304k
65
+ uuidtools 10.852k (± 3.5%) i/s - 54.912k
66
+ ruby-uuid 17.866k (± 3.4%) i/s - 90.577k
67
+ uuid4r 21.429k (± 3.1%) i/s - 108.385k
68
+
69
+ Comparison:
70
+ hakusho: 86471.2 i/s
71
+ uuid4r: 21429.2 i/s - 4.04x slower
72
+ ruby-uuid: 17865.8 i/s - 4.84x slower
73
+ uuidtools: 10852.4 i/s - 7.97x slower
74
+ ```
75
+
76
+ ### UUID Version 4
77
+
78
+ ```
79
+ Calculating -------------------------------------
80
+ fast_uuid 12.581k i/100ms
81
+ uuidtools 2.584k i/100ms
82
+ ruby-uuid 2.829k i/100ms
83
+ uuid4r 3.396k i/100ms
84
+ securerandom 10.522k i/100ms
85
+ -------------------------------------------------
86
+ fast_uuid 156.784k (± 3.3%) i/s - 792.603k
87
+ uuidtools 27.215k (± 3.1%) i/s - 136.952k
88
+ ruby-uuid 29.793k (± 3.9%) i/s - 149.937k
89
+ uuid4r 36.125k (± 3.1%) i/s - 183.384k
90
+ securerandom 129.485k (± 3.2%) i/s - 652.364k
91
+
92
+ Comparison:
93
+ fast_uuid: 156783.6 i/s
94
+ securerandom: 129485.5 i/s - 1.21x slower
95
+ uuid4r: 36125.3 i/s - 4.34x slower
96
+ ruby-uuid: 29792.8 i/s - 5.26x slower
97
+ uuidtools: 27214.7 i/s - 5.76x slower
98
+ ```
99
+
100
+ ### UUID Version 5
101
+
102
+ ```
103
+ Calculating -------------------------------------
104
+ hakusho 7.406k i/100ms
105
+ uuidtools 1.032k i/100ms
106
+ ruby-uuid 1.662k i/100ms
107
+ uuid4r 2.029k i/100ms
108
+ -------------------------------------------------
109
+ hakusho 82.308k (± 2.9%) i/s - 414.736k
110
+ uuidtools 10.669k (± 3.0%) i/s - 53.664k
111
+ ruby-uuid 17.515k (± 3.1%) i/s - 88.086k
112
+ uuid4r 21.395k (± 2.6%) i/s - 107.537k
113
+
114
+ Comparison:
115
+ hakusho: 82308.0 i/s
116
+ uuid4r: 21394.9 i/s - 3.85x slower
117
+ ruby-uuid: 17514.8 i/s - 4.70x slower
118
+ uuidtools: 10669.1 i/s - 7.71x slower
119
+ ```
120
+
121
+ ## TODO
122
+
123
+ * Tests (non-existant)
124
+ * Better documentation
125
+ * Version 1 & 2 UUIDs
126
+
127
+ ## Development
128
+
129
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
130
+
131
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
132
+
133
+ ## Credits
134
+
135
+ * [Matthew Callis](https://github.com/matthewcallis) -- For his inspiring name and design input
136
+ * [Ruby on Ales](https://twitter.com/rbonales) -- For the liquid courage to get this done
137
+
138
+ ## Contributing
139
+
140
+ 1. Fork it ( https://github.com/robotblake/hakusho/fork )
141
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
142
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
143
+ 4. Push to the branch (`git push origin my-new-feature`)
144
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+
5
+ # the champions
6
+ require "uuid"
7
+ require "uuidtools"
8
+ require "uuid4r"
9
+
10
+ # the challenger
11
+ require "hakusho"
12
+
13
+ # the referee
14
+ require "benchmark/ips"
15
+
16
+ # setup
17
+ uuid = "6ba7b811-9dad-11d1-80b4-00c04fd430c8"
18
+ str = "benchmarking things"
19
+
20
+ # v3 benchmark
21
+ puts "UUID Version 3\n"
22
+ Benchmark.ips do |x|
23
+ # Version 3
24
+ x.report("hakusho") { Hakusho.create_md5(Hakusho.parse(uuid), str).to_s }
25
+ x.report("uuidtools") { UUIDTools::UUID.md5_create(UUIDTools::UUID.parse(uuid), str).to_s }
26
+ x.report("ruby-uuid") { UUID.create_md5(str, UUID.parse(uuid)).to_s }
27
+ x.report("uuid4r") { UUID4R.uuid_v3("ns:URL", str) }
28
+ x.compare!
29
+ end
30
+
31
+ # v4 benchmark
32
+ puts "UUID Version 4\n"
33
+ Benchmark.ips do |x|
34
+ # Version 4
35
+ x.report("fast_uuid") { Hakusho.create_random.to_s }
36
+ x.report("uuidtools") { UUIDTools::UUID.random_create.to_s }
37
+ x.report("ruby-uuid") { UUID.create_random.to_s }
38
+ x.report("uuid4r") { UUID4R.uuid_v4 }
39
+ x.report("securerandom") { SecureRandom.uuid }
40
+ x.compare!
41
+ end
42
+
43
+ # v5 benchmark
44
+ puts "UUID Version 5\n"
45
+ Benchmark.ips do |x|
46
+ # Version 5
47
+ x.report("hakusho") { Hakusho.create_sha1(Hakusho.parse(uuid), str).to_s }
48
+ x.report("uuidtools") { UUIDTools::UUID.sha1_create(UUIDTools::UUID.parse(uuid), str).to_s }
49
+ x.report("ruby-uuid") { UUID.create_sha1(str, UUID.parse(uuid)).to_s }
50
+ x.report("uuid4r") { UUID4R.uuid_v5("ns:URL", str) }
51
+ x.compare!
52
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+
5
+ require "hakusho"
6
+
7
+ require "pry"
8
+ Pry.start
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+
3
+ set -euo pipefail
4
+
5
+ bundle install --path vendor/bundle --binstubs vendor/bundle/bin
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "hakusho/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hakusho"
8
+ spec.version = Hakusho::VERSION
9
+ spec.authors = ["Blake Imsland"]
10
+ spec.email = ["blake@retroco.de"]
11
+
12
+ spec.summary = "fast uuid generation and parsing library"
13
+ spec.description = "fast uuid generation and parsing library"
14
+ spec.homepage = "https://github.com/robotblake/hakusho"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.8"
23
+ spec.add_development_dependency "rake", "~> 10.4"
24
+
25
+ spec.add_development_dependency "pry-byebug", "~> 3.0.1"
26
+ spec.add_development_dependency "guard-rspec", "~> 4.5.0"
27
+ end
@@ -0,0 +1,69 @@
1
+ require "hakusho/version"
2
+ require "securerandom"
3
+
4
+ class Hakusho
5
+ FORMAT = /\A(\h{8})-(\h{4})-([1-5]\h{3})-(\h{4})-(\h{12})\Z/i
6
+ VERSIONS = [1, 2, 3, 4, 5]
7
+
8
+ attr_reader :raw
9
+
10
+ def initialize(bytes)
11
+ @raw = bytes
12
+ end
13
+
14
+ def version
15
+ @raw.getbyte(6) >> 4
16
+ end
17
+
18
+ def variant
19
+ byte = @raw.getbyte(8)
20
+ return "Reserved" if byte & 0x80 == 0x00
21
+ return "RFC4122" if byte & 0xc0 == 0x80
22
+ return "Microsoft" if byte & 0xe0 == 0xc0
23
+ return "Future" if byte & 0xe0 == 0xe0
24
+ end
25
+
26
+ def valid?
27
+ VERSIONS.include?(version) && variant
28
+ end
29
+
30
+ def to_s
31
+ @raw.unpack('H8H4H4H4H12').join(?-)
32
+ end
33
+
34
+ def self.parse(str)
35
+ if FORMAT =~ str
36
+ self.new([
37
+ str.byteslice(0, 8),
38
+ str.byteslice(9, 4),
39
+ str.byteslice(14, 4),
40
+ str.byteslice(19, 4),
41
+ str.byteslice(24, 12),
42
+ ].pack('H8H4H4H4H12'))
43
+ end
44
+ end
45
+
46
+ def self.create_md5(ns, str)
47
+ create_hash(Digest::MD5, ns, str, 0x30)
48
+ end
49
+
50
+ def self.create_random
51
+ create(SecureRandom.random_bytes(16), 0x40)
52
+ end
53
+
54
+ def self.create_sha1(ns, str)
55
+ create_hash(Digest::SHA1, ns, str, 0x50)
56
+ end
57
+
58
+ private
59
+
60
+ def self.create_hash(klass, ns, str, version)
61
+ create(klass.digest("#{ns.raw}#{str}").byteslice(0, 16), version)
62
+ end
63
+
64
+ def self.create(bytes, version)
65
+ bytes.setbyte(6, (bytes.getbyte(6) & 0x0f) | version)
66
+ bytes.setbyte(8, (bytes.getbyte(8) & 0x3f) | 0x80)
67
+ self.new(bytes)
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ class Hakusho
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hakusho
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Blake Imsland
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 4.5.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.5.0
69
+ description: fast uuid generation and parsing library
70
+ email:
71
+ - blake@retroco.de
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/benchmark
85
+ - bin/console
86
+ - bin/setup
87
+ - hakusho.gemspec
88
+ - lib/hakusho.rb
89
+ - lib/hakusho/version.rb
90
+ homepage: https://github.com/robotblake/hakusho
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.6
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: fast uuid generation and parsing library
114
+ test_files: []