So I was trying to implement the SHA-1 hashing algorithm according to http://en.wikipedia.org/wiki/SHA-1 but it doesn't work. I get a hash but it's not what Wikipedia says it should be. What do?
Do you know the difference between rotation and shift?
Name:
Anonymous2012-07-09 16:23
7-san is right. Your message schedule is wrong. When the spec says << it means rotation, not shift. You have to move the bit sifted out into lsb position. Same with temp and c.
Name:
Anonymous2012-07-09 16:26
>>8
Use Java, its integer arithmetic handles this automatically for you
>>10
Good suggestion. OP could also use template specialization to generate precomputed hash outputs for common inputs (eg. "password") at compile time.
template<typename Data>
class rotator
{
private:
Data data;
public:
rotator(Data&& data)
:data(std::move(data))
{
}
rotator(const Data& data)
:data(data)
{
}
Data operator>>(const Data& a)
{
return (data >> a) |
(data << CHAR_BIT*sizeof(Data)-1);
}
Data operator<<(const Data& a)
{
return (data << a) |
(data >> CHAR_BIT*sizeof(Data)-1);
}
Data operator>>(const Data&& a)
{
return (data >> a) |
(data << CHAR_BIT*sizeof(Data)-1);
}
Data operator<<(const Data&& a)
{
return (data << a) |
(data >> CHAR_BIT*sizeof(Data)-1);
}
};