Skip to content

Commit 9c8cba8

Browse files
authored
Fix:add sha512's 224 and 256 byte truncated versions (#240)
* Fix:add sha512's 224 and 256 byte truncated versions * add:missing hmac test cases
1 parent c116afa commit 9c8cba8

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

hash.go

+32
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ func SHA512(p []byte) (sum [64]byte) {
8181
return
8282
}
8383

84+
func SHA512_224(p []byte) (sum [28]byte) {
85+
if !hashOneShot(crypto.SHA512_224, p, sum[:]) {
86+
panic("openssl: SHA512 failed")
87+
}
88+
return
89+
}
90+
91+
func SHA512_256(p []byte) (sum [32]byte) {
92+
if !hashOneShot(crypto.SHA512_256, p, sum[:]) {
93+
panic("openssl: SHA512_256 failed")
94+
}
95+
return
96+
}
97+
8498
// cacheHashSupported is a cache of crypto.Hash support.
8599
var cacheHashSupported sync.Map
86100

@@ -171,6 +185,16 @@ func NewSHA512() hash.Hash {
171185
return newEvpHash(crypto.SHA512)
172186
}
173187

188+
// NewSHA512_224 returns a new SHA512_224 hash.
189+
func NewSHA512_224() hash.Hash {
190+
return newEvpHash(crypto.SHA512_224)
191+
}
192+
193+
// NewSHA512_256 returns a new SHA512_256 hash.
194+
func NewSHA512_256() hash.Hash {
195+
return newEvpHash(crypto.SHA512_256)
196+
}
197+
174198
// NewSHA3_224 returns a new SHA3-224 hash.
175199
func NewSHA3_224() hash.Hash {
176200
return newEvpHash(crypto.SHA3_224)
@@ -383,6 +407,10 @@ func (d *evpHash) AppendBinary(buf []byte) ([]byte, error) {
383407
appender = (*sha512State)(state)
384408
case crypto.SHA512:
385409
appender = (*sha512State)(state)
410+
case crypto.SHA512_224:
411+
appender = (*sha512State)(state)
412+
case crypto.SHA512_256:
413+
appender = (*sha512State)(state)
386414
default:
387415
panic("openssl: unsupported hash function: " + strconv.Itoa(int(d.alg.ch)))
388416
}
@@ -421,6 +449,10 @@ func (d *evpHash) UnmarshalBinary(b []byte) error {
421449
unmarshaler = (*sha512State)(state)
422450
case crypto.SHA512:
423451
unmarshaler = (*sha512State)(state)
452+
case crypto.SHA512_224:
453+
unmarshaler = (*sha512State)(state)
454+
case crypto.SHA512_256:
455+
unmarshaler = (*sha512State)(state)
424456
default:
425457
panic("openssl: unsupported hash function: " + strconv.Itoa(int(d.alg.ch)))
426458
}

hash_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func cryptoToHash(h crypto.Hash) func() hash.Hash {
2828
return openssl.NewSHA384
2929
case crypto.SHA512:
3030
return openssl.NewSHA512
31+
case crypto.SHA512_224:
32+
return openssl.NewSHA512_224
33+
case crypto.SHA512_256:
34+
return openssl.NewSHA512_256
3135
case crypto.SHA3_224:
3236
return openssl.NewSHA3_224
3337
case crypto.SHA3_256:
@@ -48,6 +52,8 @@ var hashes = [...]crypto.Hash{
4852
crypto.SHA256,
4953
crypto.SHA384,
5054
crypto.SHA512,
55+
crypto.SHA512_224,
56+
crypto.SHA512_256,
5157
crypto.SHA3_224,
5258
crypto.SHA3_256,
5359
crypto.SHA3_384,
@@ -294,6 +300,14 @@ func TestHash_OneShot(t *testing.T) {
294300
b := openssl.SHA512(p)
295301
return b[:]
296302
}},
303+
{crypto.SHA512_224, func(p []byte) []byte {
304+
b := openssl.SHA512_224(p)
305+
return b[:]
306+
}},
307+
{crypto.SHA512_256, func(p []byte) []byte {
308+
b := openssl.SHA512_256(p)
309+
return b[:]
310+
}},
297311
{crypto.SHA3_224, func(p []byte) []byte {
298312
b := openssl.SHA3_224(p)
299313
return b[:]

hmac_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ func TestHMAC(t *testing.T) {
1818
{"sha256", openssl.NewSHA256},
1919
{"sha384", openssl.NewSHA384},
2020
{"sha512", openssl.NewSHA512},
21+
{"sha512_224", openssl.NewSHA512_224},
22+
{"sha512_256", openssl.NewSHA512_256},
2123
}
2224
for _, tt := range tests {
2325
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)