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