Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2011
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <openssl/sha.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7.  
  8. /**
  9.  * characters used for Base64 encoding
  10.  */  
  11. const char *BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  12.  
  13. /**
  14.  * encode three bytes using base64 (RFC 3548)
  15.  *
  16.  * @param triple three bytes that should be encoded
  17.  * @param result buffer of four characters where the result is stored
  18.  */  
  19. void _base64_encode_triple(unsigned char triple[3], char result[4])
  20.  {
  21.     int tripleValue, i;
  22.  
  23.     tripleValue = triple[0];
  24.     tripleValue *= 256;
  25.     tripleValue += triple[1];
  26.     tripleValue *= 256;
  27.     tripleValue += triple[2];
  28.  
  29.     for (i=0; i<4; i++)
  30.     {
  31.  result[3-i] = BASE64_CHARS[tripleValue%64];
  32.  tripleValue /= 64;
  33.     }
  34. }
  35.  
  36. /**
  37.  * encode an array of bytes using Base64 (RFC 3548)
  38.  *
  39.  * @param source the source buffer
  40.  * @param sourcelen the length of the source buffer
  41.  * @param target the target buffer
  42.  * @param targetlen the length of the target buffer
  43.  * @return 1 on success, 0 otherwise
  44.  */  
  45. int base64_encode(unsigned char *source, size_t sourcelen, char *target, size_t targetlen)
  46.  {
  47.     /* check if the result will fit in the target buffer */
  48.     if ((sourcelen+2)/3*4 > targetlen-1)
  49.  return 0;
  50.  
  51.     /* encode all full triples */
  52.     while (sourcelen >= 3)
  53.     {
  54.  _base64_encode_triple(source, target);
  55.  sourcelen -= 3;
  56.  source += 3;
  57.  target += 4;
  58.     }
  59.  
  60.     /* encode the last one or two characters */
  61.     if (sourcelen > 0)
  62.     {
  63.  unsigned char temp[3];
  64.  memset(temp, 0, sizeof(temp));
  65.  memcpy(temp, source, sourcelen);
  66.  _base64_encode_triple(temp, target);
  67.  target[3] = '=';
  68.  if (sourcelen == 1)
  69.      target[2] = '=';
  70.  
  71.  target += 4;
  72.     }
  73.  
  74.     /* terminate the string */
  75.     target[0] = 0;
  76.  
  77.     return 1;
  78. }
  79.  
  80. /**
  81.  * determine the value of a base64 encoding character
  82.  *
  83.  * @param base64char the character of which the value is searched
  84.  * @return the value in case of success (0-63), -1 on failure
  85.  */  
  86. int _base64_char_value(char base64char)
  87.  {
  88.     if (base64char >= 'A' && base64char <= 'Z')
  89.  return base64char-'A';
  90.     if (base64char >= 'a' && base64char <= 'z')
  91.  return base64char-'a'+26;
  92.     if (base64char >= '0' && base64char <= '9')
  93.  return base64char-'0'+2*26;
  94.     if (base64char == '+')
  95.  return 2*26+10;
  96.     if (base64char == '/')
  97.  return 2*26+11;
  98.     return -1;
  99. }
  100.  
  101. /**
  102.  * decode a 4 char base64 encoded byte triple
  103.  *
  104.  * @param quadruple the 4 characters that should be decoded
  105.  * @param result the decoded data
  106.  * @return lenth of the result (1, 2 or 3), 0 on failure
  107.  */  
  108. int _base64_decode_triple(char quadruple[4], unsigned char *result)
  109.  {
  110.     int i, triple_value, bytes_to_decode = 3, only_equals_yet = 1;
  111.     int char_value[4];
  112.  
  113.     for (i=0; i<4; i++)
  114.  char_value[i] = _base64_char_value(quadruple[i]);
  115.  
  116.     /* check if the characters are valid */
  117.     for (i=3; i>=0; i--)
  118.     {
  119.  if (char_value[i]<0)
  120.  {
  121.      if (only_equals_yet && quadruple[i]=='=')
  122.      {
  123.   /* we will ignore this character anyway, make it something
  124.    * that does not break our calculations */
  125.   char_value[i]=0;
  126.   bytes_to_decode--;
  127.   continue;
  128.      }
  129.      return 0;
  130.  }
  131.  /* after we got a real character, no other '=' are allowed anymore */
  132.  only_equals_yet = 0;
  133.     }
  134.  
  135.     /* if we got "====" as input, bytes_to_decode is -1 */
  136.     if (bytes_to_decode < 0)
  137.  bytes_to_decode = 0;
  138.  
  139.     /* make one big value out of the partial values */
  140.     triple_value = char_value[0];
  141.     triple_value *= 64;
  142.     triple_value += char_value[1];
  143.     triple_value *= 64;
  144.     triple_value += char_value[2];
  145.     triple_value *= 64;
  146.     triple_value += char_value[3];
  147.  
  148.     /* break the big value into bytes */
  149.     for (i=bytes_to_decode; i<3; i++)
  150.  triple_value /= 256;
  151.     for (i=bytes_to_decode-1; i>=0; i--)
  152.     {
  153.  result[i] = triple_value%256;
  154.  triple_value /= 256;
  155.     }
  156.  
  157.     return bytes_to_decode;
  158. }
  159.  
  160. /**
  161.  * decode base64 encoded data
  162.  *
  163.  * @param source the encoded data (zero terminated)
  164.  * @param target pointer to the target buffer
  165.  * @param targetlen length of the target buffer
  166.  * @return length of converted data on success, -1 otherwise
  167.  */  
  168. size_t base64_decode(char *source, unsigned char *target, size_t targetlen)
  169.  {
  170.     char *src, *tmpptr;
  171.     char quadruple[4], tmpresult[3];
  172.     int i, tmplen = 3;
  173.     size_t converted = 0;
  174.  
  175.     /* concatinate '===' to the source to handle unpadded base64 data */
  176.     src = (char *)malloc(strlen(source)+5);
  177.     if (src == NULL)
  178.  return -1;
  179.     strcpy(src, source);
  180.     strcat(src, "====");
  181.     tmpptr = src;
  182.  
  183.     /* convert as long as we get a full result */
  184.     while (tmplen == 3)
  185.     {
  186.  /* get 4 characters to convert */
  187.  for (i=0; i<4; i++)
  188.  {
  189.      /* skip invalid characters - we won't reach the end */
  190.      while (*tmpptr != '=' && _base64_char_value(*tmpptr)<0)
  191.   tmpptr++;
  192.  
  193.      quadruple[i] = *(tmpptr++);
  194.  }
  195.  
  196.  /* convert the characters */
  197.  tmplen = _base64_decode_triple(quadruple, tmpresult);
  198.  
  199.  /* check if the fit in the result buffer */
  200.  if (targetlen < tmplen)
  201.  {
  202.      free(src);
  203.      return -1;
  204.  }
  205.  
  206.  /* put the partial result in the result buffer */
  207.  memcpy(target, tmpresult, tmplen);
  208.  target += tmplen;
  209.  targetlen -= tmplen;
  210.  converted += tmplen;
  211.     }
  212.  
  213.     free(src);
  214.     return converted;
  215. }
  216.  
  217.  
  218.  
  219. int main( int argc , void *argv[])
  220. {
  221.   int i;
  222.  
  223.   unsigned char obuf[20];
  224.   unsigned char b64[100];
  225.  
  226.  
  227.   SHA1(argv[1], strlen(argv[1]), obuf);
  228.   base64_encode(obuf,strlen(obuf),b64,100);
  229.   printf("%s\n\n",b64);
  230.  
  231.  
  232.   return 0;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement