Merge pull request #616 from tsingbx/main
add openssl sha1,sha256,sha512
This commit is contained in:
68
_demo/cshademo/sha.go
Normal file
68
_demo/cshademo/sha.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/goplus/llgo/c/openssl"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "His money is twice tainted:"
|
||||||
|
|
||||||
|
var sha1 openssl.SHA_CTX
|
||||||
|
sha1.Init()
|
||||||
|
sha1.UpdateString(str)
|
||||||
|
|
||||||
|
h1 := make([]byte, openssl.SHA_DIGEST_LENGTH)
|
||||||
|
sha1.Final(unsafe.SliceData(h1))
|
||||||
|
fmt.Printf("%x\n", h1)
|
||||||
|
|
||||||
|
h2 := make([]byte, openssl.SHA_DIGEST_LENGTH)
|
||||||
|
openssl.SHA1String(str, unsafe.SliceData(h2))
|
||||||
|
fmt.Printf("%x\n", h2)
|
||||||
|
|
||||||
|
var sha256 openssl.SHA256_CTX
|
||||||
|
sha256.Init()
|
||||||
|
sha256.UpdateString(str)
|
||||||
|
h3 := make([]byte, openssl.SHA256_DIGEST_LENGTH)
|
||||||
|
sha256.Final(unsafe.SliceData(h3))
|
||||||
|
fmt.Printf("%x\n", h3)
|
||||||
|
|
||||||
|
h4 := make([]byte, openssl.SHA256_DIGEST_LENGTH)
|
||||||
|
openssl.SHA256String(str, unsafe.SliceData(h4))
|
||||||
|
fmt.Printf("%x\n", h4)
|
||||||
|
|
||||||
|
var sha512 openssl.SHA512_CTX
|
||||||
|
sha512.Init()
|
||||||
|
sha512.UpdateString("His money is twice tainted:")
|
||||||
|
|
||||||
|
h5 := make([]byte, openssl.SHA512_DIGEST_LENGTH)
|
||||||
|
sha512.Final(unsafe.SliceData(h5))
|
||||||
|
fmt.Printf("%x\n", h5)
|
||||||
|
|
||||||
|
h6 := make([]byte, openssl.SHA512_DIGEST_LENGTH)
|
||||||
|
openssl.SHA512String(str, unsafe.SliceData(h6))
|
||||||
|
fmt.Printf("%x\n", h6)
|
||||||
|
|
||||||
|
var sha224 openssl.SHA224_CTX
|
||||||
|
sha224.Init()
|
||||||
|
sha224.UpdateString(str)
|
||||||
|
|
||||||
|
h7 := make([]byte, openssl.SHA224_DIGEST_LENGTH)
|
||||||
|
sha224.Final(unsafe.SliceData(h7))
|
||||||
|
fmt.Printf("%x\n", h7)
|
||||||
|
h8 := make([]byte, openssl.SHA224_DIGEST_LENGTH)
|
||||||
|
openssl.SHA224String(str, unsafe.SliceData(h8))
|
||||||
|
fmt.Printf("%x\n", h8)
|
||||||
|
|
||||||
|
var sha384 openssl.SHA384_CTX
|
||||||
|
sha384.Init()
|
||||||
|
sha384.UpdateString(str)
|
||||||
|
h9 := make([]byte, openssl.SHA384_DIGEST_LENGTH)
|
||||||
|
sha384.Final(unsafe.SliceData(h9))
|
||||||
|
fmt.Printf("%x\n", h9)
|
||||||
|
h10 := make([]byte, openssl.SHA384_DIGEST_LENGTH)
|
||||||
|
openssl.SHA384String(str, unsafe.SliceData(h10))
|
||||||
|
fmt.Printf("%x\n", h10)
|
||||||
|
}
|
||||||
31
c/openssl/sha.go
Normal file
31
c/openssl/sha.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package openssl
|
||||||
|
|
||||||
|
import "github.com/goplus/llgo/c"
|
||||||
|
|
||||||
|
const (
|
||||||
|
SHA_DIGEST_LENGTH = 20
|
||||||
|
SHA_LBLOCK = 16
|
||||||
|
SHA_CBLOCK = (SHA_LBLOCK * 4)
|
||||||
|
SHA_LAST_BLOCK = (SHA_CBLOCK - 8)
|
||||||
|
|
||||||
|
SHA256_CBLOCK = (SHA_LBLOCK * 4)
|
||||||
|
SHA256_192_DIGEST_LENGTH = 24
|
||||||
|
SHA224_DIGEST_LENGTH = 28
|
||||||
|
SHA256_DIGEST_LENGTH = 32
|
||||||
|
SHA384_DIGEST_LENGTH = 48
|
||||||
|
SHA512_DIGEST_LENGTH = 64
|
||||||
|
SHA512_CBLOCK = (SHA_LBLOCK * 8)
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
# if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
|
||||||
|
# define SHA_LONG64 unsigned __int64
|
||||||
|
# elif defined(__arch64__)
|
||||||
|
# define SHA_LONG64 unsigned long
|
||||||
|
# else
|
||||||
|
# define SHA_LONG64 unsigned long long
|
||||||
|
# endif
|
||||||
|
*/
|
||||||
|
type SHA_LONG64 = c.UlongLong
|
||||||
|
|
||||||
|
type SHA_LONG = c.Uint
|
||||||
59
c/openssl/sha1.go
Normal file
59
c/openssl/sha1.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package openssl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/goplus/llgo/c"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SHA_CTX struct {
|
||||||
|
H0, H1, H2, H3, H4 SHA_LONG
|
||||||
|
Nl, Nh SHA_LONG
|
||||||
|
Data [SHA_LBLOCK]SHA_LONG
|
||||||
|
Num c.Uint
|
||||||
|
}
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA1_Init(SHA_CTX *c);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA_CTX).Init C.SHA1_Init
|
||||||
|
func (c *SHA_CTX) Init() c.Int { return 0 }
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA1_Update(SHA_CTX *c, const void *data, size_t len);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA_CTX).Update C.SHA1_Update
|
||||||
|
func (c *SHA_CTX) Update(data unsafe.Pointer, n uintptr) c.Int { return 0 }
|
||||||
|
|
||||||
|
func (c *SHA_CTX) UpdateBytes(data []byte) c.Int {
|
||||||
|
return c.Update(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *SHA_CTX) UpdateString(data string) c.Int {
|
||||||
|
return c.Update(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA1_Final(unsigned char *md, SHA_CTX *c);
|
||||||
|
//
|
||||||
|
//go:linkname sha1Final C.SHA1_Final
|
||||||
|
func sha1Final(md *byte, c *SHA_CTX) c.Int
|
||||||
|
|
||||||
|
func (c *SHA_CTX) Final(md *byte) c.Int {
|
||||||
|
return sha1Final(md, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 void SHA1_Transform(SHA_CTX *c, const unsigned char *data);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA_CTX).Transform C.SHA1_Transform
|
||||||
|
func (c *SHA_CTX) Transform(data *byte) {}
|
||||||
|
|
||||||
|
// unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md);
|
||||||
|
//
|
||||||
|
//go:linkname SHA1 C.SHA1
|
||||||
|
func SHA1(data unsafe.Pointer, n uintptr, md *byte) *byte
|
||||||
|
|
||||||
|
func SHA1Bytes(data []byte, md *byte) *byte {
|
||||||
|
return SHA1(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data)), md)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SHA1String(data string, md *byte) *byte {
|
||||||
|
return SHA1(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data)), md)
|
||||||
|
}
|
||||||
101
c/openssl/sha256.go
Normal file
101
c/openssl/sha256.go
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
package openssl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/goplus/llgo/c"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SHA256_CTX struct {
|
||||||
|
H [8]SHA_LONG
|
||||||
|
Nl, Nh SHA_LONG
|
||||||
|
Data [SHA_LBLOCK]SHA_LONG
|
||||||
|
Num, MdLen c.Uint
|
||||||
|
}
|
||||||
|
|
||||||
|
type SHA224_CTX SHA256_CTX
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA224_Init(SHA256_CTX *c);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA224_CTX).Init C.SHA224_Init
|
||||||
|
func (c *SHA224_CTX) Init() c.Int { return 0 }
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA224_Update(SHA256_CTX *c, const void *data, size_t len);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA224_CTX).Update C.SHA224_Update
|
||||||
|
func (c *SHA224_CTX) Update(data unsafe.Pointer, n uintptr) c.Int { return 0 }
|
||||||
|
|
||||||
|
func (c *SHA224_CTX) UpdateBytes(data []byte) c.Int {
|
||||||
|
return c.Update(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *SHA224_CTX) UpdateString(data string) c.Int {
|
||||||
|
return c.Update(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA224_Final(unsigned char *md, SHA256_CTX *c);
|
||||||
|
//
|
||||||
|
//go:linkname sha224Final C.SHA224_Final
|
||||||
|
func sha224Final(md *byte, c *SHA224_CTX) c.Int
|
||||||
|
|
||||||
|
func (c *SHA224_CTX) Final(md *byte) c.Int {
|
||||||
|
return sha224Final(md, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA256_Init(SHA256_CTX *c);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA256_CTX).Init C.SHA256_Init
|
||||||
|
func (c *SHA256_CTX) Init() c.Int { return 0 }
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA256_Update(SHA256_CTX *c, const void *data, size_t len);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA256_CTX).Update C.SHA256_Update
|
||||||
|
func (c *SHA256_CTX) Update(data unsafe.Pointer, n uintptr) c.Int { return 0 }
|
||||||
|
|
||||||
|
func (c *SHA256_CTX) UpdateBytes(data []byte) c.Int {
|
||||||
|
return c.Update(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *SHA256_CTX) UpdateString(data string) c.Int {
|
||||||
|
return c.Update(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA256_Final(unsigned char *md, SHA256_CTX *c);
|
||||||
|
//
|
||||||
|
//go:linkname sha256Final C.SHA256_Final
|
||||||
|
func sha256Final(md *byte, c *SHA256_CTX) c.Int
|
||||||
|
|
||||||
|
func (c *SHA256_CTX) Final(md *byte) c.Int {
|
||||||
|
return sha256Final(md, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 void SHA256_Transform(SHA256_CTX *c, const unsigned char *data);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA256_CTX).Transform C.SHA256_Transform
|
||||||
|
func (c *SHA256_CTX) Transform(data *byte) {}
|
||||||
|
|
||||||
|
// unsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md);
|
||||||
|
//
|
||||||
|
//go:linkname SHA224 C.SHA224
|
||||||
|
func SHA224(data unsafe.Pointer, n uintptr, md *byte) *byte
|
||||||
|
|
||||||
|
func SHA224Bytes(data []byte, md *byte) *byte {
|
||||||
|
return SHA224(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data)), md)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SHA224String(data string, md *byte) *byte {
|
||||||
|
return SHA224(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data)), md)
|
||||||
|
}
|
||||||
|
|
||||||
|
// unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md);
|
||||||
|
//
|
||||||
|
//go:linkname SHA256 C.SHA256
|
||||||
|
func SHA256(data unsafe.Pointer, n uintptr, md *byte) *byte
|
||||||
|
|
||||||
|
func SHA256Bytes(data []byte, md *byte) *byte {
|
||||||
|
return SHA256(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data)), md)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SHA256String(data string, md *byte) *byte {
|
||||||
|
return SHA256(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data)), md)
|
||||||
|
}
|
||||||
99
c/openssl/sha512.go
Normal file
99
c/openssl/sha512.go
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
package openssl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/goplus/llgo/c"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SHA512_CTX struct {
|
||||||
|
H [8]SHA_LONG64
|
||||||
|
N1, Nh SHA_LONG64
|
||||||
|
D [SHA_LBLOCK]SHA_LONG64
|
||||||
|
Num, MdLen c.Uint
|
||||||
|
}
|
||||||
|
|
||||||
|
type SHA384_CTX SHA512_CTX
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA384_Init(SHA512_CTX *c);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA384_CTX).Init C.SHA384_Init
|
||||||
|
func (c *SHA384_CTX) Init() c.Int { return 0 }
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA384_Update(SHA512_CTX *c, const void *data, size_t len);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA384_CTX).Update C.SHA384_Update
|
||||||
|
func (c *SHA384_CTX) Update(data unsafe.Pointer, n uintptr) c.Int { return 0 }
|
||||||
|
|
||||||
|
func (c *SHA384_CTX) UpdateBytes(data []byte) c.Int {
|
||||||
|
return c.Update(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *SHA384_CTX) UpdateString(data string) c.Int {
|
||||||
|
return c.Update(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA384_Final(unsigned char *md, SHA512_CTX *c);
|
||||||
|
//
|
||||||
|
//go:linkname sha384Final C.SHA384_Final
|
||||||
|
func sha384Final(md *byte, c *SHA384_CTX) c.Int
|
||||||
|
|
||||||
|
func (c *SHA384_CTX) Final(md *byte) c.Int {
|
||||||
|
return sha384Final(md, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA512_Init(SHA512_CTX *c);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA512_CTX).Init C.SHA512_Init
|
||||||
|
func (c *SHA512_CTX) Init() c.Int { return 0 }
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA512_Update(SHA512_CTX *c, const void *data, size_t len);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA512_CTX).Update C.SHA512_Update
|
||||||
|
func (c *SHA512_CTX) Update(data unsafe.Pointer, n uintptr) c.Int { return 0 }
|
||||||
|
func (c *SHA512_CTX) UpdateBytes(data []byte) c.Int {
|
||||||
|
return c.Update(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data)))
|
||||||
|
}
|
||||||
|
func (c *SHA512_CTX) UpdateString(data string) c.Int {
|
||||||
|
return c.Update(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 int SHA512_Final(unsigned char *md, SHA512_CTX *c);
|
||||||
|
//
|
||||||
|
//go:linkname sha512Final C.SHA512_Final
|
||||||
|
func sha512Final(md *byte, c *SHA512_CTX) c.Int
|
||||||
|
|
||||||
|
func (c *SHA512_CTX) Final(md *byte) c.Int {
|
||||||
|
return sha512Final(md, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OSSL_DEPRECATEDIN_3_0 void SHA512_Transform(SHA512_CTX *c, const unsigned char *data);
|
||||||
|
//
|
||||||
|
// llgo:link (*SHA512_CTX).Transform C.SHA512_Transform
|
||||||
|
func (c *SHA512_CTX) Transform(data *byte) {}
|
||||||
|
|
||||||
|
// unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md);
|
||||||
|
//
|
||||||
|
//go:linkname SHA384 C.SHA384
|
||||||
|
func SHA384(data unsafe.Pointer, n uintptr, md *byte) *byte
|
||||||
|
|
||||||
|
func SHA384Bytes(data []byte, md *byte) *byte {
|
||||||
|
return SHA384(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data)), md)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SHA384String(data string, md *byte) *byte {
|
||||||
|
return SHA384(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data)), md)
|
||||||
|
}
|
||||||
|
|
||||||
|
// unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md);
|
||||||
|
//
|
||||||
|
//go:linkname SHA512 C.SHA512
|
||||||
|
func SHA512(data unsafe.Pointer, n uintptr, md *byte) *byte
|
||||||
|
|
||||||
|
func SHA512Bytes(data []byte, md *byte) *byte {
|
||||||
|
return SHA512(unsafe.Pointer(unsafe.SliceData(data)), uintptr(len(data)), md)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SHA512String(data string, md *byte) *byte {
|
||||||
|
return SHA512(unsafe.Pointer(unsafe.StringData(data)), uintptr(len(data)), md)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user