Class: Digest::ED2K

Inherits:
Class
  • Object
show all
Defined in:
lib/digest/ed2k.rb,
lib/digest/ed2k/version.rb

Overview

The ED2K hashing algorithm.

Constant Summary collapse

CHUNK_SIZE =

The ED2K chunk size (9500KB).

9_728_000
MD4 =

Shortcut to the OpenSSL MD4 class.

OpenSSL::Digest::MD4
VERSION =

The current gem version.

'0.3.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

Create a new ED2K hash object.


18
19
20
21
# File 'lib/digest/ed2k.rb', line 18

def initialize
    @md4 = MD4.new
    reset
end

Class Method Details

.digest(data) ⇒ Object

Calculate the digest of a string.


132
133
134
# File 'lib/digest/ed2k.rb', line 132

def digest(data)
    new.digest(data)
end

.file(path) ⇒ Object

Create a new digest object from a file.


156
157
158
# File 'lib/digest/ed2k.rb', line 156

def file(path)
    new.file(path)
end

.hexdigest(data) ⇒ Object

Calculate the hexdigest of a string.


140
141
142
# File 'lib/digest/ed2k.rb', line 140

def hexdigest(data)
    new.hexdigest(data)
end

.io(io) ⇒ Object

Create a new digest object from a IO object.


148
149
150
# File 'lib/digest/ed2k.rb', line 148

def io(io)
    new.io(io)
end

Instance Method Details

#digest(data = nil) ⇒ Object

Finalize the hash and return the digest.

If no string is provided, the current hash is used


84
85
86
87
88
89
90
91
92
93
# File 'lib/digest/ed2k.rb', line 84

def digest(data = nil)
    if data.nil?
        finish
        @md4.digest
    else
        reset
        self << data
        digest
    end
end

#file(path) ⇒ Object

Calculate the hash of a file.


35
36
37
# File 'lib/digest/ed2k.rb', line 35

def file(path)
    io(File.open(path))
end

#finishObject

Finalize the hash.


114
115
116
117
118
# File 'lib/digest/ed2k.rb', line 114

def finish
    @finalized = true unless @finalized

    self
end

#hexdigest(data = nil) ⇒ Object

Finalize the hash and return the hexdigest.

If no string is provided, the current hash is used


100
101
102
103
104
105
106
107
108
109
# File 'lib/digest/ed2k.rb', line 100

def hexdigest(data = nil)
    if data.nil?
        finish
        @md4.hexdigest
    else
        reset
        self << data
        hexdigest
    end
end

#inspectObject

Shows the current state and the digest class. If the digest is finalized, it shows the hexdigest.


122
123
124
125
# File 'lib/digest/ed2k.rb', line 122

def inspect
    dig = @finalized ? hexdigest : 'unfinalized'
    "#<#{self.class.name}: #{dig}>"
end

#io(io) ⇒ Object

Hash an IO object.


27
28
29
# File 'lib/digest/ed2k.rb', line 27

def io(io)
    self << io
end

#resetObject

Reset to the initial state.


42
43
44
45
46
47
# File 'lib/digest/ed2k.rb', line 42

def reset
    @md4.reset
    @finalized = false

    self
end

#update(data) ⇒ Object Also known as: <<

Rehash with new data.

Raises:

  • RuntimeError if the digest object has been finalized


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/digest/ed2k.rb', line 54

def update(data)
    raise RuntimeError if @finalized

    # get the IO object
    buf = to_io(data)

    # if the chunk is smaller than CHUNK_SIZE just return the MD4 hash
    if buf.size < CHUNK_SIZE
        @md4 << buf.read
    else
        # read chunks from the IO object and update the MD4 hash
        while (chunk = buf.read(CHUNK_SIZE))
            @md4 << MD4.digest(chunk)
        end

        # weird EDonkey bug requires multiples of CHUNK_SIZE
        # to append one additional MD4 hash
        @md4 << MD4.new.digest if multiple?(buf.size)
    end

    self
end