diff --git a/cryptobin/dsa/sign.go b/cryptobin/dsa/sign.go index 7ea412cb..a9495c86 100644 --- a/cryptobin/dsa/sign.go +++ b/cryptobin/dsa/sign.go @@ -16,7 +16,7 @@ func (this DSA) Sign(separator ...string) DSA { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, this.data) + hashed, err := this.dataHash(this.data) if err != nil { return this.AppendError(err) } @@ -56,7 +56,7 @@ func (this DSA) Verify(data []byte, separator ...string) DSA { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, data) + hashed, err := this.dataHash(data) if err != nil { return this.AppendError(err) } @@ -103,7 +103,7 @@ func (this DSA) SignASN1() DSA { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, this.data) + hashed, err := this.dataHash(this.data) if err != nil { return this.AppendError(err) } @@ -132,7 +132,7 @@ func (this DSA) VerifyASN1(data []byte) DSA { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, data) + hashed, err := this.dataHash(data) if err != nil { return this.AppendError(err) } @@ -156,7 +156,7 @@ func (this DSA) SignBytes() DSA { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, this.data) + hashed, err := this.dataHash(this.data) if err != nil { return this.AppendError(err) } @@ -200,7 +200,7 @@ func (this DSA) VerifyBytes(data []byte) DSA { r := new(big.Int).SetBytes(sig[:dsaByteLen]) s := new(big.Int).SetBytes(sig[dsaByteLen:]) - hashed, err := this.dataHash(this.signHash, data) + hashed, err := this.dataHash(data) if err != nil { return this.AppendError(err) } @@ -213,8 +213,12 @@ func (this DSA) VerifyBytes(data []byte) DSA { // =============== // 签名后数据 -func (this DSA) dataHash(fn HashFunc, data []byte) ([]byte, error) { - h := fn() +func (this DSA) dataHash(data []byte) ([]byte, error) { + if this.signHash == nil { + return nil, errors.New("Hash func not set.") + } + + h := this.signHash() h.Write(data) return h.Sum(nil), nil diff --git a/cryptobin/ecdsa/sign.go b/cryptobin/ecdsa/sign.go index 161a1940..6e575103 100644 --- a/cryptobin/ecdsa/sign.go +++ b/cryptobin/ecdsa/sign.go @@ -14,7 +14,7 @@ func (this ECDSA) Sign() ECDSA { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, this.data) + hashed, err := this.dataHash(this.data) if err != nil { return this.AppendError(err) } @@ -37,7 +37,7 @@ func (this ECDSA) Verify(data []byte) ECDSA { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, data) + hashed, err := this.dataHash(data) if err != nil { return this.AppendError(err) } @@ -69,7 +69,7 @@ func (this ECDSA) SignBytes() ECDSA { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, this.data) + hashed, err := this.dataHash(this.data) if err != nil { return this.AppendError(err) } @@ -99,7 +99,7 @@ func (this ECDSA) VerifyBytes(data []byte) ECDSA { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, data) + hashed, err := this.dataHash(data) if err != nil { return this.AppendError(err) } @@ -123,8 +123,12 @@ func (this ECDSA) VerifyBytes(data []byte) ECDSA { // =============== // 签名后数据 -func (this ECDSA) dataHash(fn HashFunc, data []byte) ([]byte, error) { - h := fn() +func (this ECDSA) dataHash(data []byte) ([]byte, error) { + if this.signHash == nil { + return nil, errors.New("Hash func not set.") + } + + h := this.signHash() h.Write(data) return h.Sum(nil), nil diff --git a/cryptobin/eddsa/sign.go b/cryptobin/eddsa/sign.go index cc287450..c53d3c73 100644 --- a/cryptobin/eddsa/sign.go +++ b/cryptobin/eddsa/sign.go @@ -14,7 +14,7 @@ func (this EdDSA) Sign() EdDSA { return this.AppendError(err) } - hashed := this.dataHash(this.data, this.options) + hashed := this.dataHash(this.data) sig, err := this.privateKey.Sign(rand.Reader, hashed, this.options) if err != nil { @@ -33,7 +33,7 @@ func (this EdDSA) Verify(data []byte) EdDSA { return this.AppendError(err) } - hashed := this.dataHash(data, this.options) + hashed := this.dataHash(data) err := ed25519.VerifyWithOptions(this.publicKey, hashed, this.data, this.options) if err != nil { @@ -45,9 +45,9 @@ func (this EdDSA) Verify(data []byte) EdDSA { return this } -// 判断是否需要做 hash -func (this EdDSA) dataHash(data []byte, opts *Options) []byte { - hash := opts.HashFunc() +// 判断是否需要 hash 处理 +func (this EdDSA) dataHash(data []byte) []byte { + hash := this.options.HashFunc() if hash == crypto.SHA512 { h := hash.New() diff --git a/cryptobin/elgamal/sign.go b/cryptobin/elgamal/sign.go index 5d33e3de..564fb23c 100644 --- a/cryptobin/elgamal/sign.go +++ b/cryptobin/elgamal/sign.go @@ -10,11 +10,11 @@ import ( // 私钥签名 func (this EIGamal) Sign() EIGamal { if this.privateKey == nil { - err := errors.New("elgamal: privateKey error.") + err := errors.New("privateKey error.") return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, this.data) + hashed, err := this.dataHash(this.data) if err != nil { return this.AppendError(err) } @@ -33,26 +33,32 @@ func (this EIGamal) Sign() EIGamal { // 使用原始数据[data]对比签名后数据 func (this EIGamal) Verify(data []byte) EIGamal { if this.publicKey == nil { - err := errors.New("elgamal: publicKey error.") + err := errors.New("publicKey error.") return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, data) + hashed, err := this.dataHash(data) if err != nil { return this.AppendError(err) } - this.verify, err = elgamal.VerifyASN1(this.publicKey, hashed, this.data) + verify, err := elgamal.VerifyASN1(this.publicKey, hashed, this.data) if err != nil { return this.AppendError(err) } + this.verify = verify + return this } // 签名后数据 -func (this EIGamal) dataHash(fn HashFunc, data []byte) ([]byte, error) { - h := fn() +func (this EIGamal) dataHash(data []byte) ([]byte, error) { + if this.signHash == nil { + return data, errors.New("sign hash error.") + } + + h := this.signHash() h.Write(data) return h.Sum(nil), nil diff --git a/cryptobin/sm2/sign.go b/cryptobin/sm2/sign.go index eaf3e6aa..dcc8c765 100644 --- a/cryptobin/sm2/sign.go +++ b/cryptobin/sm2/sign.go @@ -14,7 +14,7 @@ func (this SM2) Sign() SM2 { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, this.data) + hashed, err := this.dataHash(this.data) if err != nil { return this.AppendError(err) } @@ -39,7 +39,7 @@ func (this SM2) Verify(data []byte) SM2 { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, data) + hashed, err := this.dataHash(data) if err != nil { return this.AppendError(err) } @@ -74,7 +74,7 @@ func (this SM2) SignBytes() SM2 { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, this.data) + hashed, err := this.dataHash(this.data) if err != nil { return this.AppendError(err) } @@ -105,7 +105,7 @@ func (this SM2) VerifyBytes(data []byte) SM2 { return this.AppendError(err) } - hashed, err := this.dataHash(this.signHash, data) + hashed, err := this.dataHash(data) if err != nil { return this.AppendError(err) } @@ -120,12 +120,12 @@ func (this SM2) VerifyBytes(data []byte) SM2 { // =============== // 签名后数据 -func (this SM2) dataHash(fn HashFunc, data []byte) ([]byte, error) { - if fn == nil { +func (this SM2) dataHash(data []byte) ([]byte, error) { + if this.signHash == nil { return data, nil } - h := fn() + h := this.signHash() h.Write(data) return h.Sum(nil), nil diff --git a/ed448/ed448.go b/ed448/ed448.go index da59dfb9..d257878c 100644 --- a/ed448/ed448.go +++ b/ed448/ed448.go @@ -183,7 +183,9 @@ func newKeyFromSeed(privateKey, seed []byte) { if err != nil { panic(err) } + p := new(edwards448.Point).ScalarBaseMult(s) + copy(privateKey, seed) copy(privateKey[57:], p.Bytes()) }