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

encoding of integers is does not work for 0x80 #3

Open
marcheg opened this issue Apr 13, 2018 · 0 comments · May be fixed by #7
Open

encoding of integers is does not work for 0x80 #3

marcheg opened this issue Apr 13, 2018 · 0 comments · May be fixed by #7

Comments

@marcheg
Copy link

marcheg commented Apr 13, 2018

Integer of 128 (0x80) must be encoded as 0x00 0x80. It effectively applies to all numbers which have the highest bit set to 1 (not only to 128 obviously).

It impacts your asn1.go functions sizeInt64/writeInt64.

I quickly workarounded this item in your lib

func sizeInt64(i int64) (size int) {
	prev := int64(0)
	for ; i != 0 || size == 0; i >>= 8 {
		size++
		prev = i
	}
	if prev&0x80 == 0x80 {
		size++
	}
	return
}

func writeInt64(bytes *Bytes, i int64) (size int) {
	prev := int64(0)
	for ; i != 0 || size == 0; i >>= 8 { // Write at least one byte even if the value is 0
		bytes.writeBytes([]byte{byte(i)})
		size++
		prev = i
	}
	if prev&0x80 == 0x80 {
		bytes.writeBytes([]byte{byte(0)})
		size++
	}
	return
}
@superboum superboum linked a pull request Sep 15, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant