diff --git a/_cmptest/_md5demo/md5.go b/_cmptest/_md5demo/md5.go new file mode 100644 index 00000000..dbee9e66 --- /dev/null +++ b/_cmptest/_md5demo/md5.go @@ -0,0 +1,14 @@ +package main + +import ( + "crypto/md5" + "fmt" + "io" +) + +func main() { + h := md5.New() + io.WriteString(h, "The fog is getting thicker!") + io.WriteString(h, "And Leon's getting laaarger!") + fmt.Printf("%x", h.Sum(nil)) +} diff --git a/_demo/cmd5demo/md5.go b/_demo/cmd5demo/md5.go new file mode 100644 index 00000000..1c08a192 --- /dev/null +++ b/_demo/cmd5demo/md5.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "unsafe" + + "github.com/goplus/llgo/c/openssl" +) + +func main() { + var md5 openssl.MD5_CTX + var h = make([]byte, openssl.MD5_LBLOCK) + md5.Init() + md5.UpdateString("The fog is getting thicker!") + md5.UpdateString("And Leon's getting laaarger!") + md5.Final(unsafe.Pointer(unsafe.SliceData(h))) + fmt.Printf("%x", h) +} diff --git a/c/openssl/md5.go b/c/openssl/md5.go new file mode 100644 index 00000000..b828da9f --- /dev/null +++ b/c/openssl/md5.go @@ -0,0 +1,85 @@ +/* + * 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 openssl + +import ( + "unsafe" + + "github.com/goplus/llgo/c" +) + +// ----------------------------------------------------------------------------- + +const ( + MD5_CBLOCK = 64 + MD5_LBLOCK = MD5_CBLOCK / 4 +) + +// ----------------------------------------------------------------------------- + +type MD5_LONG = c.Uint + +type MD5_CTX struct { + A, B, C, D MD5_LONG + Nl, Nh MD5_LONG + Data [MD5_LBLOCK]MD5_LONG + Num c.Uint +} + +// OSSL_DEPRECATEDIN_3_0 int MD5_Init(MD5_CTX *c); +// +// llgo:link (*MD5_CTX).Init C.MD5_Init +func (c *MD5_CTX) Init() c.Int { return 0 } + +// OSSL_DEPRECATEDIN_3_0 int MD5_Update(MD5_CTX *c, const void *data, size_t len); +// +// llgo:link (*MD5_CTX).Update C.MD5_Update +func (c *MD5_CTX) Update(data unsafe.Pointer, n uintptr) c.Int { return 0 } + +func (c *MD5_CTX) UpdateBytes(data []byte) c.Int { + return c.Update(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data))) +} + +func (c *MD5_CTX) UpdateString(data string) c.Int { + return c.Update(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data))) +} + +// 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 (c *MD5_CTX) Final(md unsafe.Pointer) 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 MD5Bytes(data []byte, md unsafe.Pointer) unsafe.Pointer { + return MD5(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data)), md) +} + +func MD5String(data string, md unsafe.Pointer) unsafe.Pointer { + return MD5(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data)), md) +} + +// OSSL_DEPRECATEDIN_3_0 void MD5_Transform(MD5_CTX *c, const unsigned char *b); + +// ----------------------------------------------------------------------------- diff --git a/c/openssl/openssl.go b/c/openssl/openssl.go new file mode 100644 index 00000000..cde02ca5 --- /dev/null +++ b/c/openssl/openssl.go @@ -0,0 +1,23 @@ +/* + * 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 openssl + +const ( + LLGoPackage = "link: $(pkg-config --libs openssl); -lssl -lcrypto" +) + +// -----------------------------------------------------------------------------