@@ -21,15 +21,16 @@ import (
21
21
"crypto/hmac"
22
22
"crypto/rand"
23
23
"crypto/rsa"
24
- "crypto/sha256"
25
- "crypto/sha512"
26
24
"crypto/x509"
27
25
"errors"
28
26
"fmt"
29
27
"hash"
30
28
"math/big"
31
29
"reflect"
32
30
31
+ "crypto/sha256"
32
+ "crypto/sha512"
33
+
33
34
"github.com/hyperledger/fabric/bccsp"
34
35
"github.com/hyperledger/fabric/bccsp/utils"
35
36
"github.com/hyperledger/fabric/common/flogging"
@@ -92,13 +93,24 @@ func New(securityLevel int, hashFamily string, keyStore bccsp.KeyStore) (bccsp.B
92
93
verifiers [reflect .TypeOf (& rsaPrivateKey {})] = & rsaPrivateKeyVerifier {}
93
94
verifiers [reflect .TypeOf (& rsaPublicKey {})] = & rsaPublicKeyKeyVerifier {}
94
95
95
- return & impl {
96
+ // Set the hashers
97
+ hashers := make (map [reflect.Type ]Hasher )
98
+ hashers [reflect .TypeOf (& bccsp.SHAOpts {})] = & hasher {hash : conf .hashFunction }
99
+ hashers [reflect .TypeOf (& bccsp.SHA256Opts {})] = & hasher {hash : sha256 .New }
100
+ hashers [reflect .TypeOf (& bccsp.SHA384Opts {})] = & hasher {hash : sha512 .New384 }
101
+ hashers [reflect .TypeOf (& bccsp.SHA3_256Opts {})] = & hasher {hash : sha3 .New256 }
102
+ hashers [reflect .TypeOf (& bccsp.SHA3_384Opts {})] = & hasher {hash : sha3 .New384 }
103
+
104
+ impl := & impl {
96
105
conf : conf ,
97
106
ks : keyStore ,
98
107
encryptors : encryptors ,
99
108
decryptors : decryptors ,
100
109
signers : signers ,
101
- verifiers : verifiers }, nil
110
+ verifiers : verifiers ,
111
+ hashers : hashers }
112
+
113
+ return impl , nil
102
114
}
103
115
104
116
// SoftwareBasedBCCSP is the software-based implementation of the BCCSP.
@@ -110,6 +122,7 @@ type impl struct {
110
122
decryptors map [reflect.Type ]Decryptor
111
123
signers map [reflect.Type ]Signer
112
124
verifiers map [reflect.Type ]Verifier
125
+ hashers map [reflect.Type ]Hasher
113
126
}
114
127
115
128
// KeyGen generates a key using opts.
@@ -619,51 +632,33 @@ func (csp *impl) GetKey(ski []byte) (k bccsp.Key, err error) {
619
632
620
633
// Hash hashes messages msg using options opts.
621
634
func (csp * impl ) Hash (msg []byte , opts bccsp.HashOpts ) (digest []byte , err error ) {
622
- var h hash. Hash
635
+ // Validate arguments
623
636
if opts == nil {
624
- h = csp .conf .hashFunction ()
625
- } else {
626
- switch opts .(type ) {
627
- case * bccsp.SHAOpts :
628
- h = csp .conf .hashFunction ()
629
- case * bccsp.SHA256Opts :
630
- h = sha256 .New ()
631
- case * bccsp.SHA384Opts :
632
- h = sha512 .New384 ()
633
- case * bccsp.SHA3_256Opts :
634
- h = sha3 .New256 ()
635
- case * bccsp.SHA3_384Opts :
636
- h = sha3 .New384 ()
637
- default :
638
- return nil , fmt .Errorf ("Algorithm not recognized [%s]" , opts .Algorithm ())
639
- }
637
+ return nil , errors .New ("Invalid opts. It must not be nil." )
640
638
}
641
639
642
- h .Write (msg )
643
- return h .Sum (nil ), nil
640
+ hasher , found := csp .hashers [reflect .TypeOf (opts )]
641
+ if ! found {
642
+ return nil , fmt .Errorf ("Unsupported 'HashOpt' provided [%v]" , opts )
643
+ }
644
+
645
+ return hasher .Hash (msg , opts )
644
646
}
645
647
646
648
// GetHash returns and instance of hash.Hash using options opts.
647
649
// If opts is nil then the default hash function is returned.
648
650
func (csp * impl ) GetHash (opts bccsp.HashOpts ) (h hash.Hash , err error ) {
651
+ // Validate arguments
649
652
if opts == nil {
650
- return csp . conf . hashFunction (), nil
653
+ return nil , errors . New ( "Invalid opts. It must not be nil." )
651
654
}
652
655
653
- switch opts .(type ) {
654
- case * bccsp.SHAOpts :
655
- return csp .conf .hashFunction (), nil
656
- case * bccsp.SHA256Opts :
657
- return sha256 .New (), nil
658
- case * bccsp.SHA384Opts :
659
- return sha512 .New384 (), nil
660
- case * bccsp.SHA3_256Opts :
661
- return sha3 .New256 (), nil
662
- case * bccsp.SHA3_384Opts :
663
- return sha3 .New384 (), nil
664
- default :
665
- return nil , fmt .Errorf ("Algorithm not recognized [%s]" , opts .Algorithm ())
656
+ hasher , found := csp .hashers [reflect .TypeOf (opts )]
657
+ if ! found {
658
+ return nil , fmt .Errorf ("Unsupported 'HashOpt' provided [%v]" , opts )
666
659
}
660
+
661
+ return hasher .GetHash (opts )
667
662
}
668
663
669
664
// Sign signs digest using key k.
0 commit comments