crypto-algorithms.c

Basic implementations of standard cryptography algorithms, like AES and SHA-1
git clone git://git.finwo.net/lib/crypto-algorithms.c
Log | Files | Refs | README

base64_test.c (2456B)


      1 /*********************************************************************
      2 * Filename:   base64_test.c
      3 * Author:     Brad Conte (brad AT bradconte.com)
      4 * Copyright:
      5 * Disclaimer: This code is presented "as is" without any guarantees.
      6 * Details:    Performs known-answer tests on the corresponding Base64
      7 	          implementation. These tests do not encompass the full
      8 	          range of available test vectors, however, if the tests
      9 	          pass it is very, very likely that the code is correct
     10 	          and was compiled properly. This code also serves as
     11 	          example usage of the functions.
     12 *********************************************************************/
     13 
     14 /*************************** HEADER FILES ***************************/
     15 #include <stdio.h>
     16 #include <memory.h>
     17 #include "base64.h"
     18 
     19 /*********************** FUNCTION DEFINITIONS ***********************/
     20 int base64_test()
     21 {
     22 	BYTE text[3][1024] = {{"fo"},
     23 	                      {"foobar"},
     24 	                      {"Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."}};
     25 	BYTE code[3][1024] = {{"Zm8="},
     26 	                      {"Zm9vYmFy"},
     27 	                      {"TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\ndGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\ndWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\nZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="}};
     28 	BYTE buf[1024];
     29 	size_t buf_len;
     30 	int pass = 1;
     31 	int idx;
     32 
     33 	for (idx = 0; idx < 3; idx++) {
     34 		buf_len = base64_encode(text[idx], buf, strlen(text[idx]), 1);
     35 		pass = pass && ((buf_len == strlen(code[idx])) &&
     36 		                 (buf_len == base64_encode(text[idx], NULL, strlen(text[idx]), 1)));
     37 		pass = pass && !strcmp(code[idx], buf);
     38 
     39 		memset(buf, 0, sizeof(buf));
     40 		buf_len = base64_decode(code[idx], buf, strlen(code[idx]));
     41 		pass = pass && ((buf_len == strlen(text[idx])) &&
     42 		                (buf_len == base64_decode(code[idx], NULL, strlen(code[idx]))));
     43 		pass = pass && !strcmp(text[idx], buf);
     44 	}
     45 
     46 	return(pass);
     47 }
     48 
     49 int main()
     50 {
     51 	printf("Base64 tests: %s\n", base64_test() ? "PASSED" : "FAILED");
     52 
     53 	return 0;
     54 }