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 6f206bf commit 6396d2a
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 167 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
```
26 changes: 5 additions & 21 deletions crypto/crypto.mbti
Original file line number Diff line number Diff line change
@@ -1,52 +1,36 @@
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

// Types and methods
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
22 changes: 11 additions & 11 deletions crypto/md5.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ pub fn MD5Context::update(self : MD5Context, data : Bytes) -> Unit {

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

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

/// compute MD5 digest from given context
pub fn MD5Context::compute(self : MD5Context) -> Bytes {
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 @@ -231,28 +231,28 @@ 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()
ctx.md5_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 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.compute())
let res2 = bytes_to_hex_string(ctx.md5_compute())
@test.eq!(res1, res2)
}
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 6396d2a

Please sign in to comment.