Skip to content

lczerniawski/crypto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Crypto for Mojo

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.

Overview

This repository currently includes:

  • MD5 and SHA-1 hash implementations
  • Secure random integer generation

Current Modules

Module Notes
crypto.hashes MD5 and SHA-1 hashing functions
crypto.random Secure integer generation functions

Installation

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 crypto

This fetches the latest version and makes it immediately available for import.

Quick Start

Hashing

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())

Random Values

from crypto.random import generate_secure_u32

def main() raises:
	var value = generate_secure_u32()
	print("Random value:", value)

API Reference

Hashes

md5(data: Span[UInt8, ...]) -> MD5Digest

Compute an MD5 digest for the given bytes.

sha1(data: Span[UInt8, ...]) -> SHA1Digest

Compute a SHA-1 digest for the given bytes.

MD5Digest

Represents a 128-bit MD5 digest.

Methods:

  • to_hex() -> String - Returns the digest as a 32-character lowercase hexadecimal string
  • to_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 values

SHA1Digest

Represents a 160-bit SHA-1 digest.

Methods:

  • to_hex() -> String - Returns the digest as a 40-character lowercase hexadecimal string
  • to_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 values

Random

generate_secure_u8()

Generate a secure random UInt8 value.

generate_secure_u16()

Generate a secure random UInt16 value.

generate_secure_u32()

Generate a secure random UInt32 value.

generate_secure_u64()

Generate a secure random UInt64 value.

generate_secure_u128()

Generate a secure random UInt128 value.

Tests

Run all tests using Pixi:

pixi run test

Or run individual test files with Mojo:

mojo run -I src tests/<test-file>.mojo

Project Layout

  • src/crypto/hashes/ - Hashing algorithms and digest types
  • src/crypto/random/ - Secure random number helpers
  • src/crypto/helpers.mojo - Shared low-level helpers
  • tests/ - Unit tests for the available primitives

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome. Please keep changes focused, well-tested, and consistent with the existing module structure.

About

A Mojo cryptography library intended to cover the commonly used cryptographic algorithms.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors