kmd5.c

最終更新:2009/10/10

kmd5.c

001: /* kmd5.c 
002:  * MD5 と BASE64 の例
003:  * MD5: apr_md5
004:  * BASE64: apr_base64_encode / apr_base64_encode_len
005: */
006: 
007: #define USAGE "Usage: kmd5.exe <keyword>\n"
008: 
009: #include "apr_general.h"
010: #include "apr_file_io.h"
011: #include "apr_errno.h"
012: 
013: #include "apr_md5.h"
014: #include "apr_base64.h"
015: 
016: #include "mystab.h"
017: 
018: #define ERROR_BUF_SIZE 1024
019:  
020: int apr_my_main (int ac, char **av, apr_file_t * astdin, apr_file_t * astdout, apr_file_t * astderr, apr_pool_t * pool) {
021: 
022:   char error_buf[ERROR_BUF_SIZE];
023:   apr_status_t statcode;
024:   
025:   unsigned char *data;
026:   unsigned char *encodedData;
027:   
028:   
029:   if (ac < 2) {
030:     apr_file_printf(astderr, USAGE);
031:     goto _ERROR_;
032:   }
033:   
034:   /* 
035:    * INCLUDE: apr_md5.h
036:    * LIBS: libapr-1.lib libaprutil-1.lib
037:    */
038:   
039:   {
040:     unsigned char digest[APR_MD5_DIGESTSIZE];
041:     char *input = av[1];
042:     apr_size_t inputLen = strlen(input);
043:     if (APR_SUCCESS != (statcode = apr_md5(digest, input, inputLen))) {
044:       apr_file_printf(astderr, "ERROR: apr_md5: %s\n", apr_strerror(statcode, error_buf, ERROR_BUF_SIZE));
045:       goto _ERROR_;
046:     }
047:     
048:     {
049:       int i;
050:       apr_file_printf(astdout, "HEX DUMP: [");
051:       for (i=0; i<APR_MD5_DIGESTSIZE; i++)
052:         apr_file_printf(astdout, "%02X", digest[i]);
053:       apr_file_printf(astdout, "]\n");
054:     }
055:     data = digest;
056:   }
057: 
058:   /* apr_base64_encode / apr_base64_encode_len
059:    * INCLUDE: apr_base64.h
060:    * LIBS: libapr-1.lib libaprutil-1.lib
061:    */
062: 
063:   if (data) {
064:     char *input = data;
065:     int inputLen = APR_MD5_DIGESTSIZE;
066: 
067:     char *encoded;
068:     apr_size_t encLen=apr_base64_encode_len(inputLen);
069: 
070:     encoded = apr_palloc(pool, encLen);
071:     memset(encoded, 0, encLen);
072: 
073:     if ((apr_base64_encode(encoded, input, inputLen))<=0) {
074:       goto _ERROR_;
075:     }
076: 
077:     encodedData = encoded;
078:   }
079: 
080:   apr_file_printf(astdout, "BASE64:   [%s][%dbytes]\n", encodedData, strlen(encodedData));
081: 
082:   apr_file_printf(astdout, "\ndone.\n");
083: 
084:   return 0; /* 正常終了 */
085:   
086:   _ERROR_:
087:   apr_file_printf(astderr, "failed!\n");
088:   return 1; /* 異常終了 */
089: }
090: 
Copyright (C) KAKU PROJECT (2009)KAKU PROJECT (2009)