2019-01-31 12:19:50 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-04-16 10:34:02 +02:00
|
|
|
# Accounting integrity verifications
|
|
|
|
module Integrity; end
|
|
|
|
|
|
|
|
# Provides checksums for archiving control
|
|
|
|
class Integrity::Checksum
|
2019-01-31 12:19:50 +01:00
|
|
|
class << self
|
|
|
|
def code
|
|
|
|
dir = Dir.pwd
|
|
|
|
|
2021-10-14 18:20:10 +02:00
|
|
|
files = if Rails.env.test?
|
|
|
|
# in test mode, we compute a "lite" checksum to speed-up test running time
|
|
|
|
children_files("#{dir}/*")
|
|
|
|
else
|
|
|
|
children_files("#{dir}/*")
|
|
|
|
.concat(children_files("#{dir}/app/**/*"))
|
|
|
|
.concat(children_files("#{dir}/bin/**/*"))
|
|
|
|
.concat(children_files("#{dir}/config/**/*"))
|
|
|
|
.concat(children_files("#{dir}/db/**/*"))
|
|
|
|
.concat(children_files("#{dir}/doc/**/*"))
|
|
|
|
.concat(children_files("#{dir}/docker/**/*"))
|
|
|
|
.concat(children_files("#{dir}/lib/**/*"))
|
|
|
|
.concat(children_files("#{dir}/node_modules/**/*"))
|
|
|
|
.concat(children_files("#{dir}/plugins/**/*"))
|
|
|
|
.concat(children_files("#{dir}/provision/**/*"))
|
|
|
|
.concat(children_files("#{dir}/scripts/**/*"))
|
|
|
|
.concat(children_files("#{dir}/test/**/*"))
|
|
|
|
.concat(children_files("#{dir}/vendor/**/*"))
|
|
|
|
end
|
2019-01-31 12:19:50 +01:00
|
|
|
|
|
|
|
content = files.map { |f| File.read(f) }.join
|
|
|
|
|
2021-04-26 10:12:18 +02:00
|
|
|
Integrity::Checksum.text(content)
|
2019-01-31 12:19:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def file(path)
|
|
|
|
return unless File.exist?(path)
|
|
|
|
|
|
|
|
content = File.read(path)
|
|
|
|
|
2021-04-26 10:12:18 +02:00
|
|
|
Integrity::Checksum.text(content)
|
2019-03-13 17:48:35 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def text(data)
|
2019-03-21 17:15:41 +01:00
|
|
|
require 'sha3'
|
|
|
|
|
|
|
|
SHA3::Digest.hexdigest(:sha256, data)
|
2019-01-31 12:19:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def children_files(path)
|
|
|
|
Dir[path].reject { |f| File.directory?(f) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|