[Autogenerated] to get a better understanding of this.
Let's consider the following demo.
First, I will introduce hash slip for you to get familiar with,
then we'll build some functions to generate fall hashes.
Using the popular algorithms such as MD five, Sha one and sha 2 56.
And finally we'll generate import hash is by using the P file module to
compare suspicious files with different hash values.
We'll kick off this demo by first going over the hash lee module.
This implements a lot of the important hashing algorithms
that will use to generate harsh values based on the contents
of the falls and scrolling down.
There are even a couple of examples on the capabilities of this module.
In the meantime, I want to implement this in our script.
Very simple example, is a function to generate the MD five hash of a file.
I will call this, get MD five and have the path to the file as the argument.
I'll create a new object called MD five.
And also really important aspect that I almost missed is to
import hash lab returning to the function,
I will open the file in a with statement Since we might be dealing
with large files and memory might become an issue.
I will read this in chunks of 4096 bytes and in a wild loop I will use the
condition for the chunk size to be greater than zero.
I will update the MD five object with the value of the chunk
And also to make sure to read the next 4096 bytes from the
file into the Chunk variable.
This loop will continue until the end of the fall and the last
statement is a return of the hex digest.
This will be the string representation of the hash value,
jumping to the main part of the script.
I will make use of the for loop for each individual
file to generate the hash value.
To print this in a nice form, I will use format strings first,
I'll create a variable called F.
S and this will have to place holders.
The first one will put a string in a placeholder of 15
characters aligned to the right.
Then I will use the print function and use fs dot format and provide the file
name as the first argument and the result returned by the get MD five
function for the second Running this will provide the list of all the files
and their corresponding MD five hash value.
And what I notice is that the alignment for the README file is not right.
I can correct this by allocating 20 characters for the first placeholder
and running the script again is displayed much nicer now creating nearly
identical functions for each of the hashing algorithms while doable can
make the script harder to maintain.
So I'll create another function that generates not only the MD
five but also the san juan and shot 2 56 values and this will
be called get hashes fast forward,
I have already written the function and while it's
very similar to the first one,
the only important difference is at the end where rather than a single value,
a dictionary is returned and the hash algorithm is
used as a key with this in place.
It's time to create a false summary feature in our script.
I will start by adding a new argument.
Dash S will be the short name.
Dash dash fall summary is the long name.
Will specify the description in the health field and also I use
the action equals store through This means that S can be used by
itself without any additional value running the script and
displaying the help section.
We can notice the new argument.
Now let's return and implement this in the main portion of the script.
I will use an if statement to verify if the false summary
argument has been specified in the command line.
If so,
I will iterate through all of the files And for each file I'll
print a new line character and then the file name.
Then we'll specify a format string that can be used by each of the fall details.
The first piece of information is the fall type and this
can be retrieved using the magic module.
Next hour,
generate all of the hashes using the function I've written previously
and display all of the values and the corresponding algorithms to make
sure the information is easy to read.
I will add another line break at the end as well.
Now I can run the script using the dash s argument and we can see
it generates a nice report for multiple falls but this can also be
executed for a single file as well.
All of this is great but there is one more aspect that we need to cover and
this is the import hash This can be achieved using the peaceful module
while we'll cover this in much greater detail.
Later on in the course I will install this first using pip.
I will also create a separate function called get impact to keep
the naming convention standard since the files that we might be
dealing with might not be portable.
executable I will use a try except statement,
create a new p object and call the method get in passion in
case this generates an exception by not being API foul
function will return an error.
Finally,
we can integrate this into our file summary feature and to
test the south will call the function one more time and it
works to make things more interesting.
Let's run the script again and provide the entire folder and we start to
notice interesting things such as even though fall three and fall to have
different hash values they have the same import hash which might flag the
fact that they might be very similar.
You can also notice that some of the falls a narrower is displayed
instead still this is very interesting and we're starting to get
some real insight using the script,
but there is still much more to discover and build.