add crypto sha1, sha256, sha512

This commit is contained in:
tsingbx
2024-07-31 13:56:42 +08:00
parent 36072584d0
commit 2d7958f726
9 changed files with 310 additions and 0 deletions

11
_cmptest/sha1demo/sha1.go Normal file
View File

@@ -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))
}

View File

@@ -0,0 +1,11 @@
package main
import (
"crypto/sha256"
"fmt"
)
func main() {
sum := sha256.Sum256([]byte("hello world\n"))
fmt.Printf("%x", sum)
}

View File

@@ -0,0 +1,11 @@
package main
import (
"crypto/sha512"
"fmt"
)
func main() {
sum := sha512.Sum512([]byte("hello world\n"))
fmt.Printf("%x", sum)
}

View File

@@ -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": {},

View File

@@ -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
}

View File

@@ -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[:]...)
}

View File

@@ -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
}

View File

@@ -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[:]...)
}

View File

@@ -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")
}