Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hex to bytes function has problems #14

Closed
GoogleCodeExporter opened this issue Feb 19, 2016 · 3 comments
Closed

hex to bytes function has problems #14

GoogleCodeExporter opened this issue Feb 19, 2016 · 3 comments

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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

  • Changed state: WontFix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant