如何从哈希值返回id:

时间:2018-04-04 01:23:43

标签: ethereum solidity smartcontracts

我与映射表签订了合同,我将id(uint)映射到哈希值。我可以从id返回哈希值,但如果我想反过来做什么,比如从哈希值中获取id。这是我正在使用的代码:

pragma solidity ^0.4.18;

contract Hash {
  bytes32 comphash;

  struct hashstruct {   
    bytes32 fhash;
  }

  mapping (uint => hashstruct) hashstructs;
  uint[] public hashAccts;

  function setinstructor(uint _uint,string _fhash) public {
    var a = hashstructs[_uint];

    a.fhash = sha256(_fhash);  

    hashAccts.push(_uint) -1;              
  }

  function getInstructor(uint ins) view public returns (bytes32) {
    return (hashstructs[ins].fhash);
  }

  function count() view public returns (uint) {
    return hashAccts.length;
  }            
}

1 个答案:

答案 0 :(得分:0)

将结构的哈希值存储到另一个映射中:

mapping(bytes32 => uint) _map;

function setinstructor(uint _uint,string _fhash) public {
  var a = hashstructs[_uint];

  a.fhash = sha256(_fhash);  

  hashAccts.push(_uint) -1;              

  _map[keccak256(a.fhash)] = _uint; // Can pass in other struct members as well
}