web3.js sha3、keccack256、randomHex

  • web3.utils.sha3 – sha256哈希函数
  • web3.utils.keccak256 – keccak256哈希函数
  • web3.utils.randomHex – 生成十六进制随机数

示例

app.js

const Web3 = require('web3')
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_API_KEY') // YOUR_INFURA_API_KEY替换为你自己的key

// sha256哈希函数
console.log(web3.utils.sha3('qikegu.com'))

// keccak256哈希函数
console.log(web3.utils.keccak256('qikegu.com'))

// 生成十六进制随机数
console.log(web3.utils.randomHex(32))

运行app.js

$ node app

输出

0xd7b9972e7f508c3ebe64894ef1cba79f12b0e31267f5a6d5187a5c2d893dc282
0xd7b9972e7f508c3ebe64894ef1cba79f12b0e31267f5a6d5187a5c2d893dc282
0x060b008cf3b9abea3ee8c2c113fe7d4485e73d9d7eb63bc824846fb2da1dc509

Web3.js附带了underscoreJS库,可用于操作JavaScript数组或对象。示例app.jsconst Web3 = require('web3')const web3 = new ...