diff --git a/README.md b/README.md index f9c3fcf4..a6963999 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,7 @@ Here are the Go packages that can be imported correctly: * [encoding/base32](https://pkg.go.dev/encoding/base32) * [encoding/base64](https://pkg.go.dev/encoding/base64) * [encoding/csv](https://pkg.go.dev/encoding/csv) +* [crypto/md5](https://pkg.go.dev/crypto/md5) * [regexp](https://pkg.go.dev/regexp) * [regexp/syntax](https://pkg.go.dev/regexp/syntax) diff --git a/_cmptest/_md5demo/md5.go b/_cmptest/md5demo/md5.go similarity index 100% rename from _cmptest/_md5demo/md5.go rename to _cmptest/md5demo/md5.go diff --git a/_demo/cmd5demo/md5.go b/_demo/cmd5demo/md5.go index 1c08a192..26db4527 100644 --- a/_demo/cmd5demo/md5.go +++ b/_demo/cmd5demo/md5.go @@ -13,6 +13,6 @@ func main() { md5.Init() md5.UpdateString("The fog is getting thicker!") md5.UpdateString("And Leon's getting laaarger!") - md5.Final(unsafe.Pointer(unsafe.SliceData(h))) + md5.Final(unsafe.SliceData(h)) fmt.Printf("%x", h) } diff --git a/c/openssl/md5.go b/c/openssl/md5.go index b828da9f..99aac0c3 100644 --- a/c/openssl/md5.go +++ b/c/openssl/md5.go @@ -61,22 +61,22 @@ func (c *MD5_CTX) UpdateString(data string) c.Int { // OSSL_DEPRECATEDIN_3_0 int MD5_Final(unsigned char *md, MD5_CTX *c); // //go:linkname md5Final C.MD5_Final -func md5Final(md unsafe.Pointer, c *MD5_CTX) c.Int +func md5Final(md *byte, c *MD5_CTX) c.Int -func (c *MD5_CTX) Final(md unsafe.Pointer) c.Int { +func (c *MD5_CTX) Final(md *byte) c.Int { return md5Final(md, c) } // OSSL_DEPRECATEDIN_3_0 unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md); // //go:linkname MD5 C.MD5 -func MD5(data unsafe.Pointer, n uintptr, md unsafe.Pointer) unsafe.Pointer +func MD5(data unsafe.Pointer, n uintptr, md *byte) *byte -func MD5Bytes(data []byte, md unsafe.Pointer) unsafe.Pointer { +func MD5Bytes(data []byte, md *byte) *byte { return MD5(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data)), md) } -func MD5String(data string, md unsafe.Pointer) unsafe.Pointer { +func MD5String(data string, md *byte) *byte { return MD5(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data)), md) } diff --git a/cl/import.go b/cl/import.go index 43d5aa0e..6729b7cf 100644 --- a/cl/import.go +++ b/cl/import.go @@ -590,9 +590,8 @@ func ignoreName(name string) bool { */ const internal = "internal/" return (strings.HasPrefix(name, internal) && !supportedInternal(name[len(internal):])) || - strings.HasPrefix(name, "crypto/") || strings.HasPrefix(name, "runtime/") || - strings.HasPrefix(name, "arena.") || strings.HasPrefix(name, "maps.") || - strings.HasPrefix(name, "plugin.") + strings.HasPrefix(name, "runtime/") || strings.HasPrefix(name, "arena.") || + strings.HasPrefix(name, "maps.") || strings.HasPrefix(name, "plugin.") } func supportedInternal(name string) bool { diff --git a/internal/build/build.go b/internal/build/build.go index 8854fb36..ce75c02a 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -755,6 +755,7 @@ func findDylibDep(exe, lib string) string { type none struct{} var hasAltPkg = map[string]none{ + "crypto/md5": {}, "fmt": {}, "internal/abi": {}, "internal/bytealg": {}, diff --git a/internal/lib/crypto/md5/md5.go b/internal/lib/crypto/md5/md5.go new file mode 100644 index 00000000..9baf0419 --- /dev/null +++ b/internal/lib/crypto/md5/md5.go @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package md5 + +// llgo:skipall +import ( + "hash" + "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/openssl" +) + +// The blocksize of MD5 in bytes. +const BlockSize = 64 + +// The size of an MD5 checksum in bytes. +const Size = 16 + +type digest struct { + ctx openssl.MD5_CTX +} + +func (d *digest) Size() int { return Size } + +func (d *digest) BlockSize() int { return BlockSize } + +func (d *digest) Reset() { + d.ctx.Init() +} + +func (d *digest) Write(p []byte) (nn int, err error) { + d.ctx.UpdateBytes(p) + return len(p), nil +} + +func (d *digest) Sum(in []byte) []byte { + hash := (*[Size]byte)(c.Alloca(Size)) + d.ctx.Final((*byte)(unsafe.Pointer(hash))) + return append(in, hash[:]...) +} + +func New() hash.Hash { + d := new(digest) + d.ctx.Init() + return d +} + +func Sum(data []byte) (ret [Size]byte) { + openssl.MD5Bytes(data, &ret[0]) + return +}