Skip to content

Commit

Permalink
optimize sm3 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
notch1p committed Aug 6, 2024
1 parent ef34e9c commit c2f585b
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 149 deletions.
26 changes: 13 additions & 13 deletions crypto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@ println(bytes_to_hex_string(sha1(input.to_bytes())))

```moonbit
let input = "The quick brown fox jumps over the lazy dog"
println(bytes_to_hex_string(md5sum(input.to_bytes())))
println(bytes_to_hex_string(md5(input.to_bytes())))
// => b0986ae6ee1eefee8a4a399090126837
// or buffered
let ctx = MD5Context::make()
ctx.update(b"\x61") // 'a'
ctx.update(b"\x62") // 'b'
ctx.update(b"\x63") // 'c'
println(bytes_to_hex_string(ctx.finialize())) // or `ctx.compute()`
// 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(sm3sum(input.to_bytes())))
println(bytes_to_hex_string(sm3(input.to_bytes())))
// => fc2b31896629e88652ca1e3be449ec7ec93f7e5e29769f273fb973bc1858c66d
//buffered
let ctx = SM3Context::make()
ctx.update(b"\x61") // 'a'
ctx.update(b"\x62") // 'b'
ctx.update(b"\x63") // 'c'
println(bytes_to_hex_string(ctx.finialize()))
let ctx = SM3Context::new()
ctx.update(b"a")
ctx.update(b"b")
ctx.update(b"c")
println(bytes_to_hex_string(ctx.finalize()))
// => 66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0
```
24 changes: 5 additions & 19 deletions crypto/crypto.mbti
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
package moonbitlang/x/crypto

// Values
fn arr_u32_to_u8be(Array[UInt]) -> Bytes

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

fn bytes_to_hex_string(Bytes) -> String

fn bytes_to_iter(Bytes) -> Iter[Byte]

fn chacha12(FixedArray[UInt], UInt, Bytes, ~nonce : UInt = ..) -> Bytes!

fn chacha20(FixedArray[UInt], UInt, Bytes, ~nonce : UInt = ..) -> Bytes!

fn chacha8(FixedArray[UInt], UInt, Bytes, ~nonce : UInt = ..) -> Bytes!

fn md5sum(Bytes) -> Bytes

fn rotate_left(Int, Int) -> Int
fn md5(Bytes) -> Bytes

fn sha1(Bytes) -> Bytes

fn sm3sum(Bytes) -> Bytes

fn sm3sum_from_iter(Iter[Byte]) -> Bytes

fn u32_to_u8be(UInt) -> Array[Byte]

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

Expand All @@ -38,15 +24,15 @@ type MD5Context
impl MD5Context {
compute(Self) -> Bytes
finalize(Self) -> Bytes
make() -> Self
new() -> Self
update(Self, Bytes) -> Unit
}

type SM3Context
impl SM3Context {
compute(Self) -> Bytes
finalize(Self) -> Bytes
make() -> Self
new() -> Self
update(Self, Bytes) -> Unit
update_from_iter(Self, Iter[Byte]) -> Unit
}
Expand Down
12 changes: 6 additions & 6 deletions crypto/md5.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn MD5Context::finalize(self : MD5Context) -> Bytes {
}

/// Instantiate a MD5 context
pub fn MD5Context::make() -> MD5Context {
pub fn MD5Context::new() -> MD5Context {
padding[0] = b'\x80'
{
state: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476],
Expand Down Expand Up @@ -231,27 +231,27 @@ fn md5_transform(state : FixedArray[UInt], input : FixedArray[UInt]) -> Unit {
/// Compute the MD5 digest of some `data` based on [RFC1321](https://www.ietf.org/rfc/rfc1321.txt).
/// - Note that MD5 is considered _cryptographically broken_.
/// Unless mandated, more secure alternatives should be preferred.
pub fn md5sum(data : Bytes) -> Bytes {
let ctx = MD5Context::make()
pub fn md5(data : Bytes) -> Bytes {
let ctx = MD5Context::new()
md5_update(ctx, data)
ctx.compute()
}

test "md5_wb" {
let hash = md5sum("The quick brown fox jumps over the lazy dog".to_bytes())
let hash = md5("The quick brown fox jumps over the lazy dog".to_bytes())
inspect!(
bytes_to_hex_string(hash),
content="b0986ae6ee1eefee8a4a399090126837",
)
}

test {
let ctx = MD5Context::make()
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.compute())
let ctx = MD5Context::make()
let ctx = MD5Context::new()
md5_update(ctx, b"\x61\x62\x63")
let res2 = bytes_to_hex_string(ctx.compute())
@test.eq!(res1, res2)
Expand Down
4 changes: 2 additions & 2 deletions crypto/md5_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

fn md5test(s : String) -> String {
bytes_to_hex_string(@crypto.md5sum(s.to_bytes()))
bytes_to_hex_string(@crypto.md5(s.to_bytes()))
}

test "md5_rfc1321" { // testsuites in RFC1321
Expand Down Expand Up @@ -46,7 +46,7 @@ test "md5_additional" { // Additional testsuites
for index = 0; index < 1000; index = index + 1 {
a[index] = (index % 256).to_byte()
}
let hash = @crypto.md5sum(Bytes::from_array(a))
let hash = @crypto.md5(Bytes::from_array(a))
inspect!(
bytes_to_hex_string(hash),
content="cbecbdb0fdd5cec1e242493b6008cc79",
Expand Down
Loading

0 comments on commit c2f585b

Please sign in to comment.