diff --git a/README.md b/README.md index 10d12960..4766ac6d 100644 --- a/README.md +++ b/README.md @@ -305,6 +305,9 @@ Here are the Go packages that can be imported correctly: * [hash/crc32](https://pkg.go.dev/hash/crc32) (partially) * [hash/crc64](https://pkg.go.dev/hash/crc64) * [crypto/md5](https://pkg.go.dev/crypto/md5) +* [crypto/sha1](https://pkg.go.dev/crypto/sha1) +* [crypto/sha256](https://pkg.go.dev/crypto/sha256) +* [crypto/sha512](https://pkg.go.dev/crypto/sha512) (partially) * [crypto/rand](https://pkg.go.dev/crypto/rand) (partially) * [regexp](https://pkg.go.dev/regexp) * [regexp/syntax](https://pkg.go.dev/regexp/syntax) diff --git a/internal/lib/crypto/md5/md5.go b/internal/lib/crypto/md5/md5.go index 9baf0419..7865000e 100644 --- a/internal/lib/crypto/md5/md5.go +++ b/internal/lib/crypto/md5/md5.go @@ -18,6 +18,7 @@ package md5 // llgo:skipall import ( + "crypto" "hash" "unsafe" @@ -25,6 +26,10 @@ import ( "github.com/goplus/llgo/c/openssl" ) +func init() { + crypto.RegisterHash(crypto.MD5, New) +} + // The blocksize of MD5 in bytes. const BlockSize = 64 diff --git a/internal/lib/crypto/sha1/sha1.go b/internal/lib/crypto/sha1/sha1.go index 93637a51..d804ef10 100644 --- a/internal/lib/crypto/sha1/sha1.go +++ b/internal/lib/crypto/sha1/sha1.go @@ -2,6 +2,7 @@ package sha1 // llgo:skipall import ( + "crypto" "hash" "unsafe" @@ -9,6 +10,10 @@ import ( "github.com/goplus/llgo/c/openssl" ) +func init() { + crypto.RegisterHash(crypto.SHA1, New) +} + // The blocksize of SHA-1 in bytes. const BlockSize = 64 diff --git a/internal/lib/crypto/sha256/sha224.go b/internal/lib/crypto/sha256/sha224.go index 2a9d5a70..79d9d0da 100644 --- a/internal/lib/crypto/sha256/sha224.go +++ b/internal/lib/crypto/sha256/sha224.go @@ -1,12 +1,16 @@ package sha256 import ( + "hash" "unsafe" "github.com/goplus/llgo/c" "github.com/goplus/llgo/c/openssl" ) +// The size of a SHA224 checksum in bytes. +const Size224 = 28 + type digest224 struct { ctx openssl.SHA224_CTX } @@ -29,3 +33,16 @@ func (d *digest224) Sum(in []byte) []byte { d.ctx.Final((*byte)(unsafe.Pointer(hash))) return append(in, hash[:]...) } + +// New224 returns a new hash.Hash computing the SHA224 checksum. +func New224() hash.Hash { + d := new(digest224) + d.ctx.Init() + return d +} + +// Sum224 returns the SHA224 checksum of the data. +func Sum224(data []byte) (ret [Size224]byte) { + openssl.SHA224Bytes(data, &ret[0]) + return +} diff --git a/internal/lib/crypto/sha256/sha256.go b/internal/lib/crypto/sha256/sha256.go index 480325be..4e4dcb00 100644 --- a/internal/lib/crypto/sha256/sha256.go +++ b/internal/lib/crypto/sha256/sha256.go @@ -2,6 +2,7 @@ package sha256 // llgo:skipall import ( + "crypto" "hash" "unsafe" @@ -9,15 +10,17 @@ import ( "github.com/goplus/llgo/c/openssl" ) +func init() { + crypto.RegisterHash(crypto.SHA224, New224) + crypto.RegisterHash(crypto.SHA256, New) +} + // The blocksize of SHA256 and SHA224 in bytes. const BlockSize = 64 // The size of a SHA256 checksum in bytes. const Size = 32 -// The size of a SHA224 checksum in bytes. -const Size224 = 28 - type digest256 struct { ctx openssl.SHA256_CTX } @@ -48,19 +51,6 @@ func New() hash.Hash { return d } -// New224 returns a new hash.Hash computing the SHA224 checksum. -func New224() hash.Hash { - d := new(digest224) - d.ctx.Init() - return d -} - -// Sum224 returns the SHA224 checksum of the data. -func Sum224(data []byte) (ret [Size224]byte) { - openssl.SHA224Bytes(data, &ret[0]) - return -} - // Sum256 returns the SHA256 checksum of the data. func Sum256(data []byte) (ret [Size]byte) { openssl.SHA256Bytes(data, &ret[0]) diff --git a/internal/lib/crypto/sha512/sha384.go b/internal/lib/crypto/sha512/sha384.go index ab69e872..ecfd18d7 100644 --- a/internal/lib/crypto/sha512/sha384.go +++ b/internal/lib/crypto/sha512/sha384.go @@ -1,6 +1,7 @@ package sha512 import ( + "hash" "unsafe" "github.com/goplus/llgo/c" @@ -29,3 +30,14 @@ func (d *digest384) Sum(in []byte) []byte { d.ctx.Final((*byte)(unsafe.Pointer(hash))) return append(in, hash[:]...) } + +func New384() hash.Hash { + d := new(digest384) + d.ctx.Init() + return d +} + +func Sum384(data []byte) (ret [Size384]byte) { + openssl.SHA384Bytes(data, &ret[0]) + return +} diff --git a/internal/lib/crypto/sha512/sha512.go b/internal/lib/crypto/sha512/sha512.go index 0cdf6ec9..57790485 100644 --- a/internal/lib/crypto/sha512/sha512.go +++ b/internal/lib/crypto/sha512/sha512.go @@ -2,6 +2,7 @@ package sha512 // llgo:skipall import ( + "crypto" "hash" "unsafe" @@ -9,6 +10,13 @@ import ( "github.com/goplus/llgo/c/openssl" ) +func init() { + crypto.RegisterHash(crypto.SHA384, New384) + crypto.RegisterHash(crypto.SHA512, New) + crypto.RegisterHash(crypto.SHA512_224, New512_224) + crypto.RegisterHash(crypto.SHA512_256, New512_256) +} + const ( // Size is the size, in bytes, of a SHA-512 checksum. Size = 64 @@ -56,34 +64,23 @@ func New() hash.Hash { return d } -func New384() hash.Hash { - d := new(digest384) - d.ctx.Init() - return d -} - -func Sum384(data []byte) (ret [Size384]byte) { - openssl.SHA384Bytes(data, &ret[0]) - return -} - func Sum512(data []byte) (ret [Size]byte) { openssl.SHA512Bytes(data, &ret[0]) return } func New512_224() hash.Hash { - panic("todo: New512_224") -} - -func New512_256() hash.Hash { - panic("todo: New512_256") + panic("todo: crypto/sha512.New512_224") } func Sum512_224(data []byte) [Size224]byte { - panic("todo: Sum512_224") + panic("todo: crypto/sha512.Sum512_224") +} + +func New512_256() hash.Hash { + panic("todo: crypto/sha512.New512_256") } func Sum512_256(data []byte) [Size256]byte { - panic("todo: Sum512_256") + panic("todo: crypto/sha512.Sum512_256") }