Skip to content

Commit 7a97d57

Browse files
authored
Wrap hash state not marshallable errors with ErrUnsupported (#287)
* fix:wrap current non marshallable errors with ErrUnsupported * fix:move hash to struct with wrapper * fix:use errors.Is for assertion
1 parent 2ebdf03 commit 7a97d57

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

hash.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,19 @@ func (h *evpHash) Clone() (HashCloner, error) {
372372
return h2, nil
373373
}
374374

375-
var errHashNotMarshallable = errors.New("openssl: hash state is not marshallable")
375+
type errMarshallUnsupported struct{}
376+
377+
func (e errMarshallUnsupported) Error() string {
378+
return "cryptokit: hash state is not marshallable"
379+
}
380+
381+
func (e errMarshallUnsupported) Unwrap() error {
382+
return errors.ErrUnsupported
383+
}
376384

377385
func (d *evpHash) MarshalBinary() ([]byte, error) {
378386
if !d.alg.marshallable {
379-
return nil, errHashNotMarshallable
387+
return nil, errMarshallUnsupported{}
380388
}
381389
buf := make([]byte, 0, d.alg.marshalledSize)
382390
return d.AppendBinary(buf)
@@ -385,7 +393,7 @@ func (d *evpHash) MarshalBinary() ([]byte, error) {
385393
func (d *evpHash) AppendBinary(buf []byte) ([]byte, error) {
386394
defer runtime.KeepAlive(d)
387395
if !d.alg.marshallable {
388-
return nil, errHashNotMarshallable
396+
return nil, errMarshallUnsupported{}
389397
}
390398
d.init()
391399
switch d.alg.provider {
@@ -402,7 +410,7 @@ func (d *evpHash) UnmarshalBinary(b []byte) error {
402410
defer runtime.KeepAlive(d)
403411
d.init()
404412
if !d.alg.marshallable {
405-
return errHashNotMarshallable
413+
return errMarshallUnsupported{}
406414
}
407415
if len(b) < len(d.alg.magic) || string(b[:len(d.alg.magic)]) != d.alg.magic {
408416
return errors.New("openssl: invalid hash state identifier")

hash_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"crypto"
66
"encoding"
7+
"errors"
78
"hash"
89
"io"
910
"runtime"
@@ -195,7 +196,7 @@ func TestHash_BinaryAppender(t *testing.T) {
195196
// Append binary data to the prebuilt slice
196197
state, err := hashWithBinaryAppender.AppendBinary(prebuiltSlice)
197198
if err != nil {
198-
if strings.Contains(err.Error(), "hash state is not marshallable") {
199+
if errors.Is(err, errors.ErrUnsupported) {
199200
t.Skip("AppendBinary not supported")
200201
}
201202
t.Errorf("could not append binary: %v", err)

0 commit comments

Comments
 (0)