/ examples / hashlib_simpletest.py
hashlib_simpletest.py
 1  # pylint: disable=no-member, line-too-long
 2  import adafruit_hashlib as hashlib
 3  
 4  # Bytes-to-encode
 5  byte_string = b"CircuitPython"
 6  
 7  # Create a SHA-1 message
 8  print("--SHA1--")
 9  m = hashlib.sha1()
10  # Update the hash object with byte_string
11  m.update(byte_string)
12  # Obtain the digest, digest size, and block size
13  print(
14      "Msg Digest: {}\nMsg Digest Size: {}\nMsg Block Size: {}".format(
15          m.hexdigest(), m.digest_size, m.block_size
16      )
17  )
18  # Validate the digest against CPython3 hashlib-sha1
19  assert (
20      m.hexdigest() == "62c6e222ccd72f21b8ce0c61f42860d6c70954c0"
21  ), "Digest does not match expected string."
22  
23  
24  # Create a SHA-224 message
25  print("--SHA224--")
26  m = hashlib.sha224()
27  # Update the hash object with byte_string
28  m.update(byte_string)
29  # Obtain the digest, digest size, and block size
30  print(
31      "Msg Digest: {}\nMsg Digest Size: {}\nMsg Block Size: {}".format(
32          m.hexdigest(), m.digest_size, m.block_size
33      )
34  )
35  # Validate the digest against CPython hashlib-sha224
36  assert (
37      m.hexdigest() == "744535a10879be6b18bbcdd135032891346f530a7845d580f7869f36"
38  ), "Digest does not match expected string."
39  
40  # SHA-256
41  print("--SHA256--")
42  m = hashlib.sha256()
43  # Update the hash object with byte_string
44  m.update(byte_string)
45  # Obtain the digest, digest size, and block size
46  print(
47      "Msg Digest: {}\nMsg Digest Size: {}\nMsg Block Size: {}".format(
48          m.hexdigest(), m.digest_size, m.block_size
49      )
50  )
51  # Validate the digest against CPython hashlib-sha256
52  assert (
53      m.hexdigest() == "3ce8334ca39e66afb9c37d571da4caad68ab4a8bcbd6d584f75e4268e36c0954"
54  ), "Digest does not match expected string."
55  
56  # SHA-384
57  print("--SHA384--")
58  m = hashlib.sha384()
59  # Update the hash object with byte_string
60  m.update(byte_string)
61  # Obtain the digest, digest size, and block size
62  print(
63      "Msg Digest: {}\nMsg Digest Size: {}\nMsg Block Size: {}".format(
64          m.hexdigest(), m.digest_size, m.block_size
65      )
66  )
67  # Validate the digest against CPython hashlib-sha384
68  assert (
69      m.hexdigest()
70      == "7a12f0815f5511b8ba52c67922d1ae86dfd9bfcc4e0799ad89a9f01fc526c8f074ddb5948c06db9893536f2e65c7621b"
71  ), "Digest does not match expected string."
72  
73  # SHA-512
74  print("--SHA512--")
75  m = hashlib.sha512()
76  # Update the hash object with byte_string
77  m.update(byte_string)
78  # Obtain the digest, digest size, and block size
79  print(
80      "Msg Digest: {}\nMsg Digest Size: {}\nMsg Block Size: {}".format(
81          m.hexdigest(), m.digest_size, m.block_size
82      )
83  )
84  # Validate the digest against CPython hashlib-sha512
85  assert (
86      m.hexdigest()
87      == "20a88a9b04aa490e457f8980e57331bc85c4d6ca30735a9e502f817e74011a9ece07078e53adf70c232ac91f6c79d4cd6cc69426cd77535645fe9016a71122c2"
88  ), "Digest does not match expected string."