我有一份合同,其中有一个映射表,我将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;
}
}
将struct的哈希集存储到另一个映射中:
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
}