Skip to content

Commit

Permalink
Add ASN1 method to dump (URL-safe) Base64 encoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
lapo-luchini committed Aug 23, 2018
1 parent edc8de4 commit 9e273d4
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions asn1.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ Stream.prototype.hexDump = function (start, end, raw) {
}
return s;
};
var b64Safe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
Stream.prototype.b64Dump = function (start, end) {
var extra = (end - start) % 3,
s = '',
i, c;
for (i = start; i + 2 < end; i += 3) {
c = this.get(i) << 16 | this.get(i + 1) << 8 | this.get(i + 2);
s += b64Safe.charAt(c >> 18 & 0x3F);
s += b64Safe.charAt(c >> 12 & 0x3F);
s += b64Safe.charAt(c >> 6 & 0x3F);
s += b64Safe.charAt(c & 0x3F);
}
if (extra > 0) {
c = this.get(i) << 16;
if (extra > 1) c |= this.get(i + 1) << 8;
s += b64Safe.charAt(c >> 18 & 0x3F);
s += b64Safe.charAt(c >> 12 & 0x3F);
if (extra == 2) s += b64Safe.charAt(c >> 6 & 0x3F);
}
return s;
};
Stream.prototype.isASCII = function (start, end) {
for (var i = start; i < end; ++i) {
var c = this.get(i);
Expand Down Expand Up @@ -364,6 +385,9 @@ ASN1.prototype.posEnd = function () {
ASN1.prototype.toHexString = function () {
return this.stream.hexDump(this.posStart(), this.posEnd(), true);
};
ASN1.prototype.toB64String = function () {
return this.stream.b64Dump(this.posStart(), this.posEnd());
};
ASN1.decodeLength = function (stream) {
var buf = stream.get(),
len = buf & 0x7F;
Expand Down

0 comments on commit 9e273d4

Please sign in to comment.