asn1-ts
Version:
ASN.1 encoding and decoding, including BER, CER, and DER.
17 lines (16 loc) • 743 B
JavaScript
import * as errors from "../../../errors.mjs";
import { bufferToInteger } from "../../../utils/bigint.mjs";
import { Buffer } from "node:buffer";
export default function decodeInteger(value) {
if (value.length === 0) {
throw new errors.ASN1SizeError("INTEGER or ENUMERATED encoded on zero bytes");
}
if (value.length > 2
&& ((value[0] === 0xFF && value[1] >= 0b10000000)
|| (value[0] === 0x00 && value[1] < 0b10000000))) {
const buf = Buffer.from(value.slice(0, 16));
throw new errors.ASN1PaddingError("Unnecessary padding bytes on INTEGER or ENUMERATED. "
+ `First 16 bytes of the offending value were: 0x${buf.toString("hex")}`);
}
return bufferToInteger(value);
}