Module: Puppet::Util::Checksums
Overview
A stand-alone module for calculating checksums in a generic way.
Defined Under Namespace
Classes: DigestLite, FakeChecksum
Class Method Summary
collapse
-
.checksum?(string) ⇒ Boolean
Is the provided string a checksum?.
-
.checksum_file(digest, filename, lite = false) ⇒ Object
Perform an incremental checksum on a file.
-
.checksum_stream(digest, block, lite = false) ⇒ Object
-
.ctime(content) ⇒ Object
-
.ctime_file(filename) ⇒ Object
Return the :ctime of a file.
-
.ctime_stream(&block) ⇒ Object
-
.known_checksum_types ⇒ Object
It’s not a good idea to use some of these in some contexts: for example, I wouldn’t try bucketing a file using the :none checksum type.
-
.md5(content) ⇒ Object
Calculate a checksum using Digest::MD5.
-
.md5_file(filename, lite = false) ⇒ Object
Calculate a checksum of a file’s content using Digest::MD5.
-
.md5_hex_length ⇒ Object
-
.md5_stream(lite = false, &block) ⇒ Object
-
.md5lite(content) ⇒ Object
Calculate a checksum of the first 500 chars of the content using Digest::MD5.
-
.md5lite_file(filename) ⇒ Object
Calculate a checksum of the first 500 chars of a file’s content using Digest::MD5.
-
.md5lite_hex_length ⇒ Object
-
.md5lite_stream(&block) ⇒ Object
-
.mtime(content) ⇒ Object
-
.mtime_file(filename) ⇒ Object
Return the :mtime timestamp of a file.
-
.mtime_stream {|noop_digest| ... } ⇒ Object
by definition this doesn’t exist but we still need to execute the block given.
-
.none(content) ⇒ Object
-
.none_file(filename) ⇒ Object
-
.none_stream {|noop_digest| ... } ⇒ Object
-
.sha1(content) ⇒ Object
Calculate a checksum using Digest::SHA1.
-
.sha1_file(filename, lite = false) ⇒ Object
Calculate a checksum of a file’s content using Digest::SHA1.
-
.sha1_hex_length ⇒ Object
-
.sha1_stream(lite = false, &block) ⇒ Object
-
.sha1lite(content) ⇒ Object
Calculate a checksum of the first 500 chars of the content using Digest::SHA1.
-
.sha1lite_file(filename) ⇒ Object
Calculate a checksum of the first 500 chars of a file’s content using Digest::SHA1.
-
.sha1lite_hex_length ⇒ Object
-
.sha1lite_stream(&block) ⇒ Object
-
.sha256(content) ⇒ Object
Calculate a checksum using Digest::SHA256.
-
.sha256_file(filename, lite = false) ⇒ Object
-
.sha256_hex_length ⇒ Object
-
.sha256_stream(lite = false, &block) ⇒ Object
-
.sha256lite(content) ⇒ Object
-
.sha256lite_file(filename) ⇒ Object
-
.sha256lite_hex_length ⇒ Object
-
.sha256lite_stream(&block) ⇒ Object
-
.sumdata(checksum) ⇒ Object
Strip the checksum type from an existing checksum.
-
.sumtype(checksum) ⇒ Object
Strip the checksum type from an existing checksum.
Class Method Details
.checksum?(string) ⇒ Boolean
Is the provided string a checksum?
23
24
25
26
|
# File 'lib/puppet/util/checksums.rb', line 23
def checksum?(string)
string =~ /^\{(\w{3,10})\}\S+/
end
|
.checksum_file(digest, filename, lite = false) ⇒ Object
Perform an incremental checksum on a file.
222
223
224
225
226
227
228
229
230
231
232
|
# File 'lib/puppet/util/checksums.rb', line 222
def checksum_file(digest, filename, lite = false)
buffer = lite ? 512 : 4096
File.open(filename, 'rb') do |file|
while content = file.read(buffer)
digest << content
break if lite
end
end
digest.hexdigest
end
|
.checksum_stream(digest, block, lite = false) ⇒ Object
234
235
236
237
|
# File 'lib/puppet/util/checksums.rb', line 234
def checksum_stream(digest, block, lite = false)
block.call(DigestLite.new(digest, lite))
digest.hexdigest
end
|
.ctime(content) ⇒ Object
179
180
181
|
# File 'lib/puppet/util/checksums.rb', line 179
def ctime(content)
""
end
|
.ctime_file(filename) ⇒ Object
Return the :ctime of a file.
171
172
173
|
# File 'lib/puppet/util/checksums.rb', line 171
def ctime_file(filename)
Puppet::FileSystem.stat(filename).send(:ctime)
end
|
.ctime_stream(&block) ⇒ Object
175
176
177
|
# File 'lib/puppet/util/checksums.rb', line 175
def ctime_stream(&block)
mtime_stream(&block)
end
|
.known_checksum_types ⇒ Object
It’s not a good idea to use some of these in some contexts: for example, I wouldn’t try bucketing a file using the :none checksum type.
11
12
13
14
|
# File 'lib/puppet/util/checksums.rb', line 11
def known_checksum_types
[:sha256, :sha256lite, :md5, :md5lite, :sha1, :sha1lite,
:mtime, :ctime, :none]
end
|
.md5(content) ⇒ Object
Calculate a checksum using Digest::MD5.
78
79
80
|
# File 'lib/puppet/util/checksums.rb', line 78
def md5(content)
Digest::MD5.hexdigest(content)
end
|
.md5_file(filename, lite = false) ⇒ Object
Calculate a checksum of a file’s content using Digest::MD5.
88
89
90
91
|
# File 'lib/puppet/util/checksums.rb', line 88
def md5_file(filename, lite = false)
digest = Digest::MD5.new
checksum_file(digest, filename, lite)
end
|
.md5_hex_length ⇒ Object
103
104
105
|
# File 'lib/puppet/util/checksums.rb', line 103
def md5_hex_length
32
end
|
.md5_stream(lite = false, &block) ⇒ Object
98
99
100
101
|
# File 'lib/puppet/util/checksums.rb', line 98
def md5_stream(lite = false, &block)
digest = Digest::MD5.new
checksum_stream(digest, block, lite)
end
|
.md5lite(content) ⇒ Object
Calculate a checksum of the first 500 chars of the content using Digest::MD5.
83
84
85
|
# File 'lib/puppet/util/checksums.rb', line 83
def md5lite(content)
md5(content[0..511])
end
|
.md5lite_file(filename) ⇒ Object
Calculate a checksum of the first 500 chars of a file’s content using Digest::MD5.
94
95
96
|
# File 'lib/puppet/util/checksums.rb', line 94
def md5lite_file(filename)
md5_file(filename, true)
end
|
.md5lite_hex_length ⇒ Object
111
112
113
|
# File 'lib/puppet/util/checksums.rb', line 111
def md5lite_hex_length
md5_hex_length
end
|
.md5lite_stream(&block) ⇒ Object
107
108
109
|
# File 'lib/puppet/util/checksums.rb', line 107
def md5lite_stream(&block)
md5_stream(true, &block)
end
|
.mtime(content) ⇒ Object
128
129
130
|
# File 'lib/puppet/util/checksums.rb', line 128
def mtime(content)
""
end
|
.mtime_file(filename) ⇒ Object
Return the :mtime timestamp of a file.
116
117
118
|
# File 'lib/puppet/util/checksums.rb', line 116
def mtime_file(filename)
Puppet::FileSystem.stat(filename).send(:mtime)
end
|
.mtime_stream {|noop_digest| ... } ⇒ Object
by definition this doesn’t exist but we still need to execute the block given
122
123
124
125
126
|
# File 'lib/puppet/util/checksums.rb', line 122
def mtime_stream(&block)
noop_digest = FakeChecksum.new
yield noop_digest
nil
end
|
.none(content) ⇒ Object
194
195
196
|
# File 'lib/puppet/util/checksums.rb', line 194
def none(content)
""
end
|
.none_file(filename) ⇒ Object
184
185
186
|
# File 'lib/puppet/util/checksums.rb', line 184
def none_file(filename)
""
end
|
.none_stream {|noop_digest| ... } ⇒ Object
188
189
190
191
192
|
# File 'lib/puppet/util/checksums.rb', line 188
def none_stream
noop_digest = FakeChecksum.new
yield noop_digest
""
end
|
.sha1(content) ⇒ Object
Calculate a checksum using Digest::SHA1.
133
134
135
|
# File 'lib/puppet/util/checksums.rb', line 133
def sha1(content)
Digest::SHA1.hexdigest(content)
end
|
.sha1_file(filename, lite = false) ⇒ Object
Calculate a checksum of a file’s content using Digest::SHA1.
143
144
145
146
|
# File 'lib/puppet/util/checksums.rb', line 143
def sha1_file(filename, lite = false)
digest = Digest::SHA1.new
checksum_file(digest, filename, lite)
end
|
.sha1_hex_length ⇒ Object
158
159
160
|
# File 'lib/puppet/util/checksums.rb', line 158
def sha1_hex_length
40
end
|
.sha1_stream(lite = false, &block) ⇒ Object
153
154
155
156
|
# File 'lib/puppet/util/checksums.rb', line 153
def sha1_stream(lite = false, &block)
digest = Digest::SHA1.new
checksum_stream(digest, block, lite)
end
|
.sha1lite(content) ⇒ Object
Calculate a checksum of the first 500 chars of the content using Digest::SHA1.
138
139
140
|
# File 'lib/puppet/util/checksums.rb', line 138
def sha1lite(content)
sha1(content[0..511])
end
|
.sha1lite_file(filename) ⇒ Object
Calculate a checksum of the first 500 chars of a file’s content using Digest::SHA1.
149
150
151
|
# File 'lib/puppet/util/checksums.rb', line 149
def sha1lite_file(filename)
sha1_file(filename, true)
end
|
.sha1lite_hex_length ⇒ Object
166
167
168
|
# File 'lib/puppet/util/checksums.rb', line 166
def sha1lite_hex_length
sha1_hex_length
end
|
.sha1lite_stream(&block) ⇒ Object
162
163
164
|
# File 'lib/puppet/util/checksums.rb', line 162
def sha1lite_stream(&block)
sha1_stream(true, &block)
end
|
.sha256(content) ⇒ Object
Calculate a checksum using Digest::SHA256.
39
40
41
42
|
# File 'lib/puppet/util/checksums.rb', line 39
def sha256(content)
require 'digest/sha2'
Digest::SHA256.hexdigest(content)
end
|
.sha256_file(filename, lite = false) ⇒ Object
48
49
50
51
52
53
|
# File 'lib/puppet/util/checksums.rb', line 48
def sha256_file(filename, lite = false)
require 'digest/sha2'
digest = Digest::SHA256.new
checksum_file(digest, filename, lite)
end
|
.sha256_hex_length ⇒ Object
65
66
67
|
# File 'lib/puppet/util/checksums.rb', line 65
def sha256_hex_length
64
end
|
.sha256_stream(lite = false, &block) ⇒ Object
59
60
61
62
63
|
# File 'lib/puppet/util/checksums.rb', line 59
def sha256_stream(lite = false, &block)
require 'digest/sha2'
digest = Digest::SHA256.new
checksum_stream(digest, block, lite)
end
|
.sha256lite(content) ⇒ Object
44
45
46
|
# File 'lib/puppet/util/checksums.rb', line 44
def sha256lite(content)
sha256(content[0..511])
end
|
.sha256lite_file(filename) ⇒ Object
55
56
57
|
# File 'lib/puppet/util/checksums.rb', line 55
def sha256lite_file(filename)
sha256_file(filename, true)
end
|
.sha256lite_hex_length ⇒ Object
73
74
75
|
# File 'lib/puppet/util/checksums.rb', line 73
def sha256lite_hex_length
sha256_hex_length
end
|
.sha256lite_stream(&block) ⇒ Object
69
70
71
|
# File 'lib/puppet/util/checksums.rb', line 69
def sha256lite_stream(&block)
sha256_stream(true, &block)
end
|
.sumdata(checksum) ⇒ Object
Strip the checksum type from an existing checksum
29
30
31
|
# File 'lib/puppet/util/checksums.rb', line 29
def sumdata(checksum)
checksum =~ /^\{(\w+)\}(.+)/ ? $2 : nil
end
|
.sumtype(checksum) ⇒ Object
Strip the checksum type from an existing checksum
34
35
36
|
# File 'lib/puppet/util/checksums.rb', line 34
def sumtype(checksum)
checksum =~ /^\{(\w+)\}/ ? $1 : nil
end
|