digest-ed2k-hash 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c45ba32ed829add3658beb53e3d759e18f2c701
4
- data.tar.gz: 8c6c76c0908c82069b9e1ae9de60413b81bcfc2f
3
+ metadata.gz: c566efddbb745ed6d57490058dec5e58d3cf5947
4
+ data.tar.gz: 62330d443a752bf07ff48e228f370440d2a522fa
5
5
  SHA512:
6
- metadata.gz: 14dee83b52e39f823a7fb1428394f14a832b6893d3f560681f08c43c3fb85a9e5316dfbe8f3d96dbfea94d9593be263a698c0197f3da39c4b9a8e2d8119eb40c
7
- data.tar.gz: 20e6b9c2a9220ad706e64dccccbdfdc94dd140864676930b8bc258fe79349f9cd37d4b9a01928f74d6559e6807b04958d5bd5cdd1de79bc7b2faca57ceb2a02b
6
+ metadata.gz: 0215025b821f70c0f64c2d37340af4169890cf49b24555d4840762485e4c14110a6918de3881582037cab416b604e0be7e72bfd5a5191d8034af221458f20dfd
7
+ data.tar.gz: 9603340360cf8ab63ce224ca5cfd8ae6d879dfd085aa8f580ba112538331c759eac9a13004a29858fb853dbf670455add054a37f4f2d3282e169a9bf2d990b56
@@ -2,6 +2,8 @@ AllCops:
2
2
  TargetRubyVersion: 2.3
3
3
  DisplayCopNames: true
4
4
  DisplayStyleGuide: true
5
+ Exclude:
6
+ - 'spec/**/*'
5
7
 
6
8
 
7
9
  # Because only 2 spaces indent is insane
data/README.md CHANGED
@@ -1,10 +1,15 @@
1
1
  # ED2K digest for Ruby
2
2
 
3
3
  [![build status](https://gitlab.com/valeth/digest-ed2k-hash.rb/badges/master/build.svg)](https://gitlab.com/valeth/digest-ed2k-hash.rb/commits/master)
4
+ [![Gem](https://img.shields.io/gem/v/digest-ed2k-hash.svg)](https://rubygems.org/gems/digest-ed2k-hash)
5
+ [![Gem](https://img.shields.io/gem/dt/digest-ed2k-hash.svg)](https://rubygems.org/gems/digest-ed2k-hash)
6
+ [![Inline docs](https://inch-ci.org/github/valeth/digest-ed2k-hash.rb.svg?branch=master&style=shields)](https://inch-ci.org/github/valeth/digest-ed2k-hash.rb)
7
+ [![Code Climate](https://codeclimate.com/github/valeth/digest-ed2k-hash.rb/badges/gpa.svg)](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
 
@@ -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
- while (buf = io.read(CHUNK_SIZE))
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
- @buf += data
64
- hash
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
- # @param [String] str use this string to digest,
74
- # otherwise digest the current md4 hash
75
- def digest(str = nil)
76
- if str.nil?
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 << str
90
+ self << data
82
91
  digest
83
92
  end
84
93
  end
85
94
 
86
- # {include:#digest}
87
- def hexdigest(str = nil)
88
- if str.nil?
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 << str
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] str the string to digest
130
+ # @param [String, IO] data the string to digest
126
131
  # @return a finalized digest object
127
- def digest(str)
128
- new.digest(str)
132
+ def digest(data)
133
+ new.digest(data)
129
134
  end
130
135
 
131
136
  # Calculate the hexdigest of a string.
132
- # @param [String] str the string to digest
137
+ #
138
+ # @param [String, IO] data the string to digest
133
139
  # @return a finalized digest object
134
- def hexdigest(str)
135
- new.hexdigest(str)
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
- # @param [String] file the file to read
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 hash
156
- while @buf.size >= CHUNK_SIZE
157
- @small = false
158
- @md4 << MD4.digest(@buf.slice!(0...CHUNK_SIZE))
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
@@ -3,6 +3,6 @@
3
3
  module Digest
4
4
  class ED2K < Digest::Class
5
5
  # The current gem version.
6
- VERSION = '0.2.0'
6
+ VERSION = '0.3.0'
7
7
  end
8
8
  end
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.2.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-13 00:00:00.000000000 Z
11
+ date: 2017-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler