14
pragma solidity ^0.4.11;
contract test2 
{

    address creater;
    string username;
    string password;

    function testusernamepassword(string username,string password) returns (bool) 
    {
        if (username == "deepak" && password == "123") //error: operator == is not compatible with string ?
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
29

You can compare strings like this:

    function compareStrings (string memory a, string memory b) public view 
       returns (bool) {
  return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))) );

       }

As a side note: It's not secure at all dealing with passwords and usernames in Solidity.

  • 2
    keccak256 is Ethereum's native hash function. ethereum.stackexchange.com/a/554/229 – ripper234 Jun 11 '18 at 12:11
  • 1
    Note, this emits a warning in Remix. imgur.com/a/ruV6IM1 – ripper234 Jun 11 '18 at 12:15
  • Gives following error: equires a single bytes argument. Use abi.encodePacked(...) to obtain the pre-0.5.0 behaviour or abi.encode(...) to use ABI encoding. @pabloruiz55 – alper Mar 7 at 15:59
0

Strings are not really a primitive type in Solidity. There are lots of things that are missing, a bit like in old C. They are a lot like a bytes array, so the low level of comparing would be to treat them as such and iterate over the arrays.

PabloRuiz's answer is a better choice.

If you want to know a little more about strings, you could try my blog post on that subject.

Regarding testing user names and passwords in Solidity, keep in mind that everything you send in a transaction is visible to everyone. The data can be retrieved from a blockchain explorer. The first thing you should try, at least, is to Hash the password, but even then, who ever finds the hash can copy it and relay it for themselves in a fake authentication. In non-blockchain systems, you'd usually use some kind of challenge-response setup. If you could give more details about your authentication setup, people here might be able to give some ideas around it.

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.