0

I have to convert string to sha256 in dart. For example I use String a = "2424242401224672"; In my code below I get an result as; digest: 7b73641404a8fe6a4b1161a85db736be2a6c07a046109b993186f7a22190bf13

The Code:

  String a = "2424242401224672";
  var bytes = utf8.encode(a.hashCode.toString());
  var digest = sha256.convert(bytes);
  print("digest: $digest");

In other party use c# for sha256. They get bytes from string(object) using c# MemoryStream, BinaryFormatter and XMLSerilizer.

But when I show my result they told me that they get different result in C# sha256?

How to get same result with Dart sha256 and C# sha256?

Update:

The string "2424242401224672" in CSharp has a sha256 value as: DE4841A9E623AF7D5C598A67C2461702485F6B77C3EB5448FA5E0DDF074C70D8

Update-2:

The csharp code:

private static string ComputeHash(byte[] objectAsBytes)
{                        
    try
    {
        SHA256 shaM = new SHA256Managed();
        byte[] result = shaM.ComputeHash(objectAsBytes);

        return byteArrayToHex(result);
    }
    catch (ArgumentNullException ane)
    {
        return null;
    }
}



private static byte[] ObjectToByteArray(Object objectToSerialize)
{            
    MemoryStream ms = new MemoryStream();
    //BinaryFormatter formatter = new BinaryFormatter();            
    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(objectToSerialize.GetType());
    try
    {
        //Here's the core functionality! One Line!
        //To be thread-safe we lock the object
        lock (locker)
        {
            x.Serialize(ms, objectToSerialize);
            //formatter.Serialize(fs, objectToSerialize);
        }
        //return fs.ToArray();                
        return ms.ToArray();
    }
    catch (SerializationException se)
    {                
        return null;
    }
    finally
    {
        //fs.Close();
        ms.Close();
    }
}

}

Nick
  • 4,163
  • 13
  • 38
  • 63
  • I have no idea what the C# version is doing to generate that hash. It doesn't seem correct. Do you have the C# code? – jamesdlin Apr 22 '19 at 07:09
  • you should also show the c# code. Might be caused by different encoding (utf8 vs utf16) – derpirscher Apr 22 '19 at 07:09
  • I tried some of the common encodings with c#. None of them produces your hashcode. So you are doing something wrong on the c# side (maybe the same error as on the scala side) – derpirscher Apr 22 '19 at 07:16
  • I will try to get and update the csharp code – Nick Apr 22 '19 at 07:17
  • I update my question and add the csharp code. – Nick Apr 22 '19 at 07:44
  • I am not familiar with C#, but I suspect that `XmlSerializer` is returning data that contains XML tags. There are much simpler, more straightforward ways to convert [strings to UTF-8 in C#](https://stackoverflow.com/questions/14057434/how-can-i-transform-string-to-utf-8-in-c). – jamesdlin Apr 22 '19 at 07:49
  • @jamesdin, I understand. Do you know how to use XMLSerilizer part in dart? – Nick Apr 22 '19 at 07:51
  • Nope, but I don't think it makes sense to generate a hash from serialized XML. What guarantee is there that two different XML serializers will generate identical XML? What guarantee is that there two different versions of the same XML serializer will generate identical XML? – jamesdlin Apr 22 '19 at 07:56

1 Answers1

1

You did not hash the UTF-8 representation of your string. You hashed the UTF-8 representation of your string's hashcode. That is:

  var bytes = utf8.encode(a.hashCode.toString());

should be just:

  var bytes = utf8.encode(a);
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • jamesdin still I get a different result on csharp – Nick Apr 22 '19 at 06:56
  • @Nick: It would help a lot if you posted what digest you get and what digest you expect. Possibly the C# version is doing something wrong/different. For example, it might be including a NUL-terminator. Making the change I described in my answer generates the same digest as https://emn178.github.io/online-tools/sha256.html. – jamesdlin Apr 22 '19 at 07:03
  • I update my question and add the csharp code. I don't have a csharp knowledge so bear with me. The other party uses csharp code and they found a different result. I don't have a csharp development environment so I can't test if its true or not. – Nick Apr 22 '19 at 07:26
  • @Nick There are a number of online SHA-256 you can compare against. If you're using Linux, then you also can use the `sha256sum` command-line program to compare against. (Note that with with the `sha256sum` program, you'll need to be careful to provide input that does not include a newline. Or add a newline to your Dart string to compare against that.) All of this should be sufficient evidence that the Dart program is generating correct hashes. – jamesdlin Apr 22 '19 at 07:54