Skip to main content

Secure Memory

secure_memory is a Rust/PyO3 Python extension for fixed-size sensitive-memory buffers.

secure_memory provides:

  • Zeroization on drop.
  • Explicit clear() support.
  • Best-effort OS memory locking (strict mode: require_lock=True).
  • Core-dump exclusion on Linux/Android (best-effort).
  • Read-only zero-copy memoryview access.
  • Scoped access with with_buffer() and view().
  • Fixed-size storage to keep exported pointers stable.
  • Bounds checks for reads and writes.
  • Bundled type stubs.
  • Python 3.9+ support.
  • CPython support.

Requirements

  • Python 3.9+
  • CPython
  • Rust 1.83+
  • maturin for building the extension
  • A supported OS/toolchain

Supported platforms declared by the project:

  • Linux
  • macOS
  • Windows

Installation

From PyPI

If a compatible release is published:

python -m pip install secure-memory

Using a virtual environment is recommended:

python -m venv .venv

Activate it, then install:

python -m pip install secure-memory

From source

Install the build backend:

python -m pip install -U maturin

Install the package into the active environment:

maturin develop --release

This is primarily intended for local development.

Build distributions

The project uses maturin as its PEP 517 build backend.

Install the standard Python build frontend:

python -m pip install -U build

Build the project:

python -m build

The distributions are written to:

dist/

You should normally get:

dist/
├── secure_memory-*-*.whl
└── secure_memory-*.tar.gz

The files are:

  • .whl — built distribution.
  • .tar.gz — source distribution.

Because this package contains a compiled Rust extension, wheels are platform-specific.


Basic usage

from secure_memory import SecureMemory

memory = SecureMemory(32)

memory.write(0, b"secret")

with memory.view() as view:
    process_secret(view)

memory.clear()

API

SecureMemory(size, *, require_lock=False)

Creates a fixed-size, zero-initialized buffer.

memory = SecureMemory(64)
  • Raises MemoryError if allocation fails.
  • With require_lock=True, raises OSError if the memory can't be locked (ValueError for size 0).

write(offset, data)

Writes a byte buffer (bytes, bytearray, memoryview of bytes) into the buffer.

memory.write(0, b"secret")
  • Raises ValueError for invalid ranges.
  • Raises BufferError while a view is alive.
  • Other item formats (e.g. array('i')) are rejected.
  • Prefer a bytearray source so you can overwrite it afterwards.

read(offset, length)

Returns a Python bytes copy.

data = memory.read(0, 6)

Use zero-copy access when a copy is undesirable.

clear()

Zeroizes the buffer without changing its size.

memory.clear()

len()

Returns the buffer size. len(memory) also works.

size = memory.len()

is_locked()

Reports whether OS memory locking succeeded.

memory.is_locked()

as_bytes()

Returns the complete buffer as a Python bytes copy.

data = memory.as_bytes()

with_buffer(callback)

Provides a temporary read-only memoryview.

result = memory.with_buffer(lambda view: process_secret(view))

The view is released automatically.

view()

Provides scoped read-only access:

with memory.view() as view:
    process_secret(view)

The view is released when the block exits.

Zero-copy access

Python's buffer protocol can expose the buffer without copying:

view = memoryview(memory)

The view is read-only:

view.readonly

Writing through the view is not supported.

Release it when finished:

view.release()

Operations such as bytes(view) and view.tobytes() create copies.

Memory locking

The extension attempts to lock its allocation into RAM:

  • Unix: mlock()
  • Windows: VirtualLock()

Check the result with:

memory.is_locked()

Locking is best-effort and may fail because of operating-system limits or permissions. Use require_lock=True to raise OSError instead.

Buffers use whole pages, so each one counts at least one page against the lock limit.

Core dumps

On Linux/Android the buffer is excluded from core dumps (best-effort). Not on macOS/Windows.

Zeroization

The buffer is wiped in two ways:

memory.clear()

and automatically when the Rust object is dropped.

Zeroization applies only to the Rust-owned allocation.

Python-level copies are outside that boundary.

Safe access model

The buffer is intentionally fixed-size.

This prevents its backing allocation from being resized while Python holds a pointer to it.

While a view is alive:

memory.write(...)

is rejected.

clear() is still allowed so a secret can always be wiped.


Security considerations

secure_memory is a memory-handling primitive, not a complete secrets-management system.

Avoid unnecessary copies:

bytes(view)
view.tobytes()
memory.as_bytes()

when a zero-copy operation is possible.

Also avoid:

  • Logging secrets.
  • Converting secrets to str unnecessarily.
  • Passing secrets to write() as immutable bytes.
  • Keeping temporary secret objects alive longer than needed.
  • Assuming is_locked() is always True.
  • Treating memory locking as encryption.

For persistent secrets, use an appropriate secrets manager, OS credential store, HSM, or encrypted storage instead.


Development

Build the extension:

maturin develop

Build an optimized version:

maturin develop --release

Build the Rust crate:

cargo build

Run Rust tests:

cargo test

Run Python tests:

python -m pip install pytest
maturin develop
pytest

Run formatting checks:

cargo fmt --all --check

Run Clippy:

cargo clippy --all-targets --all-features -- -D warnings

TestPyPI

Install Twine:

python -m pip install -U twine

Upload to TestPyPI:

python -m twine upload --repository testpypi dist/*

Test installation:

python -m pip install --index-url https://test.pypi.org/simple/ secure_memory

PyPI

After testing the distributions:

python -m twine upload dist/*

Then users can install the package with:

python -m pip install secure_memory

License

Licensed under the Apache License 2.0.

See the repository's license file for the complete terms.


Python packaging references

  • A PEP 517 build backend.
  • Source distributions and wheels.
  • python -m build for creating distributions.
  • pip for installation.
  • TestPyPI for release testing.
  • PyPI for production distribution.
  • Twine or trusted publishing for uploads.

Metadata

Release files for secure-memory 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for secure-memory 0.3.1
File Actions
secure_memory-0.3.1-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for secure_memory-0.3.1-cp312-cp312-win_amd64.whl
secure_memory-0.3.1-cp312-cp312-manylinux_2_34_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ x86-64 Details for secure_memory-0.3.1-cp312-cp312-manylinux_2_34_x86_64.whl
secure_memory-0.3.1-cp312-cp312-manylinux_2_34_aarch64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ ARM64 Details for secure_memory-0.3.1-cp312-cp312-manylinux_2_34_aarch64.whl
secure_memory-0.3.1-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details for secure_memory-0.3.1-cp312-cp312-macosx_11_0_arm64.whl
secure_memory-0.3.1-cp312-cp312-macosx_10_12_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.12+ x86-64 Details for secure_memory-0.3.1-cp312-cp312-macosx_10_12_x86_64.whl

Total release size: 972.1 kB

Release files / secure_memory-0.3.1-cp312-cp312-win_amd64.whl

Download URL secure_memory-0.3.1-cp312-cp312-win_amd64.whl
Size 131.4 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
23a6c2ca0550dd425bf942d89603915e2cae6a8b0e26ca64a76fe028f31f96d4
BLAKE2b-256 checksum
How to use checksums
a29f48527c1d7023f9e38d6d979e4a981802619924d8ed586880d8be80875080
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Release files / secure_memory-0.3.1-cp312-cp312-manylinux_2_34_x86_64.whl

Download URL secure_memory-0.3.1-cp312-cp312-manylinux_2_34_x86_64.whl
Size 217.8 kB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
2516325a56e74fc8770e344dddcfa810971b074e497fdee543b94bf4e08a8b0a
BLAKE2b-256 checksum
How to use checksums
9ba627ad9c40431b81686f8f590ce4ab26810a367f99910e137b79301783cbc1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Release files / secure_memory-0.3.1-cp312-cp312-manylinux_2_34_aarch64.whl

Download URL secure_memory-0.3.1-cp312-cp312-manylinux_2_34_aarch64.whl
Size 211.5 kB
Tags CPython 3.12 Linux glibc 2.34+ ARM64
SHA-256 checksum
How to use checksums
550faa4b075dc21ad473381ef1d1022a8c365744bfd1c43a3953e082f7cda00c
BLAKE2b-256 checksum
How to use checksums
bd5ed5b27243b944f7172c4c6c57d3b609ddf6892e229229dcc81c5779867a1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Release files / secure_memory-0.3.1-cp312-cp312-macosx_11_0_arm64.whl

Download URL secure_memory-0.3.1-cp312-cp312-macosx_11_0_arm64.whl
Size 201.1 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c874e75a46565b6a0df0256e92768d3eeddc97b4350ba14cddc0498208584fb4
BLAKE2b-256 checksum
How to use checksums
11f5904daefb6ce4afbe57038a8fb507dac99755cc5647ce8c44eeb2151ec638
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Release files / secure_memory-0.3.1-cp312-cp312-macosx_10_12_x86_64.whl

Download URL secure_memory-0.3.1-cp312-cp312-macosx_10_12_x86_64.whl
Size 210.3 kB
Tags CPython 3.12 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
d55235e7a89bfe3db116fb4f396a41394b624f5262ba0ca5fc84e3af2f8eda10
BLAKE2b-256 checksum
How to use checksums
ce53577341ef40ffa853005347e9ccc5438ed72d70db012642bc030e4840e3ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Security for secure-memory 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 5 files in this release have been uploaded with provenance metadata.

Select from 5 files

Attestation for secure_memory-0.3.1-cp312-cp312-win_amd64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page