Skip to content

Commit f8d3e82

Browse files
committed
detect is md4 and md5 are supported
1 parent 72bf71c commit f8d3e82

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

evp.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ func cryptoHashToMD(ch crypto.Hash) (md C.GO_EVP_MD_PTR) {
7070
return C.go_openssl_EVP_md5_sha1()
7171
}
7272
}
73-
if !SupportsHash(ch) {
74-
return nil
75-
}
73+
sha3Defined := vMajor > 1 || (vMajor >= 1 && vMinor > 1) || (vMajor >= 1 && vMinor >= 1 && vPatch >= 1)
7674
switch ch {
7775
case crypto.MD4:
7876
return C.go_openssl_EVP_md4()
@@ -89,13 +87,21 @@ func cryptoHashToMD(ch crypto.Hash) (md C.GO_EVP_MD_PTR) {
8987
case crypto.SHA512:
9088
return C.go_openssl_EVP_sha512()
9189
case crypto.SHA3_224:
92-
return C.go_openssl_EVP_sha3_224()
90+
if sha3Defined {
91+
return C.go_openssl_EVP_sha3_224()
92+
}
9393
case crypto.SHA3_256:
94-
return C.go_openssl_EVP_sha3_256()
94+
if sha3Defined {
95+
return C.go_openssl_EVP_sha3_256()
96+
}
9597
case crypto.SHA3_384:
96-
return C.go_openssl_EVP_sha3_384()
98+
if sha3Defined {
99+
return C.go_openssl_EVP_sha3_384()
100+
}
97101
case crypto.SHA3_512:
98-
return C.go_openssl_EVP_sha3_512()
102+
if sha3Defined {
103+
return C.go_openssl_EVP_sha3_512()
104+
}
99105
}
100106
return nil
101107
}

sha.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,7 @@ func SHA512(p []byte) (sum [64]byte) {
7979

8080
// SupportsHash returns true if a hash.Hash implementation is supported for h.
8181
func SupportsHash(h crypto.Hash) bool {
82-
switch h {
83-
case crypto.MD4, crypto.MD5, crypto.SHA1, crypto.SHA224, crypto.SHA256, crypto.SHA384, crypto.SHA512:
84-
return true
85-
case crypto.SHA3_224, crypto.SHA3_256, crypto.SHA3_384, crypto.SHA3_512:
86-
return vMajor > 1 ||
87-
(vMajor >= 1 && vMinor > 1) ||
88-
(vMajor >= 1 && vMinor >= 1 && vPatch >= 1)
89-
}
90-
return false
82+
return cryptoHashToMD(h) != nil
9183
}
9284

9385
func SHA3_224(p []byte) (sum [28]byte) {

sha_test.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding"
99
"hash"
1010
"io"
11-
"strings"
1211
"testing"
1312

1413
"github.com/golang-fips/openssl/v2"
@@ -44,27 +43,30 @@ func cryptoToHash(h crypto.Hash) func() hash.Hash {
4443

4544
func TestSha(t *testing.T) {
4645
msg := []byte("testing")
47-
var tests = []crypto.Hash{
48-
crypto.MD4,
49-
crypto.MD5,
50-
crypto.SHA1,
51-
crypto.SHA224,
52-
crypto.SHA256,
53-
crypto.SHA384,
54-
crypto.SHA512,
55-
crypto.SHA3_224,
56-
crypto.SHA3_256,
57-
crypto.SHA3_384,
58-
crypto.SHA3_512,
46+
var tests = []struct {
47+
h crypto.Hash
48+
hasMarshaler bool
49+
}{
50+
{crypto.MD4, false},
51+
{crypto.MD5, true},
52+
{crypto.SHA1, true},
53+
{crypto.SHA224, true},
54+
{crypto.SHA256, true},
55+
{crypto.SHA384, true},
56+
{crypto.SHA512, true},
57+
{crypto.SHA3_224, false},
58+
{crypto.SHA3_256, false},
59+
{crypto.SHA3_384, false},
60+
{crypto.SHA3_512, false},
5961
}
6062
for _, tt := range tests {
6163
tt := tt
62-
t.Run(tt.String(), func(t *testing.T) {
64+
t.Run(tt.h.String(), func(t *testing.T) {
6365
t.Parallel()
64-
if !openssl.SupportsHash(tt) {
66+
if !openssl.SupportsHash(tt.h) {
6567
t.Skip("skipping: not supported")
6668
}
67-
h := cryptoToHash(tt)()
69+
h := cryptoToHash(tt.h)()
6870
initSum := h.Sum(nil)
6971
n, err := h.Write(msg)
7072
if err != nil {
@@ -80,12 +82,12 @@ func TestSha(t *testing.T) {
8082
if bytes.Equal(sum, initSum) {
8183
t.Error("Write didn't change internal hash state")
8284
}
83-
if name := tt.String(); name != "MD4" && !strings.HasPrefix(name, "SHA3-") {
85+
if tt.hasMarshaler {
8486
state, err := h.(encoding.BinaryMarshaler).MarshalBinary()
8587
if err != nil {
8688
t.Errorf("could not marshal: %v", err)
8789
}
88-
h2 := cryptoToHash(tt)()
90+
h2 := cryptoToHash(tt.h)()
8991
if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
9092
t.Errorf("could not unmarshal: %v", err)
9193
}

0 commit comments

Comments
 (0)