Changeset 5529 for libwired

Show
Ignore:
Timestamp:
05/25/08 15:42:03 (4 months ago)
Author:
morris
Message:

Add wi-digest, and move all digest related functions to it

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libwired/trunk/libwired/data/wi-data.c

    r5367 r5529  
    3232 
    3333#ifdef WI_CRYPTO 
    34 #include <openssl/md5.h> 
    3534#include <openssl/rand.h> 
    36 #include <openssl/sha.h> 
    3735#endif 
    3836 
    3937#include <wired/wi-data.h> 
     38#include <wired/wi-digest.h> 
    4039#include <wired/wi-file.h> 
    4140#include <wired/wi-macros.h> 
     
    191190 
    192191 
    193 wi_data_t * wi_data_init_with_base64(wi_data_t *data, wi_string_t *base64) { 
    194         const char              *buffer; 
    195         char                    ch, inbuffer[4], outbuffer[3]; 
    196         wi_uinteger_t   length, count, i, position, offset; 
    197         wi_boolean_t    ignore, stop, end; 
    198          
    199         length          = wi_string_length(base64); 
    200         buffer          = wi_string_cstring(base64); 
    201         position        = 0; 
    202         offset          = 0; 
    203         data            = wi_data_init_with_capacity(data, length); 
    204          
    205         while(position < length) { 
    206                 ignore = end = false; 
    207                 ch = buffer[position++]; 
    208                  
    209                 if(ch >= 'A' && ch <= 'Z') 
    210                         ch = ch - 'A'; 
    211                 else if(ch >= 'a' && ch <= 'z') 
    212                         ch = ch - 'a' + 26; 
    213                 else if(ch >= '0' && ch <= '9') 
    214                         ch = ch - '0' + 52; 
    215                 else if(ch == '+') 
    216                         ch = 62; 
    217                 else if(ch == '=') 
    218                         end = true; 
    219                 else if(ch == '/') 
    220                         ch = 63; 
    221                 else 
    222                         ignore = true; 
    223                  
    224                 if(!ignore) { 
    225                         count = 3; 
    226                         stop = false; 
    227                          
    228                         if(end) { 
    229                                 if(offset == 0) 
    230                                         break; 
    231                                 else if(offset == 1 || offset == 2) 
    232                                         count = 1; 
    233                                 else 
    234                                         count = 2; 
    235                                  
    236                                 offset = 3; 
    237                                 stop = true; 
    238                         } 
    239                          
    240                         inbuffer[offset++] = ch; 
    241                          
    242                         if(offset == 4) { 
    243                                 outbuffer[0] =  (inbuffer[0]         << 2) | ((inbuffer[1] & 0x30) >> 4); 
    244                                 outbuffer[1] = ((inbuffer[1] & 0x0F) << 4) | ((inbuffer[2] & 0x3C) >> 2); 
    245                                 outbuffer[2] = ((inbuffer[2] & 0x03) << 6) |  (inbuffer[3] & 0x3F); 
    246                                  
    247                                 for(i = 0; i < count; i++) 
    248                                         wi_data_append_bytes(data, &outbuffer[i], 1); 
    249  
    250                                 offset = 0; 
    251                         } 
    252                          
    253                         if(stop) 
    254                                 break; 
    255                 } 
    256         } 
    257          
    258         return data; 
     192wi_data_t * wi_data_init_with_base64(wi_data_t *data, wi_string_t *string) { 
     193        wi_release(data); 
     194         
     195        data = wi_data_from_base64_string(string); 
     196         
     197        return wi_retain(data); 
    259198} 
    260199 
     
    411350 
    412351wi_string_t * wi_data_md5(wi_data_t *data) { 
    413         static unsigned char    hex[] = "0123456789abcdef"; 
    414         MD5_CTX                                 c; 
    415         unsigned char                   md5[MD5_DIGEST_LENGTH]; 
    416         char                                    md5_hex[sizeof(md5) * 2 + 1]; 
    417         wi_uinteger_t                   i; 
    418  
    419         MD5_Init(&c); 
    420         MD5_Update(&c, data->bytes, data->length); 
    421         MD5_Final(md5, &c); 
    422          
    423         for(i = 0; i < MD5_DIGEST_LENGTH; i++) { 
    424                 md5_hex[i+i]    = hex[md5[i] >> 4]; 
    425                 md5_hex[i+i+1]  = hex[md5[i] & 0x0F]; 
    426         } 
    427  
    428         md5_hex[i+i] = '\0'; 
    429  
    430         return wi_string_with_cstring(md5_hex); 
     352        return wi_digest_md5_string(data); 
    431353} 
    432354 
     
    434356 
    435357wi_string_t * wi_data_sha1(wi_data_t *data) { 
    436         static unsigned char    hex[] = "0123456789abcdef"; 
    437         SHA_CTX                                 c; 
    438         unsigned char                   sha1[SHA_DIGEST_LENGTH]; 
    439         char                                    sha1_hex[sizeof(sha1) * 2 + 1]; 
    440         wi_uinteger_t                   i; 
    441  
    442         SHA1_Init(&c); 
    443         SHA1_Update(&c, data->bytes, data->length); 
    444         SHA1_Final(sha1, &c); 
    445          
    446         for(i = 0; i < SHA_DIGEST_LENGTH; i++) { 
    447                 sha1_hex[i+i]   = hex[sha1[i] >> 4]; 
    448                 sha1_hex[i+i+1] = hex[sha1[i] & 0x0F]; 
    449         } 
    450  
    451         sha1_hex[i+i] = '\0'; 
    452  
    453         return wi_string_with_cstring(sha1_hex); 
     358        return wi_digest_sha1_string(data); 
    454359} 
    455360 
     
    459364 
    460365wi_string_t * wi_data_base64(wi_data_t *data) { 
    461         static char                     base64_table[] = 
    462                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 
    463         wi_string_t                     *base64; 
    464         unsigned char           *bytes, inbuffer[3], outbuffer[4]; 
    465         wi_uinteger_t           i, length, count, position, offset, remaining; 
    466         size_t                          size; 
    467  
    468         position        = 0; 
    469         offset          = 0; 
    470         length          = data->length; 
    471         size            = (length * (4.0 / 3.0)) + 3; 
    472         bytes           = data->bytes; 
    473         base64          = wi_string_init_with_capacity(wi_string_alloc(), size); 
    474          
    475         while(position < length) { 
    476                 for(i = 0; i < 3; i++) { 
    477                         if(position + i < length) 
    478                                 inbuffer[i] = bytes[position + i]; 
    479                         else 
    480                                 inbuffer[i] = '\0'; 
    481                 } 
    482  
    483                 outbuffer[0] =  (inbuffer[0] & 0xFC) >> 2; 
    484                 outbuffer[1] = ((inbuffer[0] & 0x03) << 4) | ((inbuffer[1] & 0xF0) >> 4); 
    485                 outbuffer[2] = ((inbuffer[1] & 0x0F) << 2) | ((inbuffer[2] & 0xC0) >> 6); 
    486                 outbuffer[3] =   inbuffer[2] & 0x3F; 
    487  
    488                 remaining = length - position; 
    489                  
    490                 if(remaining == 1) 
    491                         count = 2; 
    492                 else if(remaining == 2) 
    493                         count = 3; 
    494                 else 
    495                         count = 4; 
    496  
    497                 for(i = 0; i < count; i++) 
    498                         wi_string_append_bytes(base64, &base64_table[outbuffer[i]], 1); 
    499  
    500                 for(i = count; i < 4; i++) 
    501                         wi_string_append_bytes(base64, "=", 1); 
    502  
    503                 position += 3; 
    504         } 
    505          
    506         return wi_autorelease(base64); 
     366        return wi_base64_string_from_data(data); 
    507367} 
    508368 
  • libwired/trunk/libwired/data/wi-string.c

    r5462 r5529  
    6565#include <wired/wi-compat.h> 
    6666#include <wired/wi-data.h> 
     67#include <wired/wi-digest.h> 
    6768#include <wired/wi-file.h> 
    6869#include <wired/wi-hash.h> 
     
    366367        wi_data_t               *data; 
    367368         
    368         data = wi_data_init_with_base64(wi_data_alloc(), base64); 
     369        data = wi_data_from_base64_string(base64); 
    369370        string = wi_string_init_with_data(string, data); 
    370         wi_release(data); 
    371371         
    372372        return string; 
     
    16611661 
    16621662wi_string_t * wi_string_md5(wi_string_t *string) { 
    1663         return wi_data_md5(wi_string_data(string)); 
     1663        return wi_digest_md5_string(wi_string_data(string)); 
    16641664} 
    16651665 
     
    16671667 
    16681668wi_string_t * wi_string_sha1(wi_string_t *string) { 
    1669         return wi_data_sha1(wi_string_data(string)); 
     1669        return wi_digest_sha1_string(wi_string_data(string)); 
    16701670} 
    16711671 
     
    16731673 
    16741674wi_string_t * wi_string_base64(wi_string_t *string) { 
    1675         static char                     base64_table[] = 
    1676                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 
    1677         wi_string_t                     *base64_string; 
    1678         unsigned char           inbuffer[3], outbuffer[4]; 
    1679         wi_uinteger_t           i, count, position, offset, remaining, size; 
    1680  
    1681         position = offset = 0; 
    1682         size = string->length * (4.0f / 3.0f) + 4; 
    1683         base64_string = wi_string_init_with_capacity(wi_string_alloc(), size); 
    1684  
    1685         while(position < string->length) { 
    1686                 for(i = 0; i < 3; i++) { 
    1687                         if(position + i < string->length) 
    1688                                 inbuffer[i] = string->string[position + i]; 
    1689                         else 
    1690                                 inbuffer[i] = '\0'; 
    1691                 } 
    1692  
    1693                 outbuffer[0] =  (inbuffer[0] & 0xFC) >> 2; 
    1694                 outbuffer[1] = ((inbuffer[0] & 0x03) << 4) | ((inbuffer[1] & 0xF0) >> 4); 
    1695                 outbuffer[2] = ((inbuffer[1] & 0x0F) << 2) | ((inbuffer[2] & 0xC0) >> 6); 
    1696                 outbuffer[3] =   inbuffer[2] & 0x3F; 
    1697  
    1698                 remaining = string->length - position; 
    1699                  
    1700                 if(remaining == 1) 
    1701                         count = 2; 
    1702                 else if(remaining == 2) 
    1703                         count = 3; 
    1704                 else 
    1705                         count = 4; 
    1706  
    1707                 for(i = 0; i < count; i++) 
    1708                         base64_string->string[offset++] = base64_table[outbuffer[i]]; 
    1709  
    1710                 for(i = count; i < 4; i++) 
    1711                         base64_string->string[offset++] = '='; 
    1712  
    1713                 position += 3; 
    1714         } 
    1715  
    1716         base64_string->string[offset] = '\0'; 
    1717          
    1718         return wi_autorelease(base64_string); 
     1675        return wi_base64_string_from_data(wi_string_data(string)); 
    17191676} 
    17201677 
  • libwired/trunk/libwired/wired.h

    r5417 r5529  
    4040#include <wired/wi-data.h> 
    4141#include <wired/wi-date.h> 
     42#include <wired/wi-digest.h> 
    4243#include <wired/wi-enumerator.h> 
    4344#include <wired/wi-error.h>