Files
purepycrypto/purepycrypto/tests/test_sha3.py
T
dvs 0d69391aa9 Release v3.2.0: pure-Python cryptography library (English docs/comments/errors)
- Zero-dependency pure-Python cryptography: SHA-2 / SHA-3 / SHAKE / BLAKE2b,
  HMAC, PBKDF2 / HKDF / scrypt / Argon2id, AES (ECB/CBC/CTR/CFB/OFB/GCM),
  AES-GCM-SIV, ChaCha20-Poly1305 / XChaCha20-Poly1305, X25519, Ed25519,
  RSA (keygen / OAEP / PSS / PEM), ASN.1 DER
- Unified error system: PPYC_ERR fatal exception vs (code, message, suggestion)
  non-fatal 3-tuple return
- Interactive TUI (Chinese / English i18n) + non-interactive CLI
- v3.2.0 adds the encoding, rand and kdf2 modules
- Documentation, code comments and error messages fully in English;
  the interactive TUI keeps its Chinese interface
- Tests: RFC / FIPS / NIST test vectors, 639 passing
2026-09-27 09:40:12 +08:00

79 lines
3.2 KiB
Python

# -*- coding: utf-8 -*-
"""SHA-3 / Keccak / SHAKE / HMAC-SHA3 tests (FIPS 202)."""
import hashlib
import hmac as _hmac
import os
import pytest
from purepycrypto import (
sha3_224, sha3_256, sha3_384, sha3_512,
shake128, shake256,
hmac_sha3_256, hmac_sha3_512,
)
from ._vectors import load
def test_fips202_known_answers():
assert sha3_256(b"").hex() == (
"a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a")
assert sha3_256(b"abc").hex() == (
"3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532")
assert sha3_512(b"").hex() == (
"a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a6"
"15b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26")
assert sha3_512(b"abc").hex() == (
"b751850b1a57168a5693cd924b6b096e08f621827444f70d884f5d0240d2712e"
"10e116e9192af3c91a7ec57647e3934057340b4cf408d5a56592f8274eec53f0")
assert sha3_224(b"abc").hex() == (
"e642824c3f8cf24ad09234ee7d3c766fc9a3a5168d0c94ad73b46fdf")
assert sha3_384(b"abc").hex().startswith("ec01498288516fc926459f58e2c6ad8d")
assert shake128(b"", 32).hex() == (
"7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26")
assert shake256(b"", 32).hex() == (
"46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f")
def test_length_sweep_matches_hashlib():
"""Cross-block boundary length sweep, exactly matching hashlib."""
for length in (0, 1, 63, 64, 71, 72, 73, 103, 104, 105, 135, 136, 137,
167, 168, 169, 500):
data = os.urandom(length)
assert sha3_224(data) == hashlib.sha3_224(data).digest()
assert sha3_256(data) == hashlib.sha3_256(data).digest()
assert sha3_384(data) == hashlib.sha3_384(data).digest()
assert sha3_512(data) == hashlib.sha3_512(data).digest()
assert shake128(data, 64) == hashlib.shake_128(data).digest(64)
assert shake256(data, 64) == hashlib.shake_256(data).digest(64)
def test_shake_variable_output_length():
data = b"variable length test"
for outlen in (16, 32, 64, 100, 200):
assert len(shake128(data, outlen)) == outlen
assert len(shake256(data, outlen)) == outlen
assert shake128(data, outlen) == hashlib.shake_128(data).digest(outlen)
assert shake256(data, outlen) == hashlib.shake_256(data).digest(outlen)
def test_hmac_sha3_matches_stdlib():
for key_len in (0, 16, 32, 100, 200):
key = os.urandom(key_len)
msg = os.urandom(80)
assert hmac_sha3_256(key, msg) == _hmac.new(key, msg, hashlib.sha3_256).digest()
assert hmac_sha3_512(key, msg) == _hmac.new(key, msg, hashlib.sha3_512).digest()
def test_vectors_file():
try:
vec = load("sha3_fips202")
except FileNotFoundError:
pytest.skip("vectors/sha3_fips202.json missing")
for case in vec.get("cases", []):
msg = case["msg_utf8"].encode()
assert sha3_224(msg).hex() == case["sha3_224"], case.get("id")
assert sha3_256(msg).hex() == case["sha3_256"], case.get("id")
assert sha3_384(msg).hex() == case["sha3_384"], case.get("id")
assert sha3_512(msg).hex() == case["sha3_512"], case.get("id")