47哈希运算

// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

contract HashFunc{
    /**
        智能合约中,hash是一个定长的bytes32
        用keccak256运算hash

        abi的encodePacked和encode不同,encodePacked有一定压缩
    **/
    function hash(string memory text, uint num,address addr) external pure returns (bytes32 ){
        return keccak256(abi.encode(text,num,addr));
    }

    /**
       abi的encodePacked和encode不同,encodePacked有一定压缩
    **/
    // 不定长返回参数加上memory
    // 给参数补0,避免hash collision
    function encode(string memory text0,string memory text1) external pure returns (bytes memory){
        return abi.encode(text0,text1);
    }
    // 没有给参数补0
    // 存在问题,不同的输入参数产生相同的打包结果,hash 碰撞
    function encodePacked(string memory text0,string memory text1) external pure returns (bytes memory){
        return abi.encodePacked(text0,text1);
    }
}