Module: Puppet::Util::Checksums

Extended by:
Checksums
Included in:
FileBucket::Dipper, FileBucketFile::File, FileServing::Metadata, Checksums
Defined in:
lib/puppet/util/checksums.rb

Overview

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

Defined Under Namespace

Classes: FakeChecksum

Instance Method Summary collapse

Instance Method Details

#checksum?(string) ⇒ Boolean

Is the provided string a checksum?

Returns:

  • (Boolean)


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

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

#ctime(content) ⇒ Object



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

def ctime(content)
  ""
end

#ctime_file(filename) ⇒ Object

Return the :ctime of a file.



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

def ctime_file(filename)
  Puppet::FileSystem.stat(filename).send(:ctime)
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.



14
15
16
17
# File 'lib/puppet/util/checksums.rb', line 14

def known_checksum_types
  [:sha256, :sha256lite, :md5, :md5lite, :sha1, :sha1lite,
    :mtime, :ctime, :none]
end

#md5(content) ⇒ Object

Calculate a checksum using Digest::MD5.



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

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.



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

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

#md5_hex_lengthObject Also known as: md5lite_hex_length



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

def md5_hex_length
  32
end

#md5_stream {|digest| ... } ⇒ Object Also known as: md5lite_stream

Yields:

  • (digest)


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

def md5_stream(&block)
  digest = Digest::MD5.new
  yield digest
  digest.hexdigest
end

#md5lite(content) ⇒ Object

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



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

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.



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

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

#mtime(content) ⇒ Object



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

def mtime(content)
  ""
end

#mtime_file(filename) ⇒ Object

Return the :mtime timestamp of a file.



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

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

#mtime_stream {|noop_digest| ... } ⇒ Object Also known as: ctime_stream

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

Yields:

  • (noop_digest)


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

def mtime_stream
  noop_digest = FakeChecksum.new
  yield noop_digest
  nil
end

#none(content) ⇒ Object



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

def none(content)
  ""
end

#none_file(filename) ⇒ Object

Return a “no checksum”



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

def none_file(filename)
  ""
end

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

Yields:

  • (noop_digest)


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

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

#sha1(content) ⇒ Object

Calculate a checksum using Digest::SHA1.



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

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.



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

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

#sha1_hex_lengthObject Also known as: sha1lite_hex_length



154
155
156
# File 'lib/puppet/util/checksums.rb', line 154

def sha1_hex_length
  40
end

#sha1_stream {|digest| ... } ⇒ Object Also known as: sha1lite_stream

Yields:

  • (digest)


148
149
150
151
152
# File 'lib/puppet/util/checksums.rb', line 148

def sha1_stream
  digest = Digest::SHA1.new
  yield digest
  digest.hexdigest
end

#sha1lite(content) ⇒ Object

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



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

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.



144
145
146
# File 'lib/puppet/util/checksums.rb', line 144

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

#sha256(content) ⇒ Object

Calculate a checksum using Digest::SHA256.



42
43
44
45
# File 'lib/puppet/util/checksums.rb', line 42

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

#sha256_file(filename, lite = false) ⇒ Object



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

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

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

#sha256_hex_lengthObject Also known as: sha256lite_hex_length



69
70
71
# File 'lib/puppet/util/checksums.rb', line 69

def sha256_hex_length
  64
end

#sha256_stream {|digest| ... } ⇒ Object Also known as: sha256lite_stream

Yields:

  • (digest)


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

def sha256_stream(&block)
  require 'digest/sha2'
  digest = Digest::SHA256.new
  yield digest
  digest.hexdigest
end

#sha256lite(content) ⇒ Object



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

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

#sha256lite_file(filename) ⇒ Object



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

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

#sumdata(checksum) ⇒ Object

Strip the checksum type from an existing checksum



32
33
34
# File 'lib/puppet/util/checksums.rb', line 32

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

#sumtype(checksum) ⇒ Object

Strip the checksum type from an existing checksum



37
38
39
# File 'lib/puppet/util/checksums.rb', line 37

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