Created
June 22, 2016 19:40
-
-
Save djom202/5ccdbc2d0f5a0a244092d6777ce41daf to your computer and use it in GitHub Desktop.
Encode Username and Password to send header via API Authorization
This file contains hidden or 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
| 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