Skip to content

Commit

Permalink
replace Sha3 RLP Hash with NewLegacyKeccak256
Browse files Browse the repository at this point in the history
  • Loading branch information
bglmmz committed Aug 25, 2022
1 parent 7516f99 commit ba58b8f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion crypto/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func SignSecp256k1(digest []byte, privateKey *ecdsa.PrivateKey) []byte {
//digestHash := ethcrypto.Keccak256([]byte(rawData))
//digestHash = RlpSHA3(rawData)
//digestHash = LegacyKeccak256SHA3(rawData)
sig, err := ethcrypto.Sign(digest, privateKey)
if err != nil {
log.WithError(err).Errorf("failed to sign credential")
Expand Down
20 changes: 20 additions & 0 deletions crypto/keccak256_hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package crypto

import (
ethcommon "github.com/ethereum/go-ethereum/common"
"golang.org/x/crypto/sha3"
"io"
)

// LegacyKeccak256SHA3Hex returns a hex string with 0x prefix
func LegacyKeccak256SHA3Hex(data string) string {
h := LegacyKeccak256SHA3(data)
return h.Hex()
}

func LegacyKeccak256SHA3(data string) (h ethcommon.Hash) {
w := sha3.NewLegacyKeccak256()
io.WriteString(w, data)
w.Sum(h[:0])
return h
}
11 changes: 11 additions & 0 deletions crypto/keccak256_hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package crypto

import "testing"

func Test_LegacyKeccak256SHA3Hex(t *testing.T) {
t.Log(LegacyKeccak256SHA3Hex("test"))
}

func Test_LegacyKeccak256SHA3(t *testing.T) {
t.Log(LegacyKeccak256SHA3("test"))
}
20 changes: 0 additions & 20 deletions crypto/rlp_hash.go

This file was deleted.

7 changes: 0 additions & 7 deletions crypto/rlp_hash_test.go

This file was deleted.

10 changes: 5 additions & 5 deletions types/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c Claim) GetHash(seed uint64) (claimDigest string, rootHash string) {
//json.Marshal会对key按字典顺序排列
claimRawData, _ := json.Marshal(newClaim)
//fmt.Printf("claimRawdata:%s\n", claimRawData)
return crypto.RlpSHA3Hex(string(claimRawData)), crypto.RlpSHA3Hex(allNewValueHashesBuilder.String())
return crypto.LegacyKeccak256SHA3Hex(string(claimRawData)), crypto.LegacyKeccak256SHA3Hex(allNewValueHashesBuilder.String())
}

func GenerateClaimSaltForMap(claimMapSalt map[string]interface{}, seed []byte, builder *strings.Builder) {
Expand All @@ -52,7 +52,7 @@ func GenerateClaimSaltForMap(claimMapSalt map[string]interface{}, seed []byte, b
seed = common.GetHash(seed)
newValue := string(vJson) + strconv.FormatUint(common.BigEndianBytesToUint64(seed), 10)
claimMapSalt[key] = newValue
builder.WriteString(crypto.RlpSHA3Hex(newValue))
builder.WriteString(crypto.LegacyKeccak256SHA3Hex(newValue))
builder.WriteString(" ")
}
} else {
Expand All @@ -63,7 +63,7 @@ func GenerateClaimSaltForMap(claimMapSalt map[string]interface{}, seed []byte, b
newValue := string(vJson) + strconv.FormatUint(common.BigEndianBytesToUint64(seed), 10)
//fmt.Printf("claim key:%s newValue:=%s\n", key, newValue)
claimMapSalt[key] = newValue
builder.WriteString(crypto.RlpSHA3Hex(newValue))
builder.WriteString(crypto.LegacyKeccak256SHA3Hex(newValue))
builder.WriteString(" ")
}
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func SplitForMap(originalClaim, disclosureMap Claim, seed []byte) error {

disclosedValueJson, _ := json.Marshal(disclosedValue)
if string(disclosedValueJson) == "0" { //不披露
originalClaim[key] = crypto.RlpSHA3Hex(newValue)
originalClaim[key] = crypto.LegacyKeccak256SHA3Hex(newValue)
}
}
} else {
Expand All @@ -138,7 +138,7 @@ func SplitForMap(originalClaim, disclosureMap Claim, seed []byte) error {

disclosedValueJson, _ := json.Marshal(disclosedValue)
if string(disclosedValueJson) == "0" { //不披露
originalClaim[key] = crypto.RlpSHA3Hex(newValue)
originalClaim[key] = crypto.LegacyKeccak256SHA3Hex(newValue)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion types/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *Credential) GetDigest(seed uint64) (credentialHash ethcommon.Hash, root
credMap := c.ToMap()
delete(credMap, credentialkeys.PROOF)
credMap[credentialkeys.CLAIM_DATA] = claimHash
return crypto.RlpSHA3(common.MapToJson(credMap)), rootHash
return crypto.LegacyKeccak256SHA3(common.MapToJson(credMap)), rootHash
}

// todo: convert to map by reflect
Expand Down
2 changes: 1 addition & 1 deletion types/presentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ func (p *Presentation) ToRawData() string {
}

func (c *Presentation) GetDigest() ethcommon.Hash {
return crypto.RlpSHA3(c.ToRawData())
return crypto.LegacyKeccak256SHA3(c.ToRawData())
}

0 comments on commit ba58b8f

Please sign in to comment.