2

i have two functions in which i calculate the checksume of a string. The first Problem is, i can change numa and numb as i want i always get the same checksume. It is as the string is not used. The second problem is with the same string in somefunction1 i get a different checksum in somefunction2.

test::somefunction1(uint8_t numa, uint8_t numb, uint64_t rndn){
    /*
        some calculation with numa and numb
    */

    std::string data = ""+std::to_string(numa)+std::to_string(numb)+std::to_string(rndn);
    checksum256 hash;
    sha256((char*)&data, data.size(), &hash);

    auto itr= tb_test.find(some_id);
    tb_test.modify(itr, _self, [&](auto& f) {
        f.hashvalue1 = hash;
        f.data1s = data;
    });
}

test::somefunction2(uint8_t numa, uint8_t numb, uint64_t rndn){
    /*
        some calculation with numa and numb
    */

    std::string data = ""+std::to_string(numa)+std::to_string(numb)+std::to_string(rndn);
    checksum256 hash;
    sha256((char*)&data, data.size(), &hash);

    auto itr= tb_test.find(some_id);
    tb_test.modify(itr, _self, [&](auto& f) {
        f.hashvalue2 = hash;
        f.data2s = data;
    });
}
2

I guess the problem lies in your cast to char * and the data.size().

You should try to use the std::string::c_str() method. Also the size you need to provide is the size of your string times the size_of(char).

I use this piece of code:

std::string toHash;
checksum256 calc_hash;        // fc::sha256
sha256(const_cast<char*>(toHash.c_str()), toHash.size() * sizeof(char), &calc_hash);

I would be careful with double encoded string characters, because they may break the size computation. But for normal ASCII chars this should be fine.

Please tell me if this solves your problem.

| improve this answer | |
  • problem solved thx – sinbad Jul 21 '18 at 18:29
  • that's not a best solution, just use &toHash[0] – Jimmy Guo Jul 23 '18 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.