digest-ed2k-hash 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/README.md +5 -0
- data/lib/digest/ed2k.rb +55 -40
- data/lib/digest/ed2k/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c566efddbb745ed6d57490058dec5e58d3cf5947
|
4
|
+
data.tar.gz: 62330d443a752bf07ff48e228f370440d2a522fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0215025b821f70c0f64c2d37340af4169890cf49b24555d4840762485e4c14110a6918de3881582037cab416b604e0be7e72bfd5a5191d8034af221458f20dfd
|
7
|
+
data.tar.gz: 9603340360cf8ab63ce224ca5cfd8ae6d879dfd085aa8f580ba112538331c759eac9a13004a29858fb853dbf670455add054a37f4f2d3282e169a9bf2d990b56
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
# ED2K digest for Ruby
|
2
2
|
|
3
3
|
[](https://gitlab.com/valeth/digest-ed2k-hash.rb/commits/master)
|
4
|
+
[](https://rubygems.org/gems/digest-ed2k-hash)
|
5
|
+
[](https://rubygems.org/gems/digest-ed2k-hash)
|
6
|
+
[](https://inch-ci.org/github/valeth/digest-ed2k-hash.rb)
|
7
|
+
[](https://codeclimate.com/github/valeth/digest-ed2k-hash.rb)
|
4
8
|
|
5
9
|
This is a Ruby implementation of the [ED2k](https://en.wikipedia.org/wiki/Ed2k_URI_scheme#eD2k_hash_algorithm) hashing algorithm.
|
6
10
|
Additional information can be found [here](http://wiki.anidb.net/w/Ed2k-hash).
|
7
11
|
|
12
|
+
[Documentation](http://www.rubydoc.info/gems/digest-ed2k-hash)
|
8
13
|
|
9
14
|
## Installation
|
10
15
|
|
data/lib/digest/ed2k.rb
CHANGED
@@ -25,11 +25,7 @@ module Digest # :nodoc:
|
|
25
25
|
# @param [IO] io the IO object that will be read
|
26
26
|
# @return self to allow method chaining
|
27
27
|
def io(io)
|
28
|
-
|
29
|
-
self << buf
|
30
|
-
end
|
31
|
-
|
32
|
-
self
|
28
|
+
self << io
|
33
29
|
end
|
34
30
|
|
35
31
|
# Calculate the hash of a file.
|
@@ -46,51 +42,68 @@ module Digest # :nodoc:
|
|
46
42
|
def reset
|
47
43
|
@md4.reset
|
48
44
|
@finalized = false
|
49
|
-
@small = true
|
50
|
-
@buf = ''
|
51
45
|
|
52
46
|
self
|
53
47
|
end
|
54
48
|
|
55
49
|
# Rehash with new data.
|
56
50
|
#
|
57
|
-
# @param [String] data the chunk of data to add to the hash
|
51
|
+
# @param [String, IO] data the chunk of data to add to the hash
|
58
52
|
# @return self to allow method chaining
|
59
53
|
# @raise RuntimeError if the digest object has been finalized
|
60
54
|
def update(data)
|
61
55
|
raise RuntimeError if @finalized
|
62
56
|
|
63
|
-
|
64
|
-
|
57
|
+
# get the IO object
|
58
|
+
buf = to_io(data)
|
59
|
+
|
60
|
+
# if the chunk is smaller than CHUNK_SIZE just return the MD4 hash
|
61
|
+
if buf.size < CHUNK_SIZE
|
62
|
+
@md4 << buf.read
|
63
|
+
else
|
64
|
+
# read chunks from the IO object and update the MD4 hash
|
65
|
+
while (chunk = buf.read(CHUNK_SIZE))
|
66
|
+
@md4 << MD4.digest(chunk)
|
67
|
+
end
|
68
|
+
|
69
|
+
# weird EDonkey bug requires multiples of CHUNK_SIZE
|
70
|
+
# to append one additional MD4 hash
|
71
|
+
@md4 << MD4.new.digest if multiple?(buf.size)
|
72
|
+
end
|
65
73
|
|
66
74
|
self
|
67
75
|
end
|
68
76
|
|
69
77
|
alias << update
|
70
78
|
|
71
|
-
# Finalize the digest.
|
79
|
+
# Finalize the hash and return the digest.
|
72
80
|
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
|
76
|
-
|
81
|
+
# If no string is provided, the current hash is used
|
82
|
+
#
|
83
|
+
# @param [String, IO] data hash this chunk of data
|
84
|
+
def digest(data = nil)
|
85
|
+
if data.nil?
|
77
86
|
finish
|
78
87
|
@md4.digest
|
79
88
|
else
|
80
89
|
reset
|
81
|
-
self <<
|
90
|
+
self << data
|
82
91
|
digest
|
83
92
|
end
|
84
93
|
end
|
85
94
|
|
86
|
-
#
|
87
|
-
|
88
|
-
|
95
|
+
# Finalize the hash and return the hexdigest.
|
96
|
+
#
|
97
|
+
# If no string is provided, the current hash is used
|
98
|
+
#
|
99
|
+
# @param [String, IO] data hash this chunk of data
|
100
|
+
def hexdigest(data = nil)
|
101
|
+
if data.nil?
|
89
102
|
finish
|
90
103
|
@md4.hexdigest
|
91
104
|
else
|
92
105
|
reset
|
93
|
-
self <<
|
106
|
+
self << data
|
94
107
|
hexdigest
|
95
108
|
end
|
96
109
|
end
|
@@ -99,15 +112,7 @@ module Digest # :nodoc:
|
|
99
112
|
#
|
100
113
|
# @return self to allow method chaining
|
101
114
|
def finish
|
102
|
-
unless @finalized
|
103
|
-
@md4 << if @small
|
104
|
-
@buf
|
105
|
-
else
|
106
|
-
MD4.digest(@buf)
|
107
|
-
end
|
108
|
-
|
109
|
-
@finalized = true
|
110
|
-
end
|
115
|
+
@finalized = true unless @finalized
|
111
116
|
|
112
117
|
self
|
113
118
|
end
|
@@ -122,20 +127,22 @@ module Digest # :nodoc:
|
|
122
127
|
class << self
|
123
128
|
# Calculate the digest of a string.
|
124
129
|
#
|
125
|
-
# @param [String]
|
130
|
+
# @param [String, IO] data the string to digest
|
126
131
|
# @return a finalized digest object
|
127
|
-
def digest(
|
128
|
-
new.digest(
|
132
|
+
def digest(data)
|
133
|
+
new.digest(data)
|
129
134
|
end
|
130
135
|
|
131
136
|
# Calculate the hexdigest of a string.
|
132
|
-
#
|
137
|
+
#
|
138
|
+
# @param [String, IO] data the string to digest
|
133
139
|
# @return a finalized digest object
|
134
|
-
def hexdigest(
|
135
|
-
new.hexdigest(
|
140
|
+
def hexdigest(data)
|
141
|
+
new.hexdigest(data)
|
136
142
|
end
|
137
143
|
|
138
144
|
# Create a new digest object from a IO object.
|
145
|
+
#
|
139
146
|
# @param [IO] io the IO object to read
|
140
147
|
# @return a new digest object
|
141
148
|
def io(io)
|
@@ -143,7 +150,8 @@ module Digest # :nodoc:
|
|
143
150
|
end
|
144
151
|
|
145
152
|
# Create a new digest object from a file.
|
146
|
-
#
|
153
|
+
#
|
154
|
+
# @param [String] path the file to read
|
147
155
|
# @return a new digest object
|
148
156
|
def file(path)
|
149
157
|
new.file(path)
|
@@ -152,11 +160,18 @@ module Digest # :nodoc:
|
|
152
160
|
|
153
161
|
private
|
154
162
|
|
155
|
-
def
|
156
|
-
|
157
|
-
|
158
|
-
|
163
|
+
def to_io(obj)
|
164
|
+
if obj.is_a? String
|
165
|
+
StringIO.new(obj)
|
166
|
+
elsif obj.is_a? IO
|
167
|
+
obj
|
168
|
+
else
|
169
|
+
raise ArgumentError, "cannot hash #{obj.class.name} object"
|
159
170
|
end
|
160
171
|
end
|
172
|
+
|
173
|
+
def multiple?(buf_size)
|
174
|
+
(buf_size % CHUNK_SIZE).zero?
|
175
|
+
end
|
161
176
|
end
|
162
177
|
end
|
data/lib/digest/ed2k/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: digest-ed2k-hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Valeth
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|