Skip to content

Commit

Permalink
api update for sm3 and md5 (#35)
Browse files Browse the repository at this point in the history
* api update for sm3 and md5

* switch to iter[byte] for SM3 internally

* optimize sm3 implementation
  • Loading branch information
notch1p authored Aug 6, 2024
1 parent 8f91865 commit 8b7e821
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 135 deletions.
31 changes: 29 additions & 2 deletions crypto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,39 @@ A collection of cryptographic hash functions and utilities.

```moonbit
let input = "The quick brown fox jumps over the lazy dog"
println(bytes_to_hex_string(sha1(input.to_bytes()))) // bd136cb58899c93173c33a90dde95ead0d0cf6df
println(bytes_to_hex_string(sha1(input.to_bytes())))
// => bd136cb58899c93173c33a90dde95ead0d0cf6df
```

### MD5

```moonbit
let input = "The quick brown fox jumps over the lazy dog"
println(bytes_to_hex_string(md5(input.to_bytes()))) // b0986ae6ee1eefee8a4a399090126837
println(bytes_to_hex_string(md5(input.to_bytes())))
// => b0986ae6ee1eefee8a4a399090126837
// buffered
let ctx = MD5Context::new()
ctx.update(b"a")
ctx.update(b"b")
ctx.update(b"c")
println(bytes_to_hex_string(ctx.finalize())) // or `ctx.compute()`
// => ce1473cf80c6b3fda8e3dfc006adc315
```

### SM3

```moonbit
let input = "The quick brown fox jumps over the lazy dog"
println(bytes_to_hex_string(sm3(input.to_bytes())))
// => fc2b31896629e88652ca1e3be449ec7ec93f7e5e29769f273fb973bc1858c66d
//buffered
let ctx = SM3Context::new()
ctx.update(b"a")
ctx.update(b"b")
ctx.update(b"c")
println(bytes_to_hex_string(ctx.finalize()))
// => 66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0
```
26 changes: 16 additions & 10 deletions crypto/crypto.mbti
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package moonbitlang/x/crypto

// Values
fn arr_u8_to_u32be(Array[Byte], ~i : Int = ..) -> UInt

fn byte_array_to_bytes(Array[Byte]) -> Bytes

fn bytes_to_byte_array(Bytes) -> Array[Byte]

fn bytes_to_hex_string(Bytes) -> String

fn chacha12(FixedArray[UInt], UInt, Bytes, ~nonce : UInt = ..) -> Bytes!
Expand All @@ -19,15 +13,27 @@ fn md5(Bytes) -> Bytes

fn sha1(Bytes) -> Bytes

fn sm3(Bytes) -> Array[UInt]

fn u8_to_u32be(Bytes, ~i : Int = ..) -> UInt
fn sm3(Bytes) -> Bytes

fn u8_to_u32le(Bytes, ~i : Int = ..) -> UInt
fn sm3_from_iter(Iter[Byte]) -> Bytes

fn uints_to_hex_string(Array[UInt]) -> String

// Types and methods
type MD5Context
impl MD5Context {
finalize(Self) -> Bytes
new() -> Self
update(Self, Bytes) -> Unit
}

type SM3Context
impl SM3Context {
finalize(Self) -> Bytes
new() -> Self
update(Self, Bytes) -> Unit
update_from_iter(Self, Iter[Byte]) -> Unit
}

// Type aliases

Expand Down
34 changes: 29 additions & 5 deletions crypto/md5.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,26 @@
// [RFC1321] https://www.ietf.org/rfc/rfc1321.txt
// [Ron Rivest] https://people.csail.mit.edu/rivest/Md5.c
// [md5-0.7.0] https://docs.rs/md5/0.7.0/src/md5/lib.rs.html
priv struct MD5Context {
struct MD5Context {
state : FixedArray[UInt] // state 'a' 'b' 'c' 'd'
count : FixedArray[UInt]
buffer : Bytes
}

let padding : Bytes = Bytes::make(64, b'\x00')

fn MD5Context::make() -> MD5Context {
/// update the state of given context from new `data`
pub fn MD5Context::update(self : MD5Context, data : Bytes) -> Unit {
md5_update(self, data)
}

/// an alias of `MD5Context::compute()`
pub fn MD5Context::finalize(self : MD5Context) -> Bytes {
self.md5_compute()
}

/// Instantiate a MD5 context
pub fn MD5Context::new() -> MD5Context {
padding[0] = b'\x80'
{
state: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476],
Expand All @@ -33,7 +44,8 @@ fn MD5Context::make() -> MD5Context {
}
}

fn MD5Context::compute(self : MD5Context) -> Bytes {
/// compute MD5 digest from given context
fn MD5Context::md5_compute(self : MD5Context) -> Bytes {
let input = FixedArray::make(16, 0U)
let idx = (self.count[0].lsr(3) & 0x3f).to_int()
input[14] = self.count[0]
Expand Down Expand Up @@ -220,9 +232,9 @@ fn md5_transform(state : FixedArray[UInt], input : FixedArray[UInt]) -> Unit {
/// - Note that MD5 is considered _cryptographically broken_.
/// Unless mandated, more secure alternatives should be preferred.
pub fn md5(data : Bytes) -> Bytes {
let ctx = MD5Context::make()
let ctx = MD5Context::new()
md5_update(ctx, data)
ctx.compute()
ctx.md5_compute()
}

test "md5_wb" {
Expand All @@ -232,3 +244,15 @@ test "md5_wb" {
content="b0986ae6ee1eefee8a4a399090126837",
)
}

test {
let ctx = MD5Context::new()
md5_update(ctx, b"\x61")
md5_update(ctx, b"\x62")
md5_update(ctx, b"\x63")
let res1 = bytes_to_hex_string(ctx.md5_compute())
let ctx = MD5Context::new()
md5_update(ctx, b"\x61\x62\x63")
let res2 = bytes_to_hex_string(ctx.md5_compute())
@test.eq!(res1, res2)
}
Loading

0 comments on commit 8b7e821

Please sign in to comment.