Skip to content

Instantly share code, notes, and snippets.

@vukicevic
Last active January 1, 2016 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vukicevic/8196318 to your computer and use it in GitHub Desktop.
Save vukicevic/8196318 to your computer and use it in GitHub Desktop.
ASN.1 BER length generator. Pass a length l to the function, outputs an array with a BER format length. JavaScript.
function berLength(l) {
var ll, bl;
if (l < 128) {
bl = [l];
} else {
ll = Math.floor(Math.log(l)/Math.log(256))+1;
bl = [128 + ll];
while (ll-- > 0)
bl.push( (l >> (ll*8)) & 255 );
}
return bl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment