abi.encodePacked()
abi.encodePacked() 함수는 여러 인자들을 사용할 때, 특정 상황에서 해시 충돌을 일으킬 수 있다. 이 함수는 배열의 일부인지 여부에 상관없이 모든 요소들을 순서대로 패킹한다. 따라서 요소들을 배열 간에 이동시키더라도 모든 요소들이 같은 순서로 있으면 동일한 인코딩을 반환합니다.
그래서 abi.encodePacked()를 사용하기보다 abi.encode()를 사용하는 것을 추천한다.
function callencodePackedHash ( ) external pure returns(bytes memory) {
bytes memory valueREtun = abi.encodePacked(int16(-1), bytes1(0x42), uint16(0x03), string("Hello, world!"));
return valueREtun ;
}
function callencodeHash ( ) external pure returns(bytes memory) {
bytes memory valueREtun = abi.encode(int16(-1), bytes1(0x42), uint16(0x03), string("Hello, world!"));
return valueREtun ;
}
codePacked는 000이라 불리는 패딩이 없는 것을 확인할 수 있다.
function callPadding() external pure returns (bytes memory,bytes memory) {
return (abi.encodePacked(uint16(0x12)), hex"0012" );
}
패딩이 필요할때는 명시적으로 표시해 준다.
[ 참고 및 추가자료 ]
https://docs.soliditylang.org/en/v0.5.3/abi-spec.html#non-standard-packed-mode
https://swcregistry.io/docs/SWC-133/
'자주나오는패턴' 카테고리의 다른 글
[Common Vulnerabilities] Missing Protection against Signature Replay Attacks (0) | 2024.01.05 |
---|---|
[Common Vulnerabilities] Insufficient Gas Griefing (0) | 2024.01.05 |
[Common Vulnerabilities] DoS With Block Gas Limit (0) | 2024.01.03 |
[Common Vulnerabilities] Message call with hardcoded gas amount (0) | 2024.01.01 |
[Common Vulnerabilities] Price Oracle Manipulation (0) | 2023.12.12 |