Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17c9ab5
commit 828cb0b
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
var BigInteger = require('cifre/rsa').BigInteger; | ||
|
||
exports.decode = decode; | ||
function decode(data) { | ||
var offset = 0; | ||
|
||
// Decode length headers | ||
function parseLength() { | ||
var value = data[offset++]; | ||
if (value < 0x80) { return value; } | ||
return parseInteger(value & 0x7f); | ||
} | ||
|
||
function parseInteger(length) { | ||
var value = 0; | ||
for (var i = 0; i < length; i++) { | ||
value = (value << 8) | data[offset++]; | ||
} | ||
return value; | ||
} | ||
|
||
function parseBigInteger(length) { | ||
var arr = new Array(length); | ||
for (var i =0; i < length; i++) { | ||
arr[i] = data[offset++]; | ||
}; | ||
return new BigInteger(arr, 256); | ||
} | ||
|
||
function parseSequence(length) { | ||
var arr = []; | ||
var end = offset + length; | ||
while (offset < end) { | ||
arr.push(parse()); | ||
} | ||
return arr; | ||
} | ||
|
||
// Decode type headers | ||
function parse() { | ||
var type = data[offset++]; | ||
var length = parseLength(); | ||
var value; | ||
switch (type) { | ||
case 0x02: | ||
if (length > 6) return parseBigInteger(length); | ||
return parseInteger(length); | ||
case 0x30: | ||
return parseSequence(length); | ||
default: | ||
throw new Error("TODO: Implement data type 0x" + type.toString(16)); | ||
} | ||
} | ||
|
||
return parse(); | ||
} |