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.
  • 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.
  • 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-0.2.0-*.whl
└── secure_memory-0.2.0.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)

Creates a fixed-size, zero-initialized buffer.

memory = SecureMemory(64)

write(offset, data)

Writes bytes into the buffer.

memory.write(0, b"secret")
  • Raises ValueError for invalid ranges.
  • Raises BufferError while a view is alive.

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.

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.

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.
  • 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 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.0

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.0
File Actions
secure_memory-0.3.0-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for secure_memory-0.3.0-cp312-cp312-win_amd64.whl
secure_memory-0.3.0-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.0-cp312-cp312-manylinux_2_34_x86_64.whl
secure_memory-0.3.0-cp312-cp312-manylinux_2_34_aarch64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ ARM64 Details for secure_memory-0.3.0-cp312-cp312-manylinux_2_34_aarch64.whl
secure_memory-0.3.0-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details for secure_memory-0.3.0-cp312-cp312-macosx_11_0_arm64.whl
secure_memory-0.3.0-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.0-cp312-cp312-macosx_10_12_x86_64.whl

Total release size: 1.1 MB

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

Download URL secure_memory-0.3.0-cp312-cp312-win_amd64.whl
Size 137.6 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
d01dea9d11f038e2be5d0d99d2e294febd28b3b197adbf84713668511d54d119
BLAKE2b-256 checksum
How to use checksums
85be1cdcdc7d29e0c605f93859d691e1552d63db9e0abeac4e0aaebffe53e5e2
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 Oct 30, 2026.

Transparency log

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

Download URL secure_memory-0.3.0-cp312-cp312-manylinux_2_34_x86_64.whl
Size 257.9 kB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
b9a1c8f21542689722d02c202c05e40693bb376b46ef7a72f2308a084d0ceadd
BLAKE2b-256 checksum
How to use checksums
8cbfb97ec0b045da26128ad135fb193a37a927d8752bae56a79cb495c63a563c
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 Oct 30, 2026.

Transparency log

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

Download URL secure_memory-0.3.0-cp312-cp312-manylinux_2_34_aarch64.whl
Size 261.9 kB
Tags CPython 3.12 Linux glibc 2.34+ ARM64
SHA-256 checksum
How to use checksums
78e622d8ec0e714f730e3b33ff19325753f01b53e46c5abd27f2fb65d9593fd1
BLAKE2b-256 checksum
How to use checksums
2e54e2b19641138c9477ef9b2464329513fd9ccfb32872752cced34e42f186b6
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 Oct 30, 2026.

Transparency log

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

Download URL secure_memory-0.3.0-cp312-cp312-macosx_11_0_arm64.whl
Size 234.1 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
45c177bacf5eff06d8ae28976a91eef3303ea092b9adbfb6bfcb587d8b14c10b
BLAKE2b-256 checksum
How to use checksums
e3d4f524edd7347abd807206f48348a8507805332b77cd6865ee538af73deccd
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 Oct 30, 2026.

Transparency log

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

Download URL secure_memory-0.3.0-cp312-cp312-macosx_10_12_x86_64.whl
Size 239.7 kB
Tags CPython 3.12 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
b3cc2b6d9a9675e852dd1b19b30b2e544e2dde2f2397c6f2ca9351dd4bf82fa9
BLAKE2b-256 checksum
How to use checksums
f6a207bf9f9fb29af7e98f1920bcce87ab9a993a12f3cbb0a5e6ce507cef85f9
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 Oct 30, 2026.

Transparency log

Security for secure-memory 0.3.0

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.0-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 Oct 30, 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