Module: Puppet::Util::Checksums

Included in:
DataSync, FileBucket::Dipper, FileBucketFile::File, FileServing::Metadata, Resource::Catalog::Compiler
Defined in:
lib/puppet/util/checksums.rb

Overview

A stand-alone module for calculating checksums in a generic way.

Defined Under Namespace

Classes: DigestLite, FakeChecksum

Constant Summary collapse

KNOWN_CHECKSUMS =
[
  :sha256, :sha256lite,
  :md5, :md5lite,
  :sha1, :sha1lite,
  :sha512,
  :sha384,
  :sha224,
  :mtime, :ctime, :none
].freeze

Class Method Summary collapse

Class Method Details

.checksum?(string) ⇒ Boolean

Is the provided string a checksum?

Returns:

  • (Boolean)

39
40
41
42
# File 'lib/puppet/util/checksums.rb', line 39

def checksum?(string)
  # 'sha256lite'.length == 10
  string =~ /^\{(\w{3,10})\}\S+/
end

.checksum_file(digest, filename, lite = false) ⇒ Object

Perform an incremental checksum on a file.


359
360
361
362
363
364
365
366
367
368
369
# File 'lib/puppet/util/checksums.rb', line 359

def checksum_file(digest, filename, lite = false)
  buffer = lite ? 512 : 4096
  File.open(filename, 'rb') do |file|
    while content = file.read(buffer) #rubocop:disable Lint/AssignmentInCondition
      digest << content
      break if lite
    end
  end

  digest.hexdigest
end

.checksum_stream(digest, block, lite = false) ⇒ Object


371
372
373
374
# File 'lib/puppet/util/checksums.rb', line 371

def checksum_stream(digest, block, lite = false)
  block.call(DigestLite.new(digest, lite))
  digest.hexdigest
end

.ctime(content) ⇒ Object


298
299
300
# File 'lib/puppet/util/checksums.rb', line 298

def ctime(content)
  ""
end

.ctime?(string) ⇒ Boolean

Returns:

  • (Boolean)

302
303
304
305
306
307
# File 'lib/puppet/util/checksums.rb', line 302

def ctime?(string)
  return true if string.is_a? Time
  !!DateTime.parse(string)
rescue
  false
end

.ctime_file(filename) ⇒ Object

Return the :ctime of a file.


310
311
312
# File 'lib/puppet/util/checksums.rb', line 310

def ctime_file(filename)
  Puppet::FileSystem.stat(filename).ctime
end

.ctime_stream(&block) ⇒ Object


314
315
316
# File 'lib/puppet/util/checksums.rb', line 314

def ctime_stream(&block)
  mtime_stream(&block)
end

.known_checksum_typesObject

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.


22
23
24
# File 'lib/puppet/util/checksums.rb', line 22

def known_checksum_types
  KNOWN_CHECKSUMS
end

.md5(content) ⇒ Object

Calculate a checksum using Digest::MD5.


183
184
185
# File 'lib/puppet/util/checksums.rb', line 183

def md5(content)
  Digest::MD5.hexdigest(content)
end

.md5?(string) ⇒ Boolean

Returns:

  • (Boolean)

187
188
189
# File 'lib/puppet/util/checksums.rb', line 187

def md5?(string)
  string =~ /^\h{32}$/
end

.md5_file(filename, lite = false) ⇒ Object

Calculate a checksum of a file's content using Digest::MD5.


192
193
194
195
# File 'lib/puppet/util/checksums.rb', line 192

def md5_file(filename, lite = false)
  digest = Digest::MD5.new
  checksum_file(digest, filename,  lite)
end

.md5_hex_lengthObject


202
203
204
# File 'lib/puppet/util/checksums.rb', line 202

def md5_hex_length
  32
end

.md5_stream(lite = false, &block) ⇒ Object


197
198
199
200
# File 'lib/puppet/util/checksums.rb', line 197

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.


207
208
209
# File 'lib/puppet/util/checksums.rb', line 207

def md5lite(content)
  md5(content[0..511])
end

.md5lite?(string) ⇒ Boolean

Returns:

  • (Boolean)

211
212
213
# File 'lib/puppet/util/checksums.rb', line 211

def md5lite?(string)
  md5?(string)
end

.md5lite_file(filename) ⇒ Object

Calculate a checksum of the first 500 chars of a file's content using Digest::MD5.


216
217
218
# File 'lib/puppet/util/checksums.rb', line 216

def md5lite_file(filename)
  md5_file(filename, true)
end

.md5lite_hex_lengthObject


224
225
226
# File 'lib/puppet/util/checksums.rb', line 224

def md5lite_hex_length
  md5_hex_length
end

.md5lite_stream(&block) ⇒ Object


220
221
222
# File 'lib/puppet/util/checksums.rb', line 220

def md5lite_stream(&block)
  md5_stream(true, &block)
end

.mtime(content) ⇒ Object


228
229
230
# File 'lib/puppet/util/checksums.rb', line 228

def mtime(content)
  ""
end

.mtime?(string) ⇒ Boolean

Returns:

  • (Boolean)

232
233
234
235
236
237
# File 'lib/puppet/util/checksums.rb', line 232

def mtime?(string)
  return true if string.is_a? Time
  !!DateTime.parse(string)
rescue
  false
end

.mtime_file(filename) ⇒ Object

Return the :mtime timestamp of a file.


240
241
242
# File 'lib/puppet/util/checksums.rb', line 240

def mtime_file(filename)
  Puppet::FileSystem.stat(filename).mtime
end

.mtime_stream {|noop_digest| ... } ⇒ Object

by definition this doesn't exist but we still need to execute the block given

Yields:

  • (noop_digest)

246
247
248
249
250
# File 'lib/puppet/util/checksums.rb', line 246

def mtime_stream(&block)
  noop_digest = FakeChecksum.new
  yield noop_digest
  nil
end

.none(content) ⇒ Object


318
319
320
# File 'lib/puppet/util/checksums.rb', line 318

def none(content)
  ""
end

.none?(string) ⇒ Boolean

Returns:

  • (Boolean)

322
323
324
# File 'lib/puppet/util/checksums.rb', line 322

def none?(string)
  string.empty?
end

.none_file(filename) ⇒ Object

Return a “no checksum”


327
328
329
# File 'lib/puppet/util/checksums.rb', line 327

def none_file(filename)
  ""
end

.none_stream {|noop_digest| ... } ⇒ Object

Yields:

  • (noop_digest)

331
332
333
334
335
# File 'lib/puppet/util/checksums.rb', line 331

def none_stream
  noop_digest = FakeChecksum.new
  yield noop_digest
  ""
end

.sha1(content) ⇒ Object

Calculate a checksum using Digest::SHA1.


253
254
255
# File 'lib/puppet/util/checksums.rb', line 253

def sha1(content)
  Digest::SHA1.hexdigest(content)
end

.sha1?(string) ⇒ Boolean

Returns:

  • (Boolean)

257
258
259
# File 'lib/puppet/util/checksums.rb', line 257

def sha1?(string)
  string =~ /^\h{40}$/
end

.sha1_file(filename, lite = false) ⇒ Object

Calculate a checksum of a file's content using Digest::SHA1.


262
263
264
265
# File 'lib/puppet/util/checksums.rb', line 262

def sha1_file(filename, lite = false)
  digest = Digest::SHA1.new
  checksum_file(digest, filename, lite)
end

.sha1_hex_lengthObject


272
273
274
# File 'lib/puppet/util/checksums.rb', line 272

def sha1_hex_length
  40
end

.sha1_stream(lite = false, &block) ⇒ Object


267
268
269
270
# File 'lib/puppet/util/checksums.rb', line 267

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.


277
278
279
# File 'lib/puppet/util/checksums.rb', line 277

def sha1lite(content)
  sha1(content[0..511])
end

.sha1lite?(string) ⇒ Boolean

Returns:

  • (Boolean)

281
282
283
# File 'lib/puppet/util/checksums.rb', line 281

def sha1lite?(string)
  sha1?(string)
end

.sha1lite_file(filename) ⇒ Object

Calculate a checksum of the first 500 chars of a file's content using Digest::SHA1.


286
287
288
# File 'lib/puppet/util/checksums.rb', line 286

def sha1lite_file(filename)
  sha1_file(filename, true)
end

.sha1lite_hex_lengthObject


294
295
296
# File 'lib/puppet/util/checksums.rb', line 294

def sha1lite_hex_length
  sha1_hex_length
end

.sha1lite_stream(&block) ⇒ Object


290
291
292
# File 'lib/puppet/util/checksums.rb', line 290

def sha1lite_stream(&block)
  sha1_stream(true, &block)
end

.sha224(content) ⇒ Object

Calculate a checksum using Digest::SHA224.


156
157
158
159
# File 'lib/puppet/util/checksums.rb', line 156

def sha224(content)
  require 'puppet/ssl/openssl_loader'
  OpenSSL::Digest::SHA224.new.hexdigest(content)
end

.sha224?(string) ⇒ Boolean

Returns:

  • (Boolean)

161
162
163
# File 'lib/puppet/util/checksums.rb', line 161

def sha224?(string)
  string =~ /^\h{56}$/
end

.sha224_file(filename, lite = false) ⇒ Object


165
166
167
168
169
170
# File 'lib/puppet/util/checksums.rb', line 165

def sha224_file(filename, lite = false)
  require 'puppet/ssl/openssl_loader'

  digest = OpenSSL::Digest::SHA224.new
  checksum_file(digest, filename,  lite)
end

.sha224_hex_lengthObject


178
179
180
# File 'lib/puppet/util/checksums.rb', line 178

def sha224_hex_length
  56
end

.sha224_stream(lite = false, &block) ⇒ Object


172
173
174
175
176
# File 'lib/puppet/util/checksums.rb', line 172

def sha224_stream(lite = false, &block)
  require 'puppet/ssl/openssl_loader'
  digest = OpenSSL::Digest::SHA224.new
  checksum_stream(digest, block, lite)
end

.sha256(content) ⇒ Object

Calculate a checksum using Digest::SHA256.


55
56
57
58
# File 'lib/puppet/util/checksums.rb', line 55

def sha256(content)
  require 'digest/sha2'
  Digest::SHA256.hexdigest(content)
end

.sha256?(string) ⇒ Boolean

Returns:

  • (Boolean)

60
61
62
# File 'lib/puppet/util/checksums.rb', line 60

def sha256?(string)
  string =~ /^\h{64}$/
end

.sha256_file(filename, lite = false) ⇒ Object


64
65
66
67
68
69
# File 'lib/puppet/util/checksums.rb', line 64

def sha256_file(filename, lite = false)
  require 'digest/sha2'

  digest = Digest::SHA256.new
  checksum_file(digest, filename,  lite)
end

.sha256_hex_lengthObject


77
78
79
# File 'lib/puppet/util/checksums.rb', line 77

def sha256_hex_length
  64
end

.sha256_stream(lite = false, &block) ⇒ Object


71
72
73
74
75
# File 'lib/puppet/util/checksums.rb', line 71

def sha256_stream(lite = false, &block)
  require 'digest/sha2'
  digest = Digest::SHA256.new
  checksum_stream(digest, block, lite)
end

.sha256lite(content) ⇒ Object


81
82
83
# File 'lib/puppet/util/checksums.rb', line 81

def sha256lite(content)
  sha256(content[0..511])
end

.sha256lite?(string) ⇒ Boolean

Returns:

  • (Boolean)

85
86
87
# File 'lib/puppet/util/checksums.rb', line 85

def sha256lite?(string)
  sha256?(string)
end

.sha256lite_file(filename) ⇒ Object


89
90
91
# File 'lib/puppet/util/checksums.rb', line 89

def sha256lite_file(filename)
  sha256_file(filename, true)
end

.sha256lite_hex_lengthObject


97
98
99
# File 'lib/puppet/util/checksums.rb', line 97

def sha256lite_hex_length
  sha256_hex_length
end

.sha256lite_stream(&block) ⇒ Object


93
94
95
# File 'lib/puppet/util/checksums.rb', line 93

def sha256lite_stream(&block)
  sha256_stream(true, &block)
end

.sha384(content) ⇒ Object

Calculate a checksum using Digest::SHA384.


102
103
104
105
# File 'lib/puppet/util/checksums.rb', line 102

def sha384(content)
  require 'digest/sha2'
  Digest::SHA384.hexdigest(content)
end

.sha384?(string) ⇒ Boolean

Returns:

  • (Boolean)

107
108
109
# File 'lib/puppet/util/checksums.rb', line 107

def sha384?(string)
  string =~ /^\h{96}$/
end

.sha384_file(filename, lite = false) ⇒ Object


111
112
113
114
115
116
# File 'lib/puppet/util/checksums.rb', line 111

def sha384_file(filename, lite = false)
  require 'digest/sha2'

  digest = Digest::SHA384.new
  checksum_file(digest, filename,  lite)
end

.sha384_hex_lengthObject


124
125
126
# File 'lib/puppet/util/checksums.rb', line 124

def sha384_hex_length
  96
end

.sha384_stream(lite = false, &block) ⇒ Object


118
119
120
121
122
# File 'lib/puppet/util/checksums.rb', line 118

def sha384_stream(lite = false, &block)
  require 'digest/sha2'
  digest = Digest::SHA384.new
  checksum_stream(digest, block, lite)
end

.sha512(content) ⇒ Object

Calculate a checksum using Digest::SHA512.


129
130
131
132
# File 'lib/puppet/util/checksums.rb', line 129

def sha512(content)
  require 'digest/sha2'
  Digest::SHA512.hexdigest(content)
end

.sha512?(string) ⇒ Boolean

Returns:

  • (Boolean)

134
135
136
# File 'lib/puppet/util/checksums.rb', line 134

def sha512?(string)
  string =~ /^\h{128}$/
end

.sha512_file(filename, lite = false) ⇒ Object


138
139
140
141
142
143
# File 'lib/puppet/util/checksums.rb', line 138

def sha512_file(filename, lite = false)
  require 'digest/sha2'

  digest = Digest::SHA512.new
  checksum_file(digest, filename,  lite)
end

.sha512_hex_lengthObject


151
152
153
# File 'lib/puppet/util/checksums.rb', line 151

def sha512_hex_length
  128
end

.sha512_stream(lite = false, &block) ⇒ Object


145
146
147
148
149
# File 'lib/puppet/util/checksums.rb', line 145

def sha512_stream(lite = false, &block)
  require 'digest/sha2'
  digest = Digest::SHA512.new
  checksum_stream(digest, block, lite)
end

.sumdata(checksum) ⇒ Object

Strip the checksum type from an existing checksum


45
46
47
# File 'lib/puppet/util/checksums.rb', line 45

def sumdata(checksum)
  checksum =~ /^\{(\w+)\}(.+)/ ? $2 : nil
end

.sumtype(checksum) ⇒ Object

Strip the checksum type from an existing checksum


50
51
52
# File 'lib/puppet/util/checksums.rb', line 50

def sumtype(checksum)
  checksum =~ /^\{(\w+)\}/ ? $1 : nil
end

.valid_checksum?(type, value) ⇒ Boolean

Returns:

  • (Boolean)

26
27
28
29
30
# File 'lib/puppet/util/checksums.rb', line 26

def valid_checksum?(type, value)
  !!send("#{type}?", value)
rescue NoMethodError
  false
end