GwFileValidatorsPattern¶
Creating a hash¶
For each file a hash value can be created.
GwFileValidatorsPattern
cares about the correct handling of files, even if the file size is too big to get handled in one step.
To create a hash, all you have to do is to use the function
hash()
:
from groundwork_validation.patterns import GwFileValidatorsPattern
class My_Plugin(GwFileValidatorsPattern):
def __init__(self, app, **kwargs):
self.name = "My_Plugin"
super(My_Plugin, self).__init__(app, **kwargs)
def activate(self):
my_file = "/path/to/file.txt"
# Generate and retrieve a string based hash value
my_hash = self.validators.file.hash(my_file)
# Store hash value directly into a file
my_hash = self.validators.file.hash(my_file, hash_file = "/path/to/file.txt.hash")
def deactivate(self):
pass
Please see
hash()
for a complete list of available parameters.
Validate a file¶
Using a hash string¶
For validation the function
validate()
is available:
from groundwork_validation.patterns import GwFileValidatorsPattern
class My_Plugin(GwFileValidatorsPattern):
def __init__(self, app, **kwargs):
self.name = "My_Plugin"
super(My_Plugin, self).__init__(app, **kwargs)
def activate(self):
my_file = "/path/to/file.txt"
my_hash = self.validators.file.hash(my_file) # Generate a hash
if self.validators.file.validate(my_file, my_hash):
print("Hash is valid")
else:
print("Hash is NOT valid")
def deactivate(self):
pass
Using a hash file¶
It is also possible to validate a file against a hash file, which has stored the hash at the first line:
from groundwork_validation.patterns import GwFileValidatorsPattern
class My_Plugin(GwFileValidatorsPattern):
def __init__(self, app, **kwargs):
self.name = "My_Plugin"
super(My_Plugin, self).__init__(app, **kwargs)
def activate(self):
my_file = "/path/to/file.txt"
my_hash_file = "/path/to/file.hash"
if self.validators.file.validate(my_file, hash_file=my_hash_file):
print("Hash is valid")
else:
print("Hash is NOT valid")
def deactivate(self):
pass
Please see
validate()
for a complete list of available parameters.
Requirements & Specifications¶
The following sections describes the implemented requirements and their related specifications.
Available requirements
ID | Title | Type | Status | Links | Tags |
---|---|---|---|---|---|
R_8E18E | File validation | Requirement | gwfilevalidators |
Available specifications
ID | Title | Type | Status | Links | Tags |
---|---|---|---|---|---|
S_31C31 | Validating a file | Specification | R_8E18E | gwfilevalidators | |
S_CFBC1 | Hashing a file | Specification | R_8E18E | gwfilevalidators |
Requirements¶
Requirement: File validation (R_8E18E)
As developer I want to be able to easily hash and validate files to detect every kind of file corruption.
tags: gwfilevalidators
Specifications¶
Specification: Hashing a file (S_CFBC1)
A function
hash
is implemented forself.validators.file
, which is able to create a hash value for a given file path. The function must have the following parameters:
- file - file path
- validator - An instance of
Validator
. Can be None- hash_file - File to store the hash value. optional
- blocksize - Max. size of a block, which gets read in gets hashed and maybe update the prior hash value.
- return_hash_object - Returns the hashlib hash object instead of a string representation
tags: gwfilevalidators
links: R_8E18E
Specification: Validating a file (S_31C31)
A function
validate
is implemented forself.validators.file
, which allows the validation of a file against a given hash.The function has the following attributes:
- file - file path
- hash_value
- hash_file - if given, hash_value is read from this file path
- validator - An instance of
Validator
. Can be None- blocksize - Max. size of a block, which gets read in gets hashed and maybe update the prior hash value.
Returns True, if calculated hash values is euqal to the given hash value.
tags: gwfilevalidators
links: R_8E18E