Skip to content

Instantly share code, notes, and snippets.

@djom202
Created June 22, 2016 19:40
Show Gist options
  • Select an option

  • Save djom202/5ccdbc2d0f5a0a244092d6777ce41daf to your computer and use it in GitHub Desktop.

Select an option

Save djom202/5ccdbc2d0f5a0a244092d6777ce41daf to your computer and use it in GitHub Desktop.
Encode Username and Password to send header via API Authorization
function setEncode(input) {
var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
chr1, chr2, chr3, enc1, enc2, enc3, enc4 = "",
output = "",
i = 0;
do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
keyStr.charAt(enc1) +
keyStr.charAt(enc2) +
keyStr.charAt(enc3) +
keyStr.charAt(enc4);
chr1 = chr2 = chr3 = enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment