Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:add sha512's 224 and 256 byte truncated versions #240

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ func SHA512(p []byte) (sum [64]byte) {
return
}

func SHA512_224(p []byte) (sum [28]byte) {
if !hashOneShot(crypto.SHA512_224, p, sum[:]) {
panic("openssl: SHA512 failed")
}
return
}

func SHA512_256(p []byte) (sum [32]byte) {
if !hashOneShot(crypto.SHA512_256, p, sum[:]) {
panic("openssl: SHA512_256 failed")
}
return
}

// cacheHashSupported is a cache of crypto.Hash support.
var cacheHashSupported sync.Map

Expand Down Expand Up @@ -171,6 +185,16 @@ func NewSHA512() hash.Hash {
return newEvpHash(crypto.SHA512)
}

// NewSHA512_224 returns a new SHA512_224 hash.
func NewSHA512_224() hash.Hash {
return newEvpHash(crypto.SHA512_224)
}

// NewSHA512_256 returns a new SHA512_256 hash.
func NewSHA512_256() hash.Hash {
return newEvpHash(crypto.SHA512_256)
}

// NewSHA3_224 returns a new SHA3-224 hash.
func NewSHA3_224() hash.Hash {
return newEvpHash(crypto.SHA3_224)
Expand Down Expand Up @@ -383,6 +407,10 @@ func (d *evpHash) AppendBinary(buf []byte) ([]byte, error) {
appender = (*sha512State)(state)
case crypto.SHA512:
appender = (*sha512State)(state)
case crypto.SHA512_224:
appender = (*sha512State)(state)
case crypto.SHA512_256:
appender = (*sha512State)(state)
default:
panic("openssl: unsupported hash function: " + strconv.Itoa(int(d.alg.ch)))
}
Expand Down Expand Up @@ -421,6 +449,10 @@ func (d *evpHash) UnmarshalBinary(b []byte) error {
unmarshaler = (*sha512State)(state)
case crypto.SHA512:
unmarshaler = (*sha512State)(state)
case crypto.SHA512_224:
unmarshaler = (*sha512State)(state)
case crypto.SHA512_256:
unmarshaler = (*sha512State)(state)
default:
panic("openssl: unsupported hash function: " + strconv.Itoa(int(d.alg.ch)))
}
Expand Down
14 changes: 14 additions & 0 deletions hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func cryptoToHash(h crypto.Hash) func() hash.Hash {
return openssl.NewSHA384
case crypto.SHA512:
return openssl.NewSHA512
case crypto.SHA512_224:
return openssl.NewSHA512_224
case crypto.SHA512_256:
return openssl.NewSHA512_256
case crypto.SHA3_224:
return openssl.NewSHA3_224
case crypto.SHA3_256:
Expand All @@ -48,6 +52,8 @@ var hashes = [...]crypto.Hash{
crypto.SHA256,
crypto.SHA384,
crypto.SHA512,
crypto.SHA512_224,
crypto.SHA512_256,
crypto.SHA3_224,
crypto.SHA3_256,
crypto.SHA3_384,
Expand Down Expand Up @@ -294,6 +300,14 @@ func TestHash_OneShot(t *testing.T) {
b := openssl.SHA512(p)
return b[:]
}},
{crypto.SHA512_224, func(p []byte) []byte {
b := openssl.SHA512_224(p)
return b[:]
}},
{crypto.SHA512_256, func(p []byte) []byte {
b := openssl.SHA512_256(p)
return b[:]
}},
{crypto.SHA3_224, func(p []byte) []byte {
b := openssl.SHA3_224(p)
return b[:]
Expand Down
2 changes: 2 additions & 0 deletions hmac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func TestHMAC(t *testing.T) {
{"sha256", openssl.NewSHA256},
{"sha384", openssl.NewSHA384},
{"sha512", openssl.NewSHA512},
{"sha512_224", openssl.NewSHA512_224},
{"sha512_256", openssl.NewSHA512_256},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading