Last active
January 18, 2017 15:14
-
-
Save darelf/a4bc9effa165da5d72dd86960227f309 to your computer and use it in GitHub Desktop.
Do base64url encoding with https://kore.io
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
#include <kore/kore.h> | |
#include <string.h> | |
char * base64_url_decode(char * in) { | |
size_t len; | |
char * padded; | |
u_int8_t * out; | |
unsigned int i; | |
len = strlen(in); | |
int remain = (len % 4); | |
if (remain == 2) { | |
snprintf(padded, (len + 3), "%s==", in); | |
} else if (remain == 3) { | |
snprintf(padded, (len + 2), "%s=", in); | |
} | |
for (i = 0; i < strlen(padded); i++) { | |
if (padded[i] == '_') padded[i] = '/'; | |
if (padded[i] == '-') padded[i] = '+'; | |
} | |
kore_base64_decode(padded, &out, &len); | |
return (char *)out; | |
} | |
char * base64_url_encode(const char * in) { | |
char * base_out; | |
char * out; | |
unsigned int i; | |
kore_base64_encode((u_int8_t *)in, strlen(in), &base_out); | |
kore_strip_chars(base_out, '=', &out); | |
for (i = 0; i < strlen(out); i++) { | |
if (out[i] == '/') { | |
out[i] = '_'; | |
} | |
if (out[i] == '+') { | |
out[i] = '-'; | |
} | |
} | |
return out; | |
} | |
char * base64_url_encode_binary(u_int8_t * in, size_t len) { | |
char * base_out; | |
char * out; | |
unsigned int i; | |
kore_base64_encode(in, len, &base_out); | |
kore_strip_chars(base_out, '=', &out); | |
for (i = 0; i < strlen(out); i++) { | |
if (out[i] == '/') { | |
out[i] = '_'; | |
} | |
if (out[i] == '+') { | |
out[i] = '-'; | |
} | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment