Back

blockchain - solidity - keccak : 科拆客, abi.encode, abi.decode

发布时间: 2024-04-20 00:53:00

refer to: https://youglish.com/pronounce/keccak/english/all/cptc=1

'kechaek

keccak , keccak256, sha3, sha256 类似,有的说是同一个东西。 kimi说不是一个。

keccack256返回的是啥? https://ethereum.stackexchange.com/questions/123176/what-exactly-is-the-output-of-a-keccak256-function

返回的是256位的bits (* 0 .. 1.. 010101 ... )

1100 bits, 代表的十进制是 12 ( 1*2^3 + 1*2^2 ), 对应的16进制(hex) 就是C

1 bytes = 8 bits,     

256 bits = 64 hex(16进制是0~9, A~F)

所以4bits = 1 hex char

  00  0
  01  1
  10: 2   2^1
  11: 3
  100 4   2^2
  101 5
  110 6
  111 7
 1000 8   2^3
1001 9
1010 10 hex: A
1011 11 hex: B
1100 12 2^3 + 2^2 = 8 + 4 = 12, hex: C
1101 13 D
1110 14 E
1111 15 F

  用法:

keccak256(bytes)

把string转换为keccak256:

keccack256(abi.encodePacked("your string"))

encode: 

abi.encode("Hello", 1234);

得到的是: 12 bytes data = 13 hex"000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000007e700000000000000000000000000000000000000000000 0000000000000000000e68656c6c6f20736f6c6964697479000000000000000000000000000000000000";

decode: (把得到的内容进行还原)

abi.encode("hello", 1234, (string memory, uint));

Back