How to use RIPEMD160 to convert string "abc"

Hi
I have a problem
I'm trying to hash a string "abc" with function RIPEMD160, but my
hash_value is not correct
My code is:

string s=”abc”;

int a = (s.size()+1)%64;
int b = 56-a+((a>56)?64:0); PADDING
int tot = a+b+8;

char * testo=new char [tot];

for(int j=0; j<a-1; j++)
testo[j]=s.c_str()[j];
for(int j=a-1; j<tot; j++)
testo[j]=0;

cout<<"adding bits 1000 0000"<<endl;
testo[a-1]= 128; //byte 10000000
unsigned long long sl = s.size()*8;
testo[a+b] = (sl & 0xFF); //Unicode little-endian
testo[a+b+1] = (sl>>8 & 0xFF);
testo[a+b+2] = (sl>>16 & 0xFF);
testo[a+b+3] = (sl>>24 & 0xFF);
testo[a+b+4] = (sl>>32 & 0xFF);
testo[a+b+5] = (sl>>40 & 0xFF);
testo[a+b+6] = (sl>>48 & 0xFF);
testo[a+b+7] = (sl>>56 & 0xFF);


RIPEMD160 hash;

word32 *digest= new word32 [5];

hash.InitState(digest);

for(int j=0; j<tot; j+=64)
{
hash.Transform(digest,(const word32 *)(&(testo[j])));
}

cout<<"The hash value is: "<<endl;
for(int j=0; j<5; j++)
cout<<std::hex<<digest[j];
cout<<endl;

Where is the problem?
Please help me,this is a project for an university exam
Thanks a lot
Is this your own implementation or you got it somewhere? It seems ridiculously complex for a hash function interface.
Last edited on
This is my code,
then how can i do?
Ideally, a hash function interface should have only three or four functions: an initializer; a function that takes a single byte and hashes it as though it was part of a continuous message; a function that takes a void * and a size_t and does the same as the previous one, but for every byte in the array; and a function to get the digest.

Since you're using C++, it makes no sense to keep the digest outside of the object. Leave it as a member of the class.
Your testo array seems unnecessarily complex. Unless I'm mistaken, you're encoding the size of the array at the end of the array. How, may I ask, do you know where to look for the size. You're not passing any other parameters to the function, so I can't imagine what it does.

Your class definition should look something like this:
1
2
3
4
5
6
7
8
class RIPEMD160{
    word32 digest[5];
public:
    RIPEMD160();
    void hash(char byte);
    void hash(void *array,size_t size);
    void result(word32 array[5]);
};


Note: if RIPEMD160 needs padding if the length of a block isn't a multiple of something, you should take care of it yourself inside hash(), rather than leave it up to the user.
Topic archived. No new replies allowed.