-
Notifications
You must be signed in to change notification settings - Fork 64
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
Encoding/decoding negative integers and bigger integers #90
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally looking very good! Thanks for submitting it. Left few comments that I hope to see fixed.
lib/asn1/decoders/der.js
Outdated
@@ -244,6 +244,10 @@ DERNode.prototype._decodeInt = function decodeInt(buffer, values) { | |||
var raw = buffer.raw(); | |||
var res = new bignum(raw); | |||
|
|||
if ((raw[0] & 0x80) > 0) { // negative |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be !== 0
? Please consider putting the comment on the line before if
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another note: should we rather use bignum.fromTwos
here, and avoid creating new bignum
if the result is negative?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, bignum.fromTwos doesn't exist.
lib/asn1/encoders/der.js
Outdated
// check if sign bit is occupied | ||
if ((bitLength % 8) == 0) { | ||
// when number is of form 10..0 no correction is needed | ||
if (num.zeroBits() + 1 != num.bitLength()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use spaces for indent.
lib/asn1/encoders/der.js
Outdated
// check if sign bit is occupied | ||
if ((bitLength % 8) == 0) { | ||
// when number is of form 10..0 no correction is needed | ||
if (num.zeroBits() + 1 != num.bitLength()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!==
lib/asn1/encoders/der.js
Outdated
var bitLength = num.bitLength(); | ||
var width = Math.ceil(bitLength / 8) * 8; | ||
// check if sign bit is occupied | ||
if ((bitLength % 8) == 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
===
lib/asn1/encoders/der.js
Outdated
} | ||
} | ||
numArray = num.toTwos(width).toArray(); | ||
//console.log(numArray); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove all commented code.
I actually just added eslint to the |
Thx, I also want to fix encoding JS integers > 2^32-1 while we are here. If you don't mind I'll redirect them to BN. |
Hi, I fixed lint issues and also added support for integers >= 2^31. I don't see BN.fromTwos in BN library so I don't see an opportunity to handle it in a more efficient way. Note that test "should encode OCSP request" fails, but it fails without my changes too. |
Is this going to be merged? |
Is this only a conflict issue ? I need number support > 32 bits. |
I added support for encoding & decoding negative integers. Existing implementation was producing erroneous values.
I've added tests for numbers at the edges: -1, -128, -129. No extensive testing was done.