objecthash 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.rubocop.yml +39 -0
- data/.travis.yml +15 -0
- data/CHANGES.md +7 -0
- data/Gemfile +14 -0
- data/Guardfile +8 -0
- data/LICENSE +202 -0
- data/README.md +80 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/objecthash.rb +143 -0
- data/lib/objecthash/version.rb +3 -0
- data/objecthash.gemspec +24 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2089181fcfda36f9d990b35901276a5c74d97d3a
|
4
|
+
data.tar.gz: e62cd6cff3db9ac44a5e325529186637fa3247c3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 07656d48d95258e4ce9e4597120f378a94b8569d101ce62820128eecdd6cee8acdba58cea65a9343be4d2dd137f7d6899be3c03053d475d40222eefd2eece7de
|
7
|
+
data.tar.gz: be41f7fc690ec9624c00d5abf646977ea310425f25da4400d02abd45ca7d14e950cc0cf33ac686ff02bc4f64c0c3ef718aa01098e93ac66f9102f19128ac3abb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.2
|
3
|
+
DisplayCopNames: true
|
4
|
+
Exclude:
|
5
|
+
- '**/*.pb.rb'
|
6
|
+
#
|
7
|
+
# Style
|
8
|
+
#
|
9
|
+
|
10
|
+
LineLength:
|
11
|
+
Max: 128
|
12
|
+
|
13
|
+
Style/StringLiterals:
|
14
|
+
EnforcedStyle: double_quotes
|
15
|
+
|
16
|
+
Style/SpaceBeforeFirstArg:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Style/ConditionalAssignment:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Style/ModuleFunction:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
#
|
26
|
+
# Metrics
|
27
|
+
#
|
28
|
+
|
29
|
+
Metrics/MethodLength:
|
30
|
+
Max: 50
|
31
|
+
|
32
|
+
Metrics/AbcSize:
|
33
|
+
Max: 15
|
34
|
+
|
35
|
+
Metrics/CyclomaticComplexity:
|
36
|
+
Max: 10
|
37
|
+
|
38
|
+
Metrics/PerceivedComplexity:
|
39
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CHANGES.md
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
202
|
+
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# ObjectHash
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/objecthash.svg)](https://rubygems.org/gems/objecthash)
|
4
|
+
[![Build Status](https://secure.travis-ci.org/cryptosphere/objecthash-ruby.svg?branch=master)](https://travis-ci.org/cryptosphere/objecthash-ruby)
|
5
|
+
[![Apache 2 licensed](https://img.shields.io/badge/license-Apache2-blue.svg)](https://github.com/cryptosphere/objecthash-ruby/blob/master/LICENSE)
|
6
|
+
|
7
|
+
A content hash algorithm which works across multiple encodings (JSON, Protobufs, etc).
|
8
|
+
|
9
|
+
This gem is a Ruby implementation of a format [originally created by Ben Laurie](https://github.com/benlaurie/objecthash).
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem "objecthash"
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install objecthash
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
ObjectHash can calculate a digest of object graphs composed of a constrained
|
30
|
+
set of types. The following types are supported:
|
31
|
+
|
32
|
+
* Booleans: `true`, `false`, `nil`
|
33
|
+
* Numerics: Fixnum, Float
|
34
|
+
* Array
|
35
|
+
* Hash
|
36
|
+
* Set
|
37
|
+
* String (and Symbol)
|
38
|
+
|
39
|
+
The `#digest` and `#hexdigest` methods are available on ObjectHash:
|
40
|
+
|
41
|
+
Please note that Strings have [Unicode normalization](http://ruby-doc.org/stdlib-2.2.0/libdoc/unicode_normalize/rdoc/String.html) applied to them by default.
|
42
|
+
You can disable this if desired by passing in the `normalize: false` keyword
|
43
|
+
argument to `#digest` or `#hexdigest`. `String#unicode_normalize` was added in
|
44
|
+
MRI Ruby v2.2.0 and that is the minimum supported version for ObjectHash.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
>> ObjectHash.hexdigest("Hello, Ruby!")
|
48
|
+
=> "c92765c1350e6df6800dbadedb3420f398d5f3c7e38a3da48aadb3332280f85f"
|
49
|
+
>> ObjectHash.hexdigest("Hello, Ruby!", normalize: false)
|
50
|
+
=> "c92765c1350e6df6800dbadedb3420f398d5f3c7e38a3da48aadb3332280f85f"
|
51
|
+
>> ObjectHash.hexdigest([{complex: "structures"}, {can: "be"}, {hashed: ["with", "ObjectHash"]}])
|
52
|
+
=> "18717799dc21fff84b30a0654807348c632bc7498c5e40c025a6c18b9a2666d2"
|
53
|
+
```
|
54
|
+
|
55
|
+
Additionally you can provide a different hash algorithm by instantiating a class:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
>> objecthash = ObjectHash.new(Digest::SHA512)
|
59
|
+
=> #<ObjectHash:0x007ffdf39765f0 @hash_algorithm=Digest::SHA512>
|
60
|
+
>> objecthash.hexdigest("")
|
61
|
+
=> "58007911bf9f66711ef65e807b26c396a2d6fb464f4381520c5d4a575dbb81510f79d35e349604128a771acf2a117a2afdedc012d83b0eb822668aee0def4747"
|
62
|
+
```
|
63
|
+
|
64
|
+
For compatibility reasons it's recommended you use the default hash algorithm (SHA-256).
|
65
|
+
|
66
|
+
## TODO
|
67
|
+
|
68
|
+
* Adapt better tests from upstream objecthash_test.py
|
69
|
+
* Redaction support
|
70
|
+
|
71
|
+
## Contributing
|
72
|
+
|
73
|
+
* Fork this repository on Github
|
74
|
+
* Make your changes and send a pull request
|
75
|
+
* If your changes look good, we'll merge them
|
76
|
+
|
77
|
+
## Copyright
|
78
|
+
|
79
|
+
Copyright (c) 2016 Tony Arcieri, Ben Laurie. Distributed under the Apache 2.0 License.
|
80
|
+
See LICENSE file for further details.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "objecthash"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/objecthash.rb
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
# Originally designed by Ben Laurie
|
2
|
+
# Adapted from https://github.com/benlaurie/objecthash/blob/master/objecthash.py
|
3
|
+
|
4
|
+
require "objecthash/version"
|
5
|
+
|
6
|
+
require "digest/sha2"
|
7
|
+
require "set"
|
8
|
+
|
9
|
+
# A content hash algorithm which works across multiple encodings (JSON, Protobufs, etc)
|
10
|
+
class ObjectHash
|
11
|
+
# Default algorithm to use for computing object hashes
|
12
|
+
DEFAULT_HASH_ALGORITHM = Digest::SHA256
|
13
|
+
|
14
|
+
# Compute a raw ObjectHash digest using the default algorithm
|
15
|
+
def self.digest(object, normalize: true)
|
16
|
+
new.digest(object, normalize: normalize)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Compute an hex ObjectHash digest using the default algorithm
|
20
|
+
def self.hexdigest(object, normalize: true)
|
21
|
+
new.hexdigest(object, normalize: normalize)
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(hash_algorithm = DEFAULT_HASH_ALGORITHM)
|
25
|
+
@hash_algorithm = hash_algorithm
|
26
|
+
end
|
27
|
+
|
28
|
+
# Compute the ObjectHash of the given object
|
29
|
+
def digest(object, normalize: true)
|
30
|
+
case object
|
31
|
+
when Array then obj_hash_list(object)
|
32
|
+
when Hash then obj_hash_dict(object)
|
33
|
+
when String then obj_hash_unicode(object, normalize)
|
34
|
+
when Symbol then obj_hash_unicode(object.to_s, normalize)
|
35
|
+
when Float then obj_hash_float(object)
|
36
|
+
when Fixnum then obj_hash_int(object)
|
37
|
+
when Set then obj_hash_set(object)
|
38
|
+
when true, false then obj_hash_bool(object)
|
39
|
+
when nil then hash_primitive("n", "")
|
40
|
+
else raise TypeError, "unsupported: #{object.class}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Compute the ObjectHash of the given object as hexadecimal
|
45
|
+
def hexdigest(object, normalize: true)
|
46
|
+
digest(object, normalize: normalize).unpack("H*").first
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def hash_primitive(t, b)
|
52
|
+
m = @hash_algorithm.is_a?(Proc) ? @hash_algorithm.call : @hash_algorithm.new
|
53
|
+
m.update(t)
|
54
|
+
m.update(b)
|
55
|
+
m.digest
|
56
|
+
end
|
57
|
+
|
58
|
+
def obj_hash_bool(b)
|
59
|
+
hash_primitive("b", b ? "1" : "0")
|
60
|
+
end
|
61
|
+
|
62
|
+
def obj_hash_list(l)
|
63
|
+
h = l.map { |o| digest(o) }.join
|
64
|
+
hash_primitive("l", h)
|
65
|
+
end
|
66
|
+
|
67
|
+
def obj_hash_dict(d)
|
68
|
+
h = d.map { |k, v| digest(k) + digest(v) }.sort.join
|
69
|
+
hash_primitive("d", h)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Takes a unicode string and a boolean to indicate
|
73
|
+
# whether to normalize unicode or not.
|
74
|
+
def obj_hash_unicode(u, n)
|
75
|
+
u_enc = u.encode("utf-8")
|
76
|
+
u_norm = n ? u_enc.unicode_normalize(:nfc) : u_enc
|
77
|
+
hash_primitive("u", u_norm)
|
78
|
+
end
|
79
|
+
|
80
|
+
# rubocop:disable Metrics/AbcSize
|
81
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
82
|
+
def float_normalize(f)
|
83
|
+
# special case 0
|
84
|
+
# Note that if we allowed f to end up > .5 or == 0, we'd get the same thing
|
85
|
+
return "+0:" if f == 0.0
|
86
|
+
|
87
|
+
# sign
|
88
|
+
if f >= 0
|
89
|
+
s = "+"
|
90
|
+
else
|
91
|
+
s = "-"
|
92
|
+
f = -f
|
93
|
+
end
|
94
|
+
|
95
|
+
# exponent
|
96
|
+
e = 0
|
97
|
+
while f > 1
|
98
|
+
f /= 2
|
99
|
+
e += 1
|
100
|
+
end
|
101
|
+
|
102
|
+
while f <= 0.5
|
103
|
+
f *= 2
|
104
|
+
e -= 1
|
105
|
+
end
|
106
|
+
|
107
|
+
s += "#{e}:"
|
108
|
+
|
109
|
+
# mantissa
|
110
|
+
raise unless f <= 1
|
111
|
+
raise unless f > 0.5
|
112
|
+
|
113
|
+
while f
|
114
|
+
if f >= 1
|
115
|
+
s += "1"
|
116
|
+
f -= 1
|
117
|
+
else
|
118
|
+
s += "0"
|
119
|
+
end
|
120
|
+
|
121
|
+
raise unless f < 1
|
122
|
+
raise unless s.length < 1000
|
123
|
+
f *= 2
|
124
|
+
end
|
125
|
+
|
126
|
+
s
|
127
|
+
end
|
128
|
+
# rubocop:enable Metrics/AbcSize
|
129
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
130
|
+
|
131
|
+
def obj_hash_float(f)
|
132
|
+
hash_primitive("f", float_normalize(f))
|
133
|
+
end
|
134
|
+
|
135
|
+
def obj_hash_int(i)
|
136
|
+
hash_primitive("i", i.to_s)
|
137
|
+
end
|
138
|
+
|
139
|
+
def obj_hash_set(s)
|
140
|
+
h = s.map { |e| e << digest(e) }.sort.join
|
141
|
+
hash_primitive("s", h)
|
142
|
+
end
|
143
|
+
end
|
data/objecthash.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "objecthash/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "objecthash"
|
7
|
+
spec.version = ObjectHash::VERSION
|
8
|
+
spec.authors = ["Tony Arcieri"]
|
9
|
+
spec.email = ["bascule@gmail.com"]
|
10
|
+
spec.licenses = ["Apache-2.0"]
|
11
|
+
spec.homepage = "https://github.com/cryptosphere/objecthash-ruby"
|
12
|
+
spec.summary = "A content hash algorithm which works across multiple encodings (JSON, Protobufs, etc)"
|
13
|
+
spec.description = <<-DESCRIPTION.strip.gsub(/\s+/, " ")
|
14
|
+
A way to cryptographically hash objects (in the JSON-ish sense) that
|
15
|
+
works cross-language, and, therefore, cross-encoding.
|
16
|
+
DESCRIPTION
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.required_ruby_version = ">= 2.2.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: objecthash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tony Arcieri
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A way to cryptographically hash objects (in the JSON-ish sense) that
|
14
|
+
works cross-language, and, therefore, cross-encoding.
|
15
|
+
email:
|
16
|
+
- bascule@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".gitignore"
|
22
|
+
- ".rspec"
|
23
|
+
- ".rubocop.yml"
|
24
|
+
- ".travis.yml"
|
25
|
+
- CHANGES.md
|
26
|
+
- Gemfile
|
27
|
+
- Guardfile
|
28
|
+
- LICENSE
|
29
|
+
- README.md
|
30
|
+
- Rakefile
|
31
|
+
- bin/console
|
32
|
+
- bin/setup
|
33
|
+
- lib/objecthash.rb
|
34
|
+
- lib/objecthash/version.rb
|
35
|
+
- objecthash.gemspec
|
36
|
+
homepage: https://github.com/cryptosphere/objecthash-ruby
|
37
|
+
licenses:
|
38
|
+
- Apache-2.0
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.2.0
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.5.1
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: A content hash algorithm which works across multiple encodings (JSON, Protobufs,
|
60
|
+
etc)
|
61
|
+
test_files: []
|