VansonLeung / crypto-js

Automatically exported from code.google.com/p/crypto-js
0 stars 0 forks source link

hex to bytes function has problems #14

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Call method Crypto.util.hexToBytes(helloHex);
2. With a hex number with uneven number of digits e.g. 3, 5, 7 and so on (12F, 
34F52)
3. then the result is not correct since the function reads indata in sets of two

What is the expected output? What do you see instead?
input: 1024 
output: [64,0]
should be: [4,0]

What version of the product are you using? On what operating system?
Chrome and 2.5.1

This could be a solution, it might not be perfect but it works
// Convert a hex string to a byte array
hexToBytes: function (hex) {
  hex=(hex.length%2==0?"":"0")+hex;
  for (var bytes = [], c = 0; c < hex.length; c += 2)
    bytes.push(parseInt(hex.substr(c, 2), 16));
  return bytes;
}

Original issue reported on code.google.com by sam...@erdtman.se on 4 Oct 2011 at 12:58

GoogleCodeExporter commented 9 years ago
I can reproduce and confirm this issue. Though, I'm undecided if this is 
something that needs fixing. Since this is a crypto library, it was only ever 
meant to operate on whole bytes.

Original comment by Jeff.Mott.OR on 5 Oct 2011 at 6:03

GoogleCodeExporter commented 9 years ago
I agree with Jeff especially since it is unclear what would be expected. 12F 
could easily be expected to be [0x12, 0xF0] instead of [0x1, 0x2F] depending on 
whether you expect the padding 0 to be first or last. An even number of digits 
should be considered invalid input to this function and the only reasonable 
change would be to throw but since this is a low level routine even that test 
isn't justified.

Original comment by Chuck...@gmail.com on 26 Oct 2011 at 8:05

GoogleCodeExporter commented 9 years ago

Original comment by Jeff.Mott.OR on 2 Nov 2011 at 11:44