Skip to content

Commit

Permalink
Fix:add sha512's 224 and 256 byte truncated versions (#240)
Browse files Browse the repository at this point in the history
* Fix:add sha512's 224 and 256 byte truncated versions

* add:missing hmac test cases
  • Loading branch information
mertakman authored Dec 25, 2024
1 parent c116afa commit 9c8cba8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
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

0 comments on commit 9c8cba8

Please sign in to comment.