mirror of
https://github.com/danbulant/jose
synced 2026-05-19 20:38:42 +00:00
47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const tagInteger = 0x02;
|
|
const tagSequence = 0x30;
|
|
class Asn1SequenceDecoder {
|
|
constructor(buffer) {
|
|
if (buffer[0] !== tagSequence) {
|
|
throw new TypeError();
|
|
}
|
|
this.buffer = buffer;
|
|
this.offset = 1;
|
|
const len = this.decodeLength();
|
|
if (len !== buffer.length - this.offset) {
|
|
throw new TypeError();
|
|
}
|
|
}
|
|
decodeLength() {
|
|
let length = this.buffer[this.offset++];
|
|
if (length & 0x80) {
|
|
const nBytes = length & ~0x80;
|
|
length = 0;
|
|
for (let i = 0; i < nBytes; i++)
|
|
length = (length << 8) | this.buffer[this.offset + i];
|
|
this.offset += nBytes;
|
|
}
|
|
return length;
|
|
}
|
|
unsignedInteger() {
|
|
if (this.buffer[this.offset++] !== tagInteger) {
|
|
throw new TypeError();
|
|
}
|
|
let length = this.decodeLength();
|
|
if (this.buffer[this.offset] === 0) {
|
|
this.offset++;
|
|
length--;
|
|
}
|
|
const result = this.buffer.slice(this.offset, this.offset + length);
|
|
this.offset += length;
|
|
return result;
|
|
}
|
|
end() {
|
|
if (this.offset !== this.buffer.length) {
|
|
throw new TypeError();
|
|
}
|
|
}
|
|
}
|
|
exports.default = Asn1SequenceDecoder;
|