From 2d7958f726f0347dcede1c891ea6e99707f9221b Mon Sep 17 00:00:00 2001 From: tsingbx Date: Wed, 31 Jul 2024 13:56:42 +0800 Subject: [PATCH] add crypto sha1, sha256, sha512 --- _cmptest/sha1demo/sha1.go | 11 ++++ _cmptest/sha256demo/sha256.go | 11 ++++ _cmptest/sha512demo/sha512.go | 11 ++++ internal/build/build.go | 3 + internal/lib/crypto/sha1/sha1.go | 53 ++++++++++++++++ internal/lib/crypto/sha256/sha224.go | 31 ++++++++++ internal/lib/crypto/sha256/sha256.go | 69 +++++++++++++++++++++ internal/lib/crypto/sha512/sha384.go | 31 ++++++++++ internal/lib/crypto/sha512/sha512.go | 90 ++++++++++++++++++++++++++++ 9 files changed, 310 insertions(+) create mode 100644 _cmptest/sha1demo/sha1.go create mode 100644 _cmptest/sha256demo/sha256.go create mode 100644 _cmptest/sha512demo/sha512.go create mode 100644 internal/lib/crypto/sha1/sha1.go create mode 100644 internal/lib/crypto/sha256/sha224.go create mode 100644 internal/lib/crypto/sha256/sha256.go create mode 100644 internal/lib/crypto/sha512/sha384.go create mode 100644 internal/lib/crypto/sha512/sha512.go diff --git a/_cmptest/sha1demo/sha1.go b/_cmptest/sha1demo/sha1.go new file mode 100644 index 00000000..e2f0d0ed --- /dev/null +++ b/_cmptest/sha1demo/sha1.go @@ -0,0 +1,11 @@ +package main + +import ( + "crypto/sha1" + "fmt" +) + +func main() { + data := []byte("This page intentionally left blank.") + fmt.Printf("% x", sha1.Sum(data)) +} diff --git a/_cmptest/sha256demo/sha256.go b/_cmptest/sha256demo/sha256.go new file mode 100644 index 00000000..41e2eab7 --- /dev/null +++ b/_cmptest/sha256demo/sha256.go @@ -0,0 +1,11 @@ +package main + +import ( + "crypto/sha256" + "fmt" +) + +func main() { + sum := sha256.Sum256([]byte("hello world\n")) + fmt.Printf("%x", sum) +} diff --git a/_cmptest/sha512demo/sha512.go b/_cmptest/sha512demo/sha512.go new file mode 100644 index 00000000..eeff921f --- /dev/null +++ b/_cmptest/sha512demo/sha512.go @@ -0,0 +1,11 @@ +package main + +import ( + "crypto/sha512" + "fmt" +) + +func main() { + sum := sha512.Sum512([]byte("hello world\n")) + fmt.Printf("%x", sum) +} diff --git a/internal/build/build.go b/internal/build/build.go index d32c39e8..7e732be9 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -758,6 +758,9 @@ type none struct{} var hasAltPkg = map[string]none{ "crypto/md5": {}, + "crypto/sha1": {}, + "crypto/sha256": {}, + "crypto/sha512": {}, "crypto/rand": {}, "fmt": {}, "hash/crc32": {}, diff --git a/internal/lib/crypto/sha1/sha1.go b/internal/lib/crypto/sha1/sha1.go new file mode 100644 index 00000000..0779e917 --- /dev/null +++ b/internal/lib/crypto/sha1/sha1.go @@ -0,0 +1,53 @@ +package sha1 + +// llgo:skipall + +import ( + "hash" + "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/openssl" +) + +// The blocksize of SHA-1 in bytes. +const BlockSize = 64 + +// The size of a SHA-1 checksum in bytes. +const Size = 20 + +type digest struct { + ctx openssl.SHA_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[:]...) +} + +// New returns a new hash.Hash computing the SHA1 checksum. +func New() hash.Hash { + d := new(digest) + d.ctx.Init() + return d +} + +// Sum returns the SHA-1 checksum of the data. +func Sum(data []byte) (ret [Size]byte) { + openssl.SHA1Bytes(data, &ret[0]) + return +} diff --git a/internal/lib/crypto/sha256/sha224.go b/internal/lib/crypto/sha256/sha224.go new file mode 100644 index 00000000..2a9d5a70 --- /dev/null +++ b/internal/lib/crypto/sha256/sha224.go @@ -0,0 +1,31 @@ +package sha256 + +import ( + "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/openssl" +) + +type digest224 struct { + ctx openssl.SHA224_CTX +} + +func (d *digest224) Size() int { return Size224 } + +func (d *digest224) BlockSize() int { return BlockSize } + +func (d *digest224) Reset() { + d.ctx.Init() +} + +func (d *digest224) Write(p []byte) (nn int, err error) { + d.ctx.UpdateBytes(p) + return len(p), nil +} + +func (d *digest224) Sum(in []byte) []byte { + hash := (*[Size]byte)(c.Alloca(Size)) + d.ctx.Final((*byte)(unsafe.Pointer(hash))) + return append(in, hash[:]...) +} diff --git a/internal/lib/crypto/sha256/sha256.go b/internal/lib/crypto/sha256/sha256.go new file mode 100644 index 00000000..b96dfd55 --- /dev/null +++ b/internal/lib/crypto/sha256/sha256.go @@ -0,0 +1,69 @@ +package sha256 + +// llgo:skipall + +import ( + "hash" + "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/openssl" +) + +// 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 +} + +func (d *digest256) Size() int { return Size } + +func (d *digest256) BlockSize() int { return BlockSize } + +func (d *digest256) Reset() { + d.ctx.Init() +} + +func (d *digest256) Write(p []byte) (nn int, err error) { + d.ctx.UpdateBytes(p) + return len(p), nil +} + +func (d *digest256) Sum(in []byte) []byte { + hash := (*[Size]byte)(c.Alloca(Size)) + d.ctx.Final((*byte)(unsafe.Pointer(hash))) + return append(in, hash[:]...) +} + +// New returns a new hash.Hash computing the SHA256 checksum. +func New() hash.Hash { + d := new(digest256) + d.ctx.Init() + 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]) + return +} diff --git a/internal/lib/crypto/sha512/sha384.go b/internal/lib/crypto/sha512/sha384.go new file mode 100644 index 00000000..ab69e872 --- /dev/null +++ b/internal/lib/crypto/sha512/sha384.go @@ -0,0 +1,31 @@ +package sha512 + +import ( + "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/openssl" +) + +type digest384 struct { + ctx openssl.SHA384_CTX +} + +func (d *digest384) Size() int { return Size384 } + +func (d *digest384) BlockSize() int { return BlockSize } + +func (d *digest384) Reset() { + d.ctx.Init() +} + +func (d *digest384) Write(p []byte) (nn int, err error) { + d.ctx.UpdateBytes(p) + return len(p), nil +} + +func (d *digest384) Sum(in []byte) []byte { + hash := (*[Size]byte)(c.Alloca(Size)) + d.ctx.Final((*byte)(unsafe.Pointer(hash))) + return append(in, hash[:]...) +} diff --git a/internal/lib/crypto/sha512/sha512.go b/internal/lib/crypto/sha512/sha512.go new file mode 100644 index 00000000..c566a64a --- /dev/null +++ b/internal/lib/crypto/sha512/sha512.go @@ -0,0 +1,90 @@ +package sha512 + +// llgo:skipall + +import ( + "hash" + "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/openssl" +) + +const ( + // Size is the size, in bytes, of a SHA-512 checksum. + Size = 64 + + // Size224 is the size, in bytes, of a SHA-512/224 checksum. + Size224 = 28 + + // Size256 is the size, in bytes, of a SHA-512/256 checksum. + Size256 = 32 + + // Size384 is the size, in bytes, of a SHA-384 checksum. + Size384 = 48 + + // BlockSize is the block size, in bytes, of the SHA-512/224, + // SHA-512/256, SHA-384 and SHA-512 hash functions. + BlockSize = 128 +) + +type digest512 struct { + ctx openssl.SHA512_CTX +} + +func (d *digest512) Size() int { return Size } + +func (d *digest512) BlockSize() int { return BlockSize } + +func (d *digest512) Reset() { + d.ctx.Init() +} + +func (d *digest512) Write(p []byte) (nn int, err error) { + d.ctx.UpdateBytes(p) + return len(p), nil +} + +func (d *digest512) 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(digest512) + d.ctx.Init() + 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") +} + +func Sum512_224(data []byte) [Size224]byte { + panic("todo: Sum512_224") +} + +func Sum512_256(data []byte) [Size256]byte { + panic("todo: Sum512_256") +}