# 签名 登录 验证 **Published by:** [point](https://paragraph.com/@point/) **Published on:** 2022-05-03 **URL:** https://paragraph.com/@point/9HtpYR6FKdaFW0VMQd40 ## Content 1. etherjs签名async walletSignMessage(message) { const signer = await window.ethProvider.getSigner(); const result = await signer.signMessage(message); return result; } 2.go后端验证func VerifySig(address, signature, msg string) bool { bytes := []byte(msg) fromAddr := common.HexToAddress(address) sig := hexutil.MustDecode(signature) if sig[64] != 27 && sig[64] != 28 { return false } sig[64] -= 27 pubKey, err := crypto.SigToPub(signHash(bytes), sig) if err != nil { return false } recoveredAddr := crypto.PubkeyToAddress(*pubKey) fmt.Println(recoveredAddr) result := fromAddr == recoveredAddr return result } func signHash(data []byte) []byte { msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data) return crypto.Keccak256([]byte(msg)) } 为了提高安全性,还可以在前端生成时加入salt,时间戳之类的限制 ## Publication Information - [point](https://paragraph.com/@point/): Publication homepage - [All Posts](https://paragraph.com/@point/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@point): Subscribe to updates