README

#moonbitlang/x/crypto

#Overview

A collection of cryptographic hash functions and utilities.

#Usage

Strings in MoonBit are UTF-16 LE encoded.

#SHA-1

///|
test {
let input = "The quick brown fox jumps over the lazy dog"
inspect(
bytes_to_hex_string(sha1(@encoding.encode(UTF16, input))),
content="bd136cb58899c93173c33a90dde95ead0d0cf6df",
)
}

#MD5

///|
test {
let input = "The quick brown fox jumps over the lazy dog"
inspect(
bytes_to_hex_string(md5(@encoding.encode(UTF16, input))),
content="b0986ae6ee1eefee8a4a399090126837",
)

// buffered
let ctx = MD5::new()
ctx.update(b"a")
ctx.update(b"b")
ctx.update(b"c")
inspect(
bytes_to_hex_string(ctx.finalize()),
content="900150983cd24fb0d6963f7d28e17f72",
)
}

#SM3

///|
test {
let input = "The quick brown fox jumps over the lazy dog"
inspect(
bytes_to_hex_string(sm3(@encoding.encode(UTF16, input))),
content="fc2b31896629e88652ca1e3be449ec7ec93f7e5e29769f273fb973bc1858c66d",
)

//buffered
let ctx = SM3::new()
ctx.update(b"a".to_fixedarray())
ctx.update(b"b".to_fixedarray())
ctx.update(b"c".to_fixedarray())
inspect(
bytes_to_hex_string(ctx.finalize()),
content="66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0",
)
}

#
ByteSource

trait ByteSource

impl ByteSource for Bytes

#
CryptoHasher

pub(open) trait CryptoHasher {
fn size(Self) -> Int
fn block_size(Self) -> Int
fn reset(Self) -> Unit
fn update(Self, BytesView) -> Unit
fn finalize_into(Self, FixedArray[Byte], offset~ : Int) -> Unit
}

#
ChaCha

type ChaCha

#
ChaCha::chacha12

fn[K : ByteSource, N : ByteSource] ChaCha::chacha12(key : K, nonce : N, counter? : UInt) -> ChaCha raise

Creates a ChaCha12 encryption context following the RFC 8439 standard.
  • [key] must be 256-bit (32 bytes), in little-endian order.
  • [nonce] must be a 96-bit (12 bytes) bytes, in little-endian order.
  • [counter] is the counter value, defaulting to 0.

raise Error if the length of key or nonce is invalid.

#
ChaCha::chacha20

fn[K : ByteSource, N : ByteSource] ChaCha::chacha20(key : K, nonce : N, counter? : UInt) -> ChaCha raise

Creates a ChaCha20 encryption context following the RFC 8439 standard.
  • [key] must be 256-bit (32 bytes), in little-endian order.
  • [nonce] must be a 96-bit (12 bytes) bytes, in little-endian order.
  • [counter] is the counter value, defaulting to 0.

raise Error if the length of key or nonce is invalid.

#
ChaCha::chacha8

fn[K : ByteSource, N : ByteSource] ChaCha::chacha8(key : K, nonce : N, counter? : UInt) -> ChaCha raise

Creates a ChaCha8 encryption context following the RFC 8439 standard.
  • [key] must be 256-bit (32 bytes), in little-endian order.
  • [nonce] must be a 96-bit (12 bytes) bytes, in little-endian order.
  • [counter] is the counter value, defaulting to 0.

raise Error if the length of key or nonce is invalid.

#
ChaCha::transform

fn[D : ByteSource] ChaCha::transform(self : ChaCha, data : D, target : FixedArray[Byte], offset? : Int) -> Unit

Transforms the given data using the ChaCha encryption algorithm.
  • [data] is the data to be transformed.
  • [target] is the output buffer to store the transformed data.
  • [offset] is the offset in the target buffer where the transformed data will be written

If the length of [data] is less then the length of [target] minus [offset], it will panic.

#
MD5

#alias(MD5Context, deprecated="Use `MD5` instead")
type MD5

impl CryptoHasher for MD5

#
MD5::finalize

fn MD5::finalize(self : MD5) -> FixedArray[Byte]

#
MD5::new

fn MD5::new() -> MD5

Instantiate a MD5 context

#
MD5::update

fn[Data : ByteSource] MD5::update(self : MD5, data : Data) -> Unit

update the state of given context from new data

#
SHA256

#alias(Sha256Context, deprecated="Use `SHA256` instead")
type SHA256

#
SHA256::finalize

fn SHA256::finalize(self : SHA256) -> FixedArray[Byte]

#
SHA256::new

fn SHA256::new(reg? : FixedArray[UInt]) -> SHA256

Instantiate a Sha256 context reg is the initial hash value. Defaults to Sha256's.

#
SHA256::update

fn[Data : ByteSource] SHA256::update(self : SHA256, data : Data) -> Unit

update the state of given context from new data

#
SHA256::update_from_iter

fn SHA256::update_from_iter(self : SHA256, data : Iter[Byte]) -> Unit

#
SM3

#alias(SM3Context, deprecated="Use `SM3` instead")
type SM3

impl CryptoHasher for SM3

#
SM3::finalize

fn SM3::finalize(self : SM3) -> FixedArray[Byte]

#
SM3::new

fn SM3::new() -> SM3

Instantiate a SM3 context

#
SM3::update

fn[Data : ByteSource] SM3::update(self : SM3, data : Data) -> Unit

update the state of given context from new data

#
SM3::update_from_iter

fn SM3::update_from_iter(self : SM3, data : Iter[Byte]) -> Unit

#
bytes_to_hex_string

fn[D : ByteSource] bytes_to_hex_string(input : D) -> String

print a sequence of byte in hex representation

#
chacha12

#deprecated("Use ChaCha::chacha12 and ChaCha::transform instead")
fn[Data : ByteSource] chacha12(key : FixedArray[UInt], counter : UInt, block : Data, nonce? : UInt) -> FixedArray[Byte] raise

Encrypts a block of data using the ChaCha12 algorithm.
  • [key] must be 8 32-bit unsigned integers.
  • [counter] is the counter value.
  • [block] is the block of data to be encrypted.
  • [nonce] is default to 0
  • Returns the encrypted block of data.

#
chacha20

#deprecated("Use ChaCha::chacha20 and ChaCha::transform instead")
fn[Data : ByteSource] chacha20(key : FixedArray[UInt], counter : UInt, block : Data, nonce? : UInt) -> FixedArray[Byte] raise

Encrypts a block of data using the ChaCha20 algorithm.
  • [key] must be 8 32-bit unsigned integers.
  • [counter] is the counter value.
  • [block] is the block of data to be encrypted.
  • [nonce] is default to 0
  • Returns the encrypted block of data.

#
chacha8

#deprecated("Use ChaCha::chacha8 and ChaCha::transform instead")
fn[Data : ByteSource] chacha8(key : FixedArray[UInt], counter : UInt, block : Data, nonce? : UInt) -> FixedArray[Byte] raise

Encrypts a block of data using the ChaCha8 algorithm.
  • [key] must be 8 32-bit unsigned integers.
  • [counter] is the counter value.
  • [block] is the block of data to be encrypted.
  • [nonce] is default to 0
  • Returns the encrypted block of data.

#
hmac

fn[H : CryptoHasher] hmac(hash : H, key : BytesView, message : BytesView) -> FixedArray[Byte]

Computes HMAC (Hash-based Message Authentication Code) using a specified cryptographic hash function.

Parameters:

  • hash : A hash function implementation that satisfies the CryptoHasher trait.
  • key : The secret key used for generating the authentication code.
  • message : The message to be authenticated.

Returns a fixed-size array of bytes containing the HMAC value. The length of the output depends on the underlying hash function's output size.

#
md5

fn[Data : ByteSource] md5(data : Data) -> FixedArray[Byte]

Compute the MD5 digest of some data based on RFC1321.
  • Note that MD5 is considered cryptographically broken. Unless mandated, more secure alternatives should be preferred.

#
sha1

fn[Data : ByteSource] sha1(input : Data) -> FixedArray[Byte]

#
sha224

fn[Data : ByteSource] sha224(data : Data) -> FixedArray[Byte]

#
sha224_from_iter

fn sha224_from_iter(data : Iter[Byte]) -> FixedArray[Byte]

#
sha256

fn[Data : ByteSource] sha256(data : Data) -> FixedArray[Byte]

Compute the Sha256 digest in Bytes of some data. Note that Sha256 is big-endian.

#
sha256_from_iter

fn sha256_from_iter(data : Iter[Byte]) -> FixedArray[Byte]

#
sm3

fn[Data : ByteSource] sm3(data : Data) -> FixedArray[Byte]

Compute the SM3 digest in FixedArray[Byte] of some data. Note that SM3 is big-endian.

#
sm3_from_iter

fn sm3_from_iter(data : Iter[Byte]) -> FixedArray[Byte]

#
uints_to_hex_string

fn uints_to_hex_string(input : Iter[UInt]) -> String

convert a sequence of UInt to hex representation

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io