base64 tests
This commit is contained in:
parent
eb84b00b75
commit
cdbb43f444
@ -61,10 +61,10 @@ int sodium_hex2bin(unsigned char * const bin, const size_t bin_maxlen,
|
||||
const char * const ignore, size_t * const bin_len,
|
||||
const char ** const hex_end);
|
||||
|
||||
#define sodium_bin2b64_VARIANT_ORIGINAL 1
|
||||
#define sodium_bin2b64_VARIANT_ORIGINAL_NO_PADDING 3
|
||||
#define sodium_bin2b64_VARIANT_URLSAFE 5
|
||||
#define sodium_bin2b64_VARIANT_URLSAFE_NO_PADDING 7
|
||||
#define sodium_base64_VARIANT_ORIGINAL 1
|
||||
#define sodium_base64_VARIANT_ORIGINAL_NO_PADDING 3
|
||||
#define sodium_base64_VARIANT_URLSAFE 5
|
||||
#define sodium_base64_VARIANT_URLSAFE_NO_PADDING 7
|
||||
|
||||
SODIUM_EXPORT
|
||||
char *sodium_bin2base64(char * const b64, const size_t b64_maxlen,
|
||||
|
@ -4,20 +4,26 @@
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
unsigned char buf_add[1000];
|
||||
unsigned char buf1[1000];
|
||||
unsigned char buf2[1000];
|
||||
unsigned char buf1_rev[1000];
|
||||
unsigned char buf2_rev[1000];
|
||||
char buf3[33];
|
||||
unsigned char buf4[4];
|
||||
unsigned char nonce[24];
|
||||
char nonce_hex[49];
|
||||
const char * hex;
|
||||
const char * hex_end;
|
||||
size_t bin_len;
|
||||
unsigned int i;
|
||||
unsigned int j;
|
||||
unsigned char buf_add[1000];
|
||||
unsigned char buf1[1000];
|
||||
unsigned char buf2[1000];
|
||||
unsigned char buf1_rev[1000];
|
||||
unsigned char buf2_rev[1000];
|
||||
char buf3[33];
|
||||
unsigned char buf4[4];
|
||||
unsigned char nonce[24];
|
||||
char nonce_hex[49];
|
||||
const char *b64;
|
||||
char *b64_;
|
||||
const char *b64_end;
|
||||
unsigned char *bin;
|
||||
const char *hex;
|
||||
const char *hex_end;
|
||||
size_t b64_len;
|
||||
size_t bin_len;
|
||||
size_t hex_len;
|
||||
unsigned int i;
|
||||
unsigned int j;
|
||||
|
||||
randombytes_buf(buf1, sizeof buf1);
|
||||
memcpy(buf2, buf1, sizeof buf2);
|
||||
@ -41,8 +47,8 @@ main(void)
|
||||
|
||||
hex = "Cafe : 6942";
|
||||
sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len, NULL);
|
||||
printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len, buf4[2], buf4[3],
|
||||
buf4[2], buf4[3]);
|
||||
printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len,
|
||||
buf4[2], buf4[3], buf4[2], buf4[3]);
|
||||
|
||||
hex = "deadbeef";
|
||||
if (sodium_hex2bin(buf1, 1U, hex, 8U, NULL, &bin_len, &hex_end) != -1) {
|
||||
@ -72,6 +78,97 @@ main(void)
|
||||
}
|
||||
printf("dt5: %ld\n", (long) (hex_end - hex));
|
||||
|
||||
printf("%s\n",
|
||||
sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
|
||||
21U, sodium_base64_VARIANT_ORIGINAL));
|
||||
printf("%s\n",
|
||||
sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
|
||||
22U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
||||
printf("%s\n",
|
||||
sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
|
||||
21U, sodium_base64_VARIANT_URLSAFE));
|
||||
printf("%s\n",
|
||||
sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
|
||||
22U, sodium_base64_VARIANT_URLSAFE_NO_PADDING));
|
||||
printf("%s\n",
|
||||
sodium_bin2base64(buf3, 1U, NULL,
|
||||
0U, sodium_base64_VARIANT_ORIGINAL));
|
||||
printf("%s\n",
|
||||
sodium_bin2base64(buf3, 5U, (const unsigned char *) "a",
|
||||
1U, sodium_base64_VARIANT_ORIGINAL));
|
||||
printf("%s\n",
|
||||
sodium_bin2base64(buf3, 5U, (const unsigned char *) "ab",
|
||||
2U, sodium_base64_VARIANT_ORIGINAL));
|
||||
printf("%s\n",
|
||||
sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
|
||||
3U, sodium_base64_VARIANT_ORIGINAL));
|
||||
printf("%s\n",
|
||||
sodium_bin2base64(buf3, 1U, NULL,
|
||||
0U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
||||
printf("%s\n",
|
||||
sodium_bin2base64(buf3, 3U, (const unsigned char *) "a",
|
||||
1U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
||||
printf("%s\n",
|
||||
sodium_bin2base64(buf3, 4U, (const unsigned char *) "ab",
|
||||
2U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
||||
printf("%s\n",
|
||||
sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
|
||||
3U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
||||
|
||||
b64 = "VGhpcyBpcyBhIGpvdXJu" "\n" "ZXkgaW50by" " " "Bzb3VuZA==";
|
||||
memset(buf4, '*', sizeof buf4);
|
||||
sodium_base642bin(buf4, sizeof buf4, b64, strlen(b64), "\n\r ", &bin_len,
|
||||
&b64_end, sodium_base64_VARIANT_ORIGINAL);
|
||||
buf4[bin_len] = 0;
|
||||
printf("[%s]\n", (const char *) buf4);
|
||||
printf("[%s]\n", b64_end);
|
||||
|
||||
memset(buf1, '*', sizeof buf1);
|
||||
sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), "\n\r ", &bin_len,
|
||||
&b64_end, sodium_base64_VARIANT_ORIGINAL);
|
||||
buf1[bin_len] = 0;
|
||||
printf("[%s]\n", (const char *) buf1);
|
||||
assert(*b64_end == 0);
|
||||
|
||||
memset(buf1, '*', sizeof buf1);
|
||||
sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, &bin_len,
|
||||
&b64_end, sodium_base64_VARIANT_ORIGINAL);
|
||||
buf1[bin_len] = 0;
|
||||
printf("[%s]\n", (const char *) buf1);
|
||||
printf("[%s]\n", b64_end);
|
||||
|
||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
||||
NULL, sodium_base64_VARIANT_ORIGINAL) == 0);
|
||||
|
||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
||||
NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
|
||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
|
||||
NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
|
||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
||||
NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
|
||||
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
|
||||
NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
|
||||
|
||||
for (i = 0; i < 1000; i++) {
|
||||
assert(sizeof buf1 >= 100);
|
||||
bin_len = (size_t) randombytes_uniform(100);
|
||||
bin = sodium_malloc(bin_len);
|
||||
b64_len = 1 + (bin_len + 2) / 3 * 4;
|
||||
b64_ = sodium_malloc(b64_len);
|
||||
randombytes_buf(bin, bin_len);
|
||||
memcpy(buf1, bin, bin_len);
|
||||
b64 = sodium_bin2base64(b64_, b64_len, bin, bin_len,
|
||||
sodium_base64_VARIANT_URLSAFE);
|
||||
assert(b64 != NULL);
|
||||
assert(sodium_base642bin(bin, bin_len, b64, b64_len,
|
||||
NULL, NULL, &b64_end,
|
||||
sodium_base64_VARIANT_URLSAFE) == 0);
|
||||
assert(b64_end == &b64[b64_len - 1]);
|
||||
assert(memcmp(bin, buf1, bin_len) == 0);
|
||||
sodium_free(bin);
|
||||
sodium_free(b64_);
|
||||
}
|
||||
|
||||
memset(nonce, 0, sizeof nonce);
|
||||
sodium_increment(nonce, sizeof nonce);
|
||||
printf("%s\n",
|
||||
|
@ -11,6 +11,25 @@ dt2: 2
|
||||
dt3: 11
|
||||
dt4: 11
|
||||
dt5: 11
|
||||
+/DxMDEyMzQ1Njc4OUFCQ0RFRmFi
|
||||
+/DxMDEyMzQ1Njc4OUFCQ0RFRmFiYw
|
||||
-_DxMDEyMzQ1Njc4OUFCQ0RFRmFi
|
||||
-_DxMDEyMzQ1Njc4OUFCQ0RFRmFiYw
|
||||
|
||||
YQ==
|
||||
YWI=
|
||||
YWJj
|
||||
|
||||
YQ
|
||||
YWI
|
||||
YWJj
|
||||
[This]
|
||||
[BpcyBhIGpvdXJu
|
||||
ZXkgaW50by Bzb3VuZA==]
|
||||
[This is a journey into sound]
|
||||
[This is a journ]
|
||||
[
|
||||
ZXkgaW50by Bzb3VuZA==]
|
||||
010000000000000000000000000000000000000000000000
|
||||
000000000000000000000000000000000000000000000000
|
||||
010100000000000000000000000000000000000000000000
|
||||
|
Loading…
Reference in New Issue
Block a user