Module: Puppet::Util::Checksums Private

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

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

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

Defined Under Namespace

Classes: DigestLite, FakeChecksum

Constant Summary collapse

KNOWN_CHECKSUMS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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

Class Method Summary collapse

Class Method Details

.checksum?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Is the provided string a checksum?

Returns:

  • (Boolean)


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

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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Perform an incremental checksum on a file.



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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.ctime(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def ctime(content)
  ""
end

.ctime?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

.ctime_file(filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the :ctime of a file.



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

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

.ctime_stream(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.known_checksum_typesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



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

def known_checksum_types
  KNOWN_CHECKSUMS
end

.md5(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculate a checksum using Digest::MD5.



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

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

.md5?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

.md5_file(filename, lite = false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



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

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

.md5_hex_lengthObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def md5_hex_length
  32
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def md5_stream(lite = false, &block)
  digest = Digest::MD5.new
  checksum_stream(digest, block, lite)
end

.md5lite(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



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

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

.md5lite?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

.md5lite_file(filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



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

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

.md5lite_hex_lengthObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def md5lite_hex_length
  md5_hex_length
end

.md5lite_stream(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.mtime(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def mtime(content)
  ""
end

.mtime?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

.mtime_file(filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the :mtime timestamp of a file.



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

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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Yields:

  • (noop_digest)


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

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

.none(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def none(content)
  ""
end

.none?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

def none?(string)
  string.empty?
end

.none_file(filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a “no checksum”



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

def none_file(filename)
  ""
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yields:

  • (noop_digest)


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

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

.sha1(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculate a checksum using Digest::SHA1.



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

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

.sha1?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

.sha1_file(filename, lite = false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



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

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

.sha1_hex_lengthObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def sha1_hex_length
  40
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def sha1_stream(lite = false, &block)
  digest = Digest::SHA1.new
  checksum_stream(digest, block, lite)
end

.sha1lite(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



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

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

.sha1lite?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

.sha1lite_file(filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



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

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

.sha1lite_hex_lengthObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def sha1lite_hex_length
  sha1_hex_length
end

.sha1lite_stream(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.sha224(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculate a checksum using Digest::SHA224.



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

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

.sha224?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

.sha224_file(filename, lite = false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

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

.sha224_hex_lengthObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def sha224_hex_length
  56
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.sha256(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculate a checksum using Digest::SHA256.



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

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

.sha256?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

.sha256_file(filename, lite = false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

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

.sha256_hex_lengthObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def sha256_hex_length
  64
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.sha256lite(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.sha256lite?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

.sha256lite_file(filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.sha256lite_hex_lengthObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def sha256lite_hex_length
  sha256_hex_length
end

.sha256lite_stream(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.sha384(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculate a checksum using Digest::SHA384.



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

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

.sha384?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

.sha384_file(filename, lite = false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

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

.sha384_hex_lengthObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def sha384_hex_length
  96
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.sha512(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculate a checksum using Digest::SHA512.



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

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

.sha512?(string) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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

.sha512_file(filename, lite = false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

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

.sha512_hex_lengthObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def sha512_hex_length
  128
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.sumdata(checksum) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Strip the checksum type from an existing checksum



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

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

.sumtype(checksum) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Strip the checksum type from an existing checksum



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

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

.valid_checksum?(type, value) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

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