Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Feb 22, 2024
1 parent a96aa23 commit 4fd5dbd
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 33 deletions.
20 changes: 12 additions & 8 deletions cryptobin/dsa/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
16 changes: 10 additions & 6 deletions cryptobin/ecdsa/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions cryptobin/eddsa/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()
Expand Down
20 changes: 13 additions & 7 deletions cryptobin/elgamal/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions cryptobin/sm2/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions ed448/ed448.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down

0 comments on commit 4fd5dbd

Please sign in to comment.