mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Set the Python file maximum line length to 88 characters (#2122)
* flake8 --max-line-length=88
* fixup! Format Python code with psf/black push
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
"""
|
||||
Adler-32 is a checksum algorithm which was invented by Mark Adler in 1995.
|
||||
Compared to a cyclic redundancy check of the same length, it trades reliability for speed (preferring the latter).
|
||||
Adler-32 is more reliable than Fletcher-16, and slightly less reliable than Fletcher-32.[2]
|
||||
Compared to a cyclic redundancy check of the same length, it trades reliability for
|
||||
speed (preferring the latter).
|
||||
Adler-32 is more reliable than Fletcher-16, and slightly less reliable than
|
||||
Fletcher-32.[2]
|
||||
|
||||
source: https://en.wikipedia.org/wiki/Adler-32
|
||||
"""
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
"""
|
||||
This algorithm was created for sdbm (a public-domain reimplementation of ndbm) database library.
|
||||
It was found to do well in scrambling bits, causing better distribution of the keys and fewer splits.
|
||||
This algorithm was created for sdbm (a public-domain reimplementation of ndbm)
|
||||
database library.
|
||||
It was found to do well in scrambling bits, causing better distribution of the keys
|
||||
and fewer splits.
|
||||
It also happens to be a good general hashing function with good distribution.
|
||||
The actual function (pseudo code) is:
|
||||
for i in i..len(str):
|
||||
hash(i) = hash(i - 1) * 65599 + str[i];
|
||||
|
||||
What is included below is the faster version used in gawk. [there is even a faster, duff-device version]
|
||||
The magic constant 65599 was picked out of thin air while experimenting with different constants.
|
||||
What is included below is the faster version used in gawk. [there is even a faster,
|
||||
duff-device version]
|
||||
The magic constant 65599 was picked out of thin air while experimenting with
|
||||
different constants.
|
||||
It turns out to be a prime.
|
||||
This is one of the algorithms used in berkeley db (see sleepycat) and elsewhere.
|
||||
|
||||
@@ -18,7 +22,8 @@
|
||||
def sdbm(plain_text: str) -> str:
|
||||
"""
|
||||
Function implements sdbm hash, easy to use, great for bits scrambling.
|
||||
iterates over each character in the given string and applies function to each of them.
|
||||
iterates over each character in the given string and applies function to each of
|
||||
them.
|
||||
|
||||
>>> sdbm('Algorithms')
|
||||
1462174910723540325254304520539387479031000036
|
||||
|
||||
@@ -3,7 +3,8 @@ Demonstrates implementation of SHA1 Hash function in a Python class and gives ut
|
||||
to find hash of string or hash of text from a file.
|
||||
Usage: python sha1.py --string "Hello World!!"
|
||||
python sha1.py --file "hello_world.txt"
|
||||
When run without any arguments, it prints the hash of the string "Hello World!! Welcome to Cryptography"
|
||||
When run without any arguments, it prints the hash of the string "Hello World!!
|
||||
Welcome to Cryptography"
|
||||
Also contains a Test class to verify that the generated Hash is same as that
|
||||
returned by the hashlib library
|
||||
|
||||
@@ -39,7 +40,8 @@ class SHA1Hash:
|
||||
def __init__(self, data):
|
||||
"""
|
||||
Inititates the variables data and h. h is a list of 5 8-digit Hexadecimal
|
||||
numbers corresponding to (1732584193, 4023233417, 2562383102, 271733878, 3285377520)
|
||||
numbers corresponding to
|
||||
(1732584193, 4023233417, 2562383102, 271733878, 3285377520)
|
||||
respectively. We will start with this as a message digest. 0x is how you write
|
||||
Hexadecimal numbers in Python
|
||||
"""
|
||||
@@ -74,8 +76,8 @@ class SHA1Hash:
|
||||
# @staticmethod
|
||||
def expand_block(self, block):
|
||||
"""
|
||||
Takes a bytestring-block of length 64, unpacks it to a list of integers and returns a
|
||||
list of 80 integers after some bit operations
|
||||
Takes a bytestring-block of length 64, unpacks it to a list of integers and
|
||||
returns a list of 80 integers after some bit operations
|
||||
"""
|
||||
w = list(struct.unpack(">16L", block)) + [0] * 64
|
||||
for i in range(16, 80):
|
||||
@@ -84,12 +86,13 @@ class SHA1Hash:
|
||||
|
||||
def final_hash(self):
|
||||
"""
|
||||
Calls all the other methods to process the input. Pads the data, then splits into
|
||||
blocks and then does a series of operations for each block (including expansion).
|
||||
Calls all the other methods to process the input. Pads the data, then splits
|
||||
into blocks and then does a series of operations for each block (including
|
||||
expansion).
|
||||
For each block, the variable h that was initialized is copied to a,b,c,d,e
|
||||
and these 5 variables a,b,c,d,e undergo several changes. After all the blocks are
|
||||
processed, these 5 variables are pairwise added to h ie a to h[0], b to h[1] and so on.
|
||||
This h becomes our final hash which is returned.
|
||||
and these 5 variables a,b,c,d,e undergo several changes. After all the blocks
|
||||
are processed, these 5 variables are pairwise added to h ie a to h[0], b to h[1]
|
||||
and so on. This h becomes our final hash which is returned.
|
||||
"""
|
||||
self.padded_data = self.padding()
|
||||
self.blocks = self.split_blocks()
|
||||
@@ -138,8 +141,8 @@ class SHA1HashTest(unittest.TestCase):
|
||||
|
||||
def main():
|
||||
"""
|
||||
Provides option 'string' or 'file' to take input and prints the calculated SHA1 hash.
|
||||
unittest.main() has been commented because we probably don't want to run
|
||||
Provides option 'string' or 'file' to take input and prints the calculated SHA1
|
||||
hash. unittest.main() has been commented because we probably don't want to run
|
||||
the test each time.
|
||||
"""
|
||||
# unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user