Not logged in. · Lost password · Register
Forum: agsXMPP RSS
Avatar
permafrost #1
Member since Nov 2008 · 6 posts
Group memberships: Members
Show profile · Link to this post
Subject: Creating hash for XEP-0153 avatars
Hi,
I am trying to create hash for vCard avatar, but I ran into some difficulties. I'm using this code for hashing:

                    SHA1CryptoServiceProvider SHA1Hasher = new SHA1CryptoServiceProvider();
                    MemoryStream avatarStream = new MemoryStream();
                    iq.Vcard.Photo.Image.Save(avatarStream, iq.Vcard.Photo.ImageFormat);
                    byte[] hashBytes = SHA1Hasher.ComputeHash(avatarStream);
                    string hash = BitConverter.ToString(hashBytes);
                    avatarStream.Close();
                    hash = hash.Replace("-", "");

XML returned from server is correct and contains the image wchich I can display properly, but the problem is that my code generates the same hash for every image. The hash changes only if ImageFormat is different. I thought that perhaps memory stream is empty and contains only imageformat information, but avatarStream.ToArray() generates different byte array for every image, large enough to contain the avatar. Can anyone please help me? It has been driving me nuts for the past two days :(

permafrost
Avatar
Alex #2
Member since Feb 2003 · 4311 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
You have to create the Hash from the binary data of the Image only.
This thread should help you. It includes sample code:
http://forum.ag-software.de/forum.php?req=thread&id=570

Alex
Avatar
permafrost #3
Member since Nov 2008 · 6 posts
Group memberships: Members
Show profile · Link to this post
I've read through the thread before, and the code posted by CameronKloot does not work. He converts avatar stored in System.Drawing.Image to byte[] using MemoryStream.ToArray(). He then uses agsXMPP.util.Hash.HexToString() to create hash, but this is not a hashing function. All he gets is byte[] converted to string.
I've tried to modify his code and hash that bytearray:
                    SHA1CryptoServiceProvider SHA1Hasher = new SHA1CryptoServiceProvider();
                    byte[] imagebytes;
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                    {
                        iq.Vcard.Photo.Image.Save(ms, iq.Vcard.Photo.ImageFormat);
                        imagebytes = ms.ToArray();
                    }
                    byte[] hashBytes = SHA1Hasher.ComputeHash(imagebytes);
                    string hash = BitConverter.ToString(hashBytes);
                            MessageBox.Show(hash);
                    hash = hash.Replace("-", "");
but the hash is different from what announces the client sending the presence.

permafrost
Avatar
Alex #4
Member since Feb 2003 · 4311 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
This is what you should do:

1) get the image bytes
2) hash them using Sha1 Hash functions yo can use agsXMPP.Util.Sha1HashBytes
3) convert the Hash bytes to the string representation used in xmpp for all hashed using the function agsXMPP.Util.HexToString

Alex
Avatar
permafrost #5
Member since Nov 2008 · 6 posts
Group memberships: Members
Show profile · Link to this post
Thank you, I thought that i was doing basically just what you advised, but getting image bytes by converting base64 string from BINVAL tag is what worked:

                    SHA1CryptoServiceProvider SHA1Hasher = new SHA1CryptoServiceProvider();
                    byte[] imageBytes = Convert.FromBase64String(iq.Vcard.Photo.GetTag("BINVAL"));
                    byte[] hashBytes = SHA1Hasher.ComputeHash(imageBytes);
                    string hash = agsXMPP.util.Hash.HexToString(hashBytes);

I use SHA1CryptoServiceProvider, because agsXMPP.Util.Hash.Sha1HashBytes does not accept byte[] as parameter :(

permafrost
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Forum: agsXMPP RSS