Skip to content

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

@marcheg

Description

@marcheg

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions