//SPDX-License-Identifier: MIT pragma solidity ^0.8.17; contract hashFunc { function hash(string memory title, uint number, address addr) public pure returns(bytes32){ return keccak256(abi.encodePacked(title, number, addr)); } function sameType(string memory astr, string memory bstr) public pure returns(bytes32){ return keccak256(abi.encode(astr, bstr));//两个数据一样时需要用abi.encode,用abi.encodePacked会出现 //"AAA,BBB"与"AA,ABBB"的哈希值相同 } function sameType(string memory astr) public pure returns(bytes32){//函数重载 return keccak256(abi.encode(astr)); } } contract guessNum { bytes32 public word = 0x0d0fbbfb6f13ca33b055053820891b647dba6d9578df439c1a6543ca379da73e;
function guessWord(string memory _word) public view returns(bool) { return word == keccak256(abi.encode(_word)); } }
decode
encode
1 2 3
function tryEncode(string memory astr, string memory bstr) public pure returns(bytes memory) { return abi.encode(astr, bstr); }
decode
1 2 3
function decode(bytes memory data) public pure returns(string memory, string memory) { return(abi.decode(data,(string, string))); }