A Mojo cryptography library intended to cover the commonly used cryptographic algorithms.
Warning
🚧 Work in progress: This library is actively being built out, and the plan is to implement all standard crypto algorithms over time.
This repository currently includes:
- MD5 and SHA-1 hash implementations
- Secure random integer generation
| Module | Notes |
|---|---|
crypto.hashes |
MD5 and SHA-1 hashing functions |
crypto.random |
Secure integer generation functions |
crypto is available in the modular-community package repository. To install, add it to your channels in pixi.toml:
channels = [
"https://conda.modular.com/max",
"https://repo.prefix.dev/modular-community",
"conda-forge"
]Then, install using the Pixi CLI:
pixi add cryptoThis fetches the latest version and makes it immediately available for import.
from crypto.hashes import md5, sha1
def main() raises:
var md5_digest = md5("abc".as_bytes())
var sha1_digest = sha1("abc".as_bytes())
print("MD5:", md5_digest.to_hex())
print("SHA-1:", sha1_digest.to_hex())from crypto.random import generate_secure_u32
def main() raises:
var value = generate_secure_u32()
print("Random value:", value)Compute an MD5 digest for the given bytes.
Compute a SHA-1 digest for the given bytes.
Represents a 128-bit MD5 digest.
Methods:
to_hex() -> String- Returns the digest as a 32-character lowercase hexadecimal stringto_bytes() -> List[UInt8]- Returns the digest as a list of 16 bytes (little-endian format)
Example:
var digest = md5("hello".as_bytes())
print(digest.to_hex()) # "5d41402abc4b2a76b9719d911017c592"
var bytes = digest.to_bytes() # List of 16 UInt8 valuesRepresents a 160-bit SHA-1 digest.
Methods:
to_hex() -> String- Returns the digest as a 40-character lowercase hexadecimal stringto_bytes() -> List[UInt8]- Returns the digest as a list of 20 bytes (big-endian format)
Example:
var digest = sha1("hello".as_bytes())
print(digest.to_hex()) # "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"
var bytes = digest.to_bytes() # List of 20 UInt8 valuesGenerate a secure random UInt8 value.
Generate a secure random UInt16 value.
Generate a secure random UInt32 value.
Generate a secure random UInt64 value.
Generate a secure random UInt128 value.
Run all tests using Pixi:
pixi run testOr run individual test files with Mojo:
mojo run -I src tests/<test-file>.mojosrc/crypto/hashes/- Hashing algorithms and digest typessrc/crypto/random/- Secure random number helperssrc/crypto/helpers.mojo- Shared low-level helperstests/- Unit tests for the available primitives
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome. Please keep changes focused, well-tested, and consistent with the existing module structure.