hashlib — 0.2.0

org.pycforge.hashlib · Author: PyCForge

License: GPL-3.0-only · Created: 2026-11-20

Runtime conversion · Direct declared calls

Using this import · Functions · Hash object methods · Conversion contract · Worked examples

Function index

file_digest · sha256

Overview

Create a SHA-256 hash with optional initial binary data. Hash a readable binary file from its current position with SHA-256.

Using this import

Use *import hashlib as hashes. Start with hashes.sha256(), update it with binary chunks, and call hexdigest() to compare a file with an expected digest.

Author's limitations

Runtime supports SHA-256. Pass optional initial data by position. update(data) accepts bytes, bytearray or a live memory mapping and consumes its contents during the call. Use text.encode() or bytes(text, 'utf-8') for UTF-8 text. file_digest(fileobj, digest, /) reads a blocking Runtime binary file from its current position and leaves it open. Pass a SHA-256 name or a direct declared SHA-256 constructor. User-defined streams and digest factories are outside the profile. The file, memory and work budgets apply. digest() returns 32 bytes and hexdigest() returns 64 lowercase hexadecimal characters. Both leave the hash available for updates. Assignment shares one hash, and copy() creates an independent snapshot. The read-only attributes are name, digest_size and block_size. Runtime memory and work limits apply. The usedforsecurity option, generic new(), other algorithms and Typed conversion are outside this profile.

Release notes

UTF-8 text and bounded SHA-256 file hashing with a reusable Runtime buffer.

Conversion contract

Declare *import hashlib in a Runtime module. Call the constructor through its declared module name or alias. You can save a bound update or digest method on a returned hash.

Hash object methods

Use UTF-8 encoded text as input. The file helper reads a blocking binary file from its current position and returns an independent hash. The caller retains the open handle.

update(data) consumes a binary chunk and returns None. The snapshot methods accept no arguments.

digest() returns independent bytes. hexdigest() returns lowercase hexadecimal text. You can read either digest repeatedly and continue updating.

copy() creates independent state at the same point. Assigning the object to another name shares subsequent updates.

Read name for the algorithm, digest_size for the 32-byte result and block_size for the 64-byte processing block.

hashlib.file_digest(fileobj, digest, /) -> sha256-state

Hash a readable binary file from its current position with SHA-256.

Normalized semantic slots: fileobj, digest. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Reads from the current file position to EOF, preserves the caller’s handle and charges file and hash work. Call digest(), hexdigest() or update() on the result.

Argument 1: A readable, open Runtime binary file.

Argument 2: A SHA-256 name or direct declared SHA-256 constructor.

Returns: A new SHA-256 object positioned after the bytes read.

View example: hash-and-copy

hashlib.sha256(data=b'', /) -> sha256-state

Create a SHA-256 hash with optional initial binary data.

Normalized semantic slots: data. These slots are supplied by the engine; the signature above defines source arguments and defaults.

update(data) returns None. Repeated digest reads preserve the state. Call copy() before two streams diverge.

Argument 1: Optional starting bytes, bytearray or an open mapping, supplied by position.

Returns: A new SHA-256 object.

View example: hash-and-copy

Worked examples

Compare your run with the expected output below. The SDK checks conversion when authoring a package. Opening Help and registering a package do not execute the examples.

Example: hash-and-copy

*import hashlib as hashes

def main():
    state = hashes.sha256()
    state.update(b"a")
    snapshot = state.copy()
    state.update(b"bc")
    print(state.hexdigest())
    print(len(state.digest()), snapshot.digest() == hashes.sha256(b"a").digest())
    with open("sample.bin", "wb") as output:
        output.write("abc".encode("utf-8"))
    with open("sample.bin", "rb") as stream:
        state = hashes.file_digest(stream, "sha256")
    print(state.hexdigest())
    return 0

Expected standard output:

ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
32 True
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

Expected exit code: 0