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.2.2

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

Total release size: 1.1 MB

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

Download URL secure_memory-0.2.2-cp312-cp312-win_amd64.whl
Size 131.3 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
d8182ee4b9a1db4a2404577fa9c70ff2c792d646295ceaf094ed0239bd5b5f88
BLAKE2b-256 checksum
How to use checksums
5b72c93bd968763ee65f013ea03456de347e17c6243300425bf8b32edebc6f6e
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 29, 2026.

Transparency log

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

Download URL secure_memory-0.2.2-cp312-cp312-manylinux_2_34_x86_64.whl
Size 251.6 kB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
599c8e12e737ef388c6d3143d9d90d1eaba54093937be9534a05d1a01bb71422
BLAKE2b-256 checksum
How to use checksums
fbd152db0d895f2e1c1b0a0e6ab5ab1e69035eee9676357ee93e393b2c21ae68
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 29, 2026.

Transparency log

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

Download URL secure_memory-0.2.2-cp312-cp312-manylinux_2_34_aarch64.whl
Size 254.1 kB
Tags CPython 3.12 Linux glibc 2.34+ ARM64
SHA-256 checksum
How to use checksums
8fed44670e5cb679c1411bf6bc93db1b86fbc237508217197c15bc8268f60988
BLAKE2b-256 checksum
How to use checksums
71ccdecc06721a9e230a2a5d57d045f9957e62a344fe3a4e00ba2f88b1d823bf
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 29, 2026.

Transparency log

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

Download URL secure_memory-0.2.2-cp312-cp312-macosx_11_0_arm64.whl
Size 228.0 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
51b46a13e1284e2e8b5d3554154a0601faf1673cbbe1510fecf389ff68732dd4
BLAKE2b-256 checksum
How to use checksums
00cf68148a5fbff7060a5160f332be87e879fb24e61484d526c5b6c2c2912c9a
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 29, 2026.

Transparency log

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

Download URL secure_memory-0.2.2-cp312-cp312-macosx_10_12_x86_64.whl
Size 232.5 kB
Tags CPython 3.12 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
4025aed52dc893fdca8c507ded388fd81da1dc5fb104a535a534d827718cae92
BLAKE2b-256 checksum
How to use checksums
ca63ec4da2c9c3f5ca8361388f85d2760f4c19cff540c30a6bb3d3a6042a92de
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 29, 2026.

Transparency log

Security for secure-memory 0.2.2

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.2.2-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 29, 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