From a3ff845a1481ee4faf1bb0c38e29d70d3dd1aa45 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Wed, 31 Jul 2024 01:27:08 +0800 Subject: [PATCH] c/openssl: rand --- _cmptest/_randcrypt/rand.go | 18 +++++ {_demo => c/openssl/_demo}/cmd5demo/md5.go | 0 c/openssl/_demo/cranddemo/rand.go | 17 +++++ {_demo => c/openssl/_demo}/cshademo/sha.go | 0 c/openssl/openssl.go | 2 + c/openssl/rand.go | 80 ++++++++++++++++++++++ 6 files changed, 117 insertions(+) create mode 100644 _cmptest/_randcrypt/rand.go rename {_demo => c/openssl/_demo}/cmd5demo/md5.go (100%) create mode 100644 c/openssl/_demo/cranddemo/rand.go rename {_demo => c/openssl/_demo}/cshademo/sha.go (100%) create mode 100644 c/openssl/rand.go diff --git a/_cmptest/_randcrypt/rand.go b/_cmptest/_randcrypt/rand.go new file mode 100644 index 00000000..c8d52e63 --- /dev/null +++ b/_cmptest/_randcrypt/rand.go @@ -0,0 +1,18 @@ +package main + +import ( + "crypto/rand" + "fmt" +) + +func main() { + c := 10 + b := make([]byte, c) + _, err := rand.Read(b) + if err != nil { + fmt.Println("error:", err) + return + } + // The slice should now contain random bytes instead of only zeroes. + fmt.Printf("%x\n", b) +} diff --git a/_demo/cmd5demo/md5.go b/c/openssl/_demo/cmd5demo/md5.go similarity index 100% rename from _demo/cmd5demo/md5.go rename to c/openssl/_demo/cmd5demo/md5.go diff --git a/c/openssl/_demo/cranddemo/rand.go b/c/openssl/_demo/cranddemo/rand.go new file mode 100644 index 00000000..4fc3a803 --- /dev/null +++ b/c/openssl/_demo/cranddemo/rand.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + + "github.com/goplus/llgo/c/openssl" +) + +func main() { + b := make([]byte, 10) + + openssl.RANDBytes(b) + fmt.Printf("%x\n", b) + + openssl.RANDPrivBytes(b) + fmt.Printf("%x\n", b) +} diff --git a/_demo/cshademo/sha.go b/c/openssl/_demo/cshademo/sha.go similarity index 100% rename from _demo/cshademo/sha.go rename to c/openssl/_demo/cshademo/sha.go diff --git a/c/openssl/openssl.go b/c/openssl/openssl.go index cde02ca5..491f61fe 100644 --- a/c/openssl/openssl.go +++ b/c/openssl/openssl.go @@ -16,6 +16,8 @@ package openssl +// ----------------------------------------------------------------------------- + const ( LLGoPackage = "link: $(pkg-config --libs openssl); -lssl -lcrypto" ) diff --git a/c/openssl/rand.go b/c/openssl/rand.go new file mode 100644 index 00000000..f9ad6338 --- /dev/null +++ b/c/openssl/rand.go @@ -0,0 +1,80 @@ +/* + * 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" +) + +// ----------------------------------------------------------------------------- + +// int RAND_bytes(unsigned char *buf, int num); +// +//go:linkname RANDBufferWithLen C.RAND_bytes +func RANDBufferWithLen(buf *byte, num c.Int) c.Int + +func RANDBytes(buf []byte) c.Int { + return RANDBufferWithLen(unsafe.SliceData(buf), c.Int(len(buf))) +} + +// int RAND_priv_bytes(unsigned char *buf, int num); +// +//go:linkname RANDPrivBufferWithLen C.RAND_priv_bytes +func RANDPrivBufferWithLen(buf *byte, num c.Int) c.Int + +func RANDPrivBytes(buf []byte) c.Int { + return RANDPrivBufferWithLen(unsafe.SliceData(buf), c.Int(len(buf))) +} + +// void RAND_seed(const void *buf, int num); +// +//go:linkname RANDSeed C.RAND_seed +func RANDSeed(buf unsafe.Pointer, num c.Int) + +// void RAND_keep_random_devices_open(int keep); +// +//go:linkname RANDKeepRandomDevicesOpen C.RAND_keep_random_devices_open +func RANDKeepRandomDevicesOpen(keep c.Int) + +// int RAND_load_file(const char *file, long max_bytes); +// +//go:linkname RANDLoadFile C.RAND_load_file +func RANDLoadFile(file *c.Char, maxBytes c.Long) c.Int + +// int RAND_write_file(const char *file); +// +//go:linkname RANDWriteFile C.RAND_write_file +func RANDWriteFile(file *c.Char) c.Int + +// const char *RAND_file_name(char *file, size_t num); +// +//go:linkname RANDFileName C.RAND_file_name +func RANDFileName(file *c.Char, num uintptr) *c.Char + +// int RAND_status(void); +// +//go:linkname RANDStatus C.RAND_status +func RANDStatus() c.Int + +// int RAND_poll(void); +// +//go:linkname RANDPoll C.RAND_poll +func RANDPoll() c.Int + +// -----------------------------------------------------------------------------