Simple test

Ensure your device works with this simple test.

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