moonbitlang/x

sha1() yields reversed byte sequence.

Closed this issue · 0 comments

the expression

sha1("The quick brown fox jumps over the lazy dog".to_bytes())

would eval to

[223, 246, 12, 13, 173, 94, 233, 221, 144, 58, 195, 115, 49, 201, 153, 136, 181, 108, 19, 189]
// in hex
[DF,  F6,  0C, 0D, AD,  5E, E9,  DD,  90,  3A, C3,  73,  31, C9,  99,  88,  B5,  6C,  13, BD]

However the correct sha1 for that would be bd136cb58899c93173c33a90dde95ead0d0cf6df, which is the reversal by 1 byte of the above byte sequence.

The fact that it still passed the testcase is because the output function i.e. bytes_to_hex_string reverses the sequence again. So in reality both sha1 bytes_to_hex_string have problem. I happened to notice this when using bytes_to_hex_string with my md5 implementation.

I have submitted a pr to fix this bug alonside the addition of md5.